Graylog: Example: Sending logs from Python

This documentation is part of the Using with Python guide. View the full guide here: How to send logs from Python to Graylog.

👋 Welcome to Stackhero documentation

Stackhero provides a fully managed Graylog cloud solution designed for speed and simplicity. You can:

  • Rely on an unlimited, dedicated SMTP email server included with your service.
  • Apply updates effortlessly with a single click, with no manual intervention needed.
  • Use a custom domain name secured by HTTPS (for example, https://logs.your-company.com), giving your team secure, direct access.
  • Experience strong performance and security on a private, dedicated infrastructure with no shared resources or noisy neighbors.

Focus on your data, not your tooling: you can get started with Stackhero's Graylog cloud hosting solution in just a few minutes.

For this example, you can use the graypy library. To get started, simply install it using pip install graypy.

Next, create a file named graylog-example.py and replace <XXXXXX>.stackhero-network.com with your own server's hostname. Here is a sample script you can try:

import logging
import graypy

my_logger = logging.getLogger('test_logger')
my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFTLSHandler('<XXXXXX>.stackhero-network.com', 12201)
my_logger.addHandler(handler)

my_logger.debug('This is a test from a Python script!')

To view your logs, open the Graylog admin panel, go to the "Search" tab, click the play icon, and set it to update every second. You can run your script with:

python graylog-example.py

Once the script runs, your log message will be sent to Graylog, and you should see it appear in the search results.

Your first log received on GraylogYour first log received on Graylog

Clicking on the message "This is a test from a Python script!" will show you useful details such as the script name and the exact line that generated the log.

Detailed logDetailed log