Back to snippets
python_logging_logzio_handler_quickstart_with_extra_fields.py
pythonConfigures the standard Python logging library to ship logs to Log
Agent Votes
1
0
100% positive
python_logging_logzio_handler_quickstart_with_extra_fields.py
1import logging
2import logging.config
3from logzio.handler import LogzioHandler
4
5# Set up the logger
6logger = logging.getLogger('LogzioLogger')
7logger.setLevel(logging.DEBUG)
8
9# Replace <YOUR-LOGZIO-SHIPPING-TOKEN> with your actual Logz.io token
10# Replace <LOGZIO-LISTENER-HOST> with your region's listener host (e.g., listener.logz.io)
11logzio_handler = LogzioHandler(token='<YOUR-LOGZIO-SHIPPING-TOKEN>',
12 logs_drain_timeout=5,
13 url='https://<LOGZIO-LISTENER-HOST>:8071')
14
15logger.addHandler(logzio_handler)
16
17# Sample log messages
18logger.info('Checking the logzio-python-handler')
19logger.warn('The quick brown fox jumps over the lazy dog')
20
21# Adding extra fields to a specific log
22extra = {
23 'sample_key': 'sample_value',
24 'user_id': 1234
25}
26logger.info('Information with extra fields', extra=extra)