Logging

This page is about the Python agent’s internal logging, if you’re interested in Scout’s Managed Logs for Python applications, you’ll find that over here.

Logging

Scout logs via the built-in Python logger, which means you can add a handler to the scout_apm package. If you don’t setup logging, use the examples below as a starting point.

In situations where you have a handler attached to the Root Logger that is catching all logs at DEBUG level (perhaps set via basicConfig), you may want to attach a separate handler to the scout_apm logger at a higher level to keep it from filling up your logs with debug messages.

Log Levels

The following log levels are available:

Django Logging

To log Scout agent output in your Django application, copy the following into your settings.py file:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'stdout': {
            'format': '%(asctime)s %(levelname)s %(message)s',
            'datefmt': '%Y-%m-%dT%H:%M:%S%z',
        },
    },
    'handlers': {
        'stdout': {
            'class': 'logging.StreamHandler',
            'formatter': 'stdout',
        },
        'scout_apm': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'scout_apm_debug.log',
        },
    },
    'root': {
        'handlers': ['stdout'],
        'level': os.environ.get('LOG_LEVEL', 'DEBUG'),
    },
    'loggers': {
        'scout_apm': {
            'handlers': ['scout_apm'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

Flask Logging

Add the following your Flask app:

dictConfig({
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'stdout': {
            'format': '%(asctime)s %(levelname)s %(message)s',
            'datefmt': '%Y-%m-%dT%H:%M:%S%z',
        },
    },
    'handlers': {
        'stdout': {
            'class': 'logging.StreamHandler',
            'formatter': 'stdout',
        },
        'scout_apm': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'scout_apm_debug.log',
        },
    },
    'root': {
        'handlers': ['stdout'],
        'level': os.environ.get('LOG_LEVEL', 'DEBUG'),
    },
    'loggers': {
        'scout_apm': {
            'handlers': ['scout_apm'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
})

If LOGGING is already defined, merge the above into the existing Dictionary.

Celery Logging

Add the following to our default Celery configuration:

import logging
logging.basicConfig(level='DEBUG')

Custom Instrumentation logging

If you’ve custom Scout instrumentation, add the following to record the agent logs:

import logging
logging.basicConfig(level='DEBUG')