Python Logs

Python is a high-level, general-purpose programming language, and its standard library comes with a logging module. This module provides a framework to implement a flexible event-logging system for applications and libraries. Use any logging format supported by Python's logging module or a custom logger to log to a file and send the file to Site24x7 AppLogs for monitoring.

Table of contents

Configuring your logger

Use any Python logger to record logging. Use this sample code to configure the logger to record logging events to a file:

import logging
logging.basicConfig(filename=`<application_dir>/logs/<FILE_NAME>.log`, encoding='utf-8', level=logging.DEBUG)
logging.debug('Error message')


Getting started with log management

  1. Log in to your Site24x7 account.
  2. Download and install the Site24x7 Server monitoring agent (Windows | Linux).
  3. Go to Admin > AppLogs > Log Profile, and click Add Log Profile.
    • Profile Name: Enter a name for your Log Profile.
    • Choose the Log Type: Select Python logs from the drop-down menu.
    • Log Source: Select Local File from the drop-down menu.
    • List of files to search for logs: Add the file path configured in your logger.
      E.g. <application_dir>/logs/<FILE_NAME>.log
  4. Select the server, and click Save.

Sample log and log pattern

You can add the required log pattern matching your logs using multiple log pattern support. Here are a few sample logs and their log pattern applicable to the Python logs:

Trace
Log pattern

$DateTime:date$ $LoggerName$ - $LogLevel$:$Message$!<NewLine>$BackTrace$!

Sample log
2023-01-10 07:35:05,456 main - ERROR:[Errno 2] No such file or directory: 'sample.txt<NewLine>Traceback (most recent call last):<NewLine>File "/App/config/loader.py"

Normal log
Log pattern

$DateTime:date$ $LogLevel$:$Message$!<NewLine>$BackTrace$!

Sample log
2023-01-10 07:35:05,456 DEBUG: Debug Message

JSON
Log pattern

json $exc_info as BackTrace$ $name as LoggerName$ $levelname as LogLevel$ $message as Message$ $asctime as DateTime:date:yyyy-MM-dd HH:mm:ss,SSS$

Sample log
{"asctime": "2023-01-10 07:35:05,456", "name": "__main__", "levelname": "ERROR", "message": "[Errno 2] No such file or directory: 'sample.txt'","exc_info": "Traceback (most recent call last):\n File \"/App/config/loader.py\", line 73, in __init__"}

As we process the first matched log pattern, make sure you mention the specific log pattern first, then go for the generic pattern. For example, when you define the trace and normal log pattern above, mention the specific log pattern (e.g., Trace) first, followed by the generic pattern (e.g., Default).

Log profile

Dashboard

AppLogs creates an exclusive dashboard for every log type and shows a few widgets by default. Here's a list of the widgets available on the Python logs dashboard:

  • Logging Levels
  • Exceptions

Dashboard

Related log type

Was this document helpful?
Thanks for taking the time to share your feedback. We’ll use your feedback to improve our online help resources.