Logz.io Python Handler sends logs in bulk over HTTPS to Logz.io. Logs are grouped into bulks based on their size.

If the main thread quits, the handler tries to consume the remaining logs and then exits. If the handler can’t send the remaining logs, they’re written to the local file system for later retrieval.

Set up Logz.io Python Handler

Supported versions: Python 3.5 or newer.

Add the dependency to your project

Navigate to your project’s folder in the command line, and run this command to install the dependency.

pip install logzio-python-handler
Configure Logz.io Python Handler for a standard Python project

Use the samples in the code block below as a starting point, and replace the sample with a configuration that matches your needs.

For a complete list of options, see the configuration parameters below the code block.👇

[handlers]
keys=LogzioHandler

[handler_LogzioHandler]
class=logzio.handler.LogzioHandler
formatter=logzioFormat

# Parameters must be set in order. Replace these parameters with your configuration.
args=('<<SHIPPING-TOKEN>>', '<<LOG-TYPE>>', <<TIMEOUT>>, '<<LISTENER-HOST>>:8071', <<DEBUG-FLAG>>)

[formatters]
keys=logzioFormat

[loggers]
keys=root

[logger_root]
handlers=LogzioHandler
level=INFO

[formatter_logzioFormat]
format={"additional_field": "value"}
Dict Config

This is an alternative configuration option recommended if you are using Python 3.8.

See Python’s documentation regarding the logging.config.dictConfig method.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'logzioFormat': {
            'format': '{"additional_field": "value"}',
            'validate': False
        }
    },
    'handlers': {
        'logzio': {
            'class': 'logzio.handler.LogzioHandler',
            'level': 'INFO',
            'formatter': 'logzioFormat',
            'token': '<<SHIPPING-TOKEN>>',
            'logs_drain_timeout': 5,
            'url': 'https://<<LISTENER-HOST>>:8071'
        }
    },
    'loggers': {
        '': {
            'level': 'DEBUG',
            'handlers': ['logzio'],
            'propogate': True
        }
    }
}
Replace the placeholders
  • Replace <<SHIPPING-TOKEN>> with the token of the account you want to ship to.
  • Replace <<LISTENER-HOST>> with your region’s listener host (for example, listener.logz.io). For more information on finding your account’s region, see Account region.
Parameters

The order of the arguments is important. Arguments must be configured in the order shown here. For example, to set debug-flag to True, you need to set every argument that comes before it.

Parameter Description
account-token Your Logz.io account token.
Replace <<SHIPPING-TOKEN>> with the token of the account you want to ship to.
log-type python The log type, shipped as type field. Used by Logz.io for consistent parsing. Can’t contain spaces.
timeout 3 Time to wait between log draining attempts, in seconds.
listener-url https://listener.logz.io:8071 Listener URL and port.
Replace <<LISTENER-HOST>> with your region’s listener host (for example, listener.logz.io). For more information on finding your account’s region, see Account region.
debug-flag False Debug flag. To print debug messages to stdout, True. Otherwise, False.
backup-logs True If set to False, disables the local backup of logs in case of failure.
network-timeout 10 Timeout in seconds, int or float, for sending the logs to Logz.io.

Serverless platforms

If you’re using a serverless function, you’ll need to import and add the LogzioFlusher annotation before your sender function. To do this, in the code sample below, uncomment the import statement and the @LogzioFlusher(logger) annotation line.

Code Example

import logging
import logging.config
# If you're using a serverless function, uncomment.
# from logzio.flusher import LogzioFlusher

# Say i have saved my configuration in a dictionary form in variable named myconf
logging.config.dictConfig(myconf)
logger = logging.getLogger('superAwesomeLogzioLogger')

# If you're using a serverless function, uncomment.
# @LogzioFlusher(logger)
def my_func():
    logger.info('Test log')
    logger.warn('Warning')

    try:
        1/0
    except:
        logger.exception("Supporting exceptions too!")

To add dynamic metadata to your logger other than the constant metadata from the formatter, you can use the extra parameter. Key-value pairs passed in extra are shown as new fields in Logz.io. You can’t override default fields from the python logger, such as lineno or thread.

logger.info('Warning', extra={'extra_key':'extra_value'})