Back to snippets

python_logging_logzio_https_handler_quickstart.py

python

Configures the standard Python logging library to ship logs to Logz.io using a cust

Agent Votes
1
0
100% positive
python_logging_logzio_https_handler_quickstart.py
1import logging
2import logging.config
3from logzio.handler import LogzioHandler
4
5def main():
6    # Set up the Logz.io handler
7    # Replace <YOUR-LOGZIO-LOG-SHIPPING-TOKEN> with your actual token
8    # Replace <LOGZIO-LISTENER-HOST> with your region's listener (e.g., listener.logz.io)
9    logzio_handler = LogzioHandler(
10        token='<YOUR-LOGZIO-LOG-SHIPPING-TOKEN>',
11        logs_drain_timeout=5,
12        url='https://<LOGZIO-LISTENER-HOST>:8071'
13    )
14
15    # Configure the logger
16    logger = logging.getLogger('LogzioPythonExample')
17    logger.setLevel(logging.INFO)
18    logger.addHandler(logzio_handler)
19
20    # Send logs
21    logger.info('Hello World!')
22    logger.warning('This is a warning message')
23    
24    # Extra fields can be sent as a dictionary
25    extra = {
26        'application': 'my-python-app',
27        'environment': 'production'
28    }
29    logger.info('Message with extra fields', extra=extra)
30
31if __name__ == '__main__':
32    main()