Skip to main content

Asynchronous log for python logging.

Project description

asynclog

Build Status Coverage Status

asynclog provide the asynchronous way for python logging. Leave the logging I/O(especially the network I/O when we want to logging to a network endpoint) to the asynchronous thread or asynchronous task provided by celery .

Requirements

  • Python 3.5+

Install

pip install asynclog

Usage

  • Config from dict
log_cfg = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(asctime)s \n %(levelname)s \n %(message)s'
        },
    },
    'handlers': {
        'async_handler': {
            'level': 'INFO',
            'formatter': 'simple',
            'class': 'asynclog.AsyncLogDispatcher',
            'func': '[Dot_Path_To_Your_Func]',
        }
    },
    'loggers': {
        'asynclogger': {
            'handlers': ['async_handler', ],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}

logging.config.dictConfig(log_cfg)
logger = logging.getLogger('asynclogger')
logger.info('Test asynclog')
  • Using thread
import logging
import time

from asynclog import AsyncLogDispatcher


def write_log(msg):
    # Do write stuff, such as write log msg into network.
    # ...
    time.sleep(0.5)


logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = AsyncLogDispatcher(write_log)
handler.setLevel(logging.INFO)
logger.addHandler(handler)

logger.info('Test Log')
  • Using Celery
from celery import shared_task

@shared_task
def write_task(msg):
    # Write log in Network IO
    print(msg)

celery_handler = AsyncLogDispatcher(write_task, use_thread=False, use_celery=True)
celery_handler.setLevel(logging.INFO)
logger.addHandler(celery_handler)

logger.info('Test Log')

Test

python3 -m unittest
......
----------------------------------------------------------------------
Ran 6 tests in 0.007s

OK

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

asynclog-0.1.6.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asynclog-0.1.6-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file asynclog-0.1.6.tar.gz.

File metadata

  • Download URL: asynclog-0.1.6.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for asynclog-0.1.6.tar.gz
Algorithm Hash digest
SHA256 d28b55b1dfb983caf4069c0eed12b2e0af3ed4fd29331ad61896d04f758eae6a
MD5 ed2c35e920f4b0ab2ed0dd385e32a064
BLAKE2b-256 71ba08d0d8f380f568b2db7bf543c44974e180f1bf01071a071a1b3834ceb1ce

See more details on using hashes here.

File details

Details for the file asynclog-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for asynclog-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f38ea7d4787132790e4e909796e6c3b170d2752f4e5f82c2b4e19eb897b139ad
MD5 fa28daf987399b462153252555282c11
BLAKE2b-256 bb86441813613b9c5f53d16c2ee2bfd727f0e8f4977e711b06fd1b1473f582fe

See more details on using hashes here.

Supported by

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