Skip to content

Instantly share code, notes, and snippets.

@unpluggedcoder
Last active May 2, 2018 17:47
Show Gist options
  • Save unpluggedcoder/994ae421e8b7d2fb86884331752cab9c to your computer and use it in GitHub Desktop.
Save unpluggedcoder/994ae421e8b7d2fb86884331752cab9c to your computer and use it in GitHub Desktop.
[Python Asynclog Examples] Examples of using asynclog module #python #asynchronous #logging #threading #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')
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')
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment