Skip to main content

JSON logging in a separate thread for asyncio projects

Project description

Do asyncio logging

JSON logging in a separate thread for asyncio projects

Basic Usage

import os
import logging
import logging.config
from daiolog import QueueListener

LOGGING_CONFIG = { 
    'version': 1,
    'disable_existing_loggers': True,
    'handlers': { 
        'default': { 
            'level': 'INFO',
            'class': 'daiolog.QueueHandler',
        },
    },
    'loggers': { 
        '': {  # root logger
            'handlers': ['default'],
            'level': 'WARNING',
            'propagate': False
        },
        'my.packg': { 
            'handlers': ['default'],
            'level': 'INFO',
            'propagate': False
        },
    } 
}

logging.config.dictConfig(LOGGING_CONFIG)


def main():
    logger = logging.getLogger('my.packg')
    logger.info('Start main', extra={'pid': os.getpid()})
    ...
    logger.info('Finish main', extra={'pid': os.getpid()})

if __name__ == '__main__':
    QueueListener().start()
    main()
    QueueListener().stop()


# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.511+00:00", "message": "Start main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 35, "traceback": null, "extra": {"pid": 60720}}
# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.512+00:00", "message": "Finish main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 37, "traceback": null, "extra": {"pid": 60720}}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

daiolog-1.0.0.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distribution

daiolog-1.0.0-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page