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}}

Usage with decorator

With dict config

import os
import logging
import logging.config
import daiolog 

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
        },
    } 
}

@daiolog.entrypoint(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__':
    main()


# {"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}}

With file config

import os
import logging
import logging.config
import daiolog 

@daiolog.entrypoint('./logging.conf')
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__':
    main()


# {"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}}

With function

import os
import logging
import logging.config
import daiolog 

def get_logging_config():
    return os.environ.get('LOGGING_FILE_CONFIG', './logging.conf')

@daiolog.entrypoint(get_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__':
    main()


# {"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}}

Release Notes

1.1.0

  • Add entrypoint function decorator(daiolog.entrypoint) for config logging and start/stop QueueListener 1.1.1
  • Added compatibility for python 3.12

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.1.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

daiolog-1.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file daiolog-1.1.1.tar.gz.

File metadata

  • Download URL: daiolog-1.1.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.12 Darwin/23.5.0

File hashes

Hashes for daiolog-1.1.1.tar.gz
Algorithm Hash digest
SHA256 e50080bdb22961f5eb39e7b8370224bf30fa5204fcc9f43178e08f65945882af
MD5 e71f5ecb1c7d9ac3073a21ca6d998ecf
BLAKE2b-256 9b959bab87470b22a2737afc22ede955391ed6c1362eca30b8d6fac6e054df4a

See more details on using hashes here.

File details

Details for the file daiolog-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: daiolog-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.12 Darwin/23.5.0

File hashes

Hashes for daiolog-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e532e832ac61dcf0e3333ae87e449c976743eee2e5a4a34fbba49c6771d2515
MD5 d542b08c42e2051f53ffa79b9f92c51f
BLAKE2b-256 400adb08be710eb922409e837ceb92777d19df8828a29a13bae0310786b82652

See more details on using hashes here.

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