Graylog: Example: Sending logs from Python

This documentation is part of the Using with Python guide. You can view the complete guide here: How to send logs from Python to Graylog.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Graylog cloud solution that provides a host of benefits, including:

  • Unlimited and dedicated SMTP email server included.
  • Effortless updates with just a click.
  • Customizable domain name secured with HTTPS (for example, https://logs.your-company.com).
  • Optimal performance and robust security powered by a private and dedicated VM.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's Graylog cloud hosting solution!

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

Next, create a file named graylog-example.py and replace <XXXXXX>.stackhero-network.com with your 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 is running, 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

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

Detailed logDetailed log