├── async_logging_handler └── __init__.py ├── readme.md └── setup.py /async_logging_handler/__init__.py: -------------------------------------------------------------------------------- 1 | from logging import FileHandler 2 | from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler 3 | from threading import Thread 4 | from Queue import Queue 5 | 6 | 7 | class AsyncHandlerMixin(object): 8 | def __init__(self, *args, **kwargs): 9 | super(AsyncHandlerMixin, self).__init__(*args, **kwargs) 10 | self.__queue = Queue() 11 | self.__thread = Thread(target=self.__loop) 12 | self.__thread.daemon = True 13 | self.__thread.start() 14 | 15 | def emit(self, record): 16 | self.__queue.put(record) 17 | 18 | def __loop(self): 19 | while True: 20 | record = self.__queue.get() 21 | try: 22 | super(AsyncHandlerMixin, self).emit(record) 23 | except: 24 | pass 25 | 26 | 27 | class AsyncFileHandler(AsyncHandlerMixin, FileHandler): 28 | pass 29 | 30 | 31 | class AsyncRotatingFileHandler(AsyncHandlerMixin, RotatingFileHandler): 32 | pass 33 | 34 | 35 | class AsyncTimedRotatingFileHandler(AsyncHandlerMixin, TimedRotatingFileHandler): 36 | pass 37 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | **async_logging_handler** 2 | 3 | Super-simple extensions for standard Python's ``logging.FileHandler``, ``logging.handlers.RotatingFileHandler`` and ``logging.handlers.TimedRotatingFileHandler`` 4 | with asynchronous writing to file (utilizing standard ``Queue.Queue``). 5 | 6 | Useful when using logging in programs, where long-time blocking for logging to file is not allowed. 7 | 8 | **Installation** 9 | 10 | ```bash 11 | pip install git+https://github.com/CopterExpress/python-async-logging-handler.git 12 | ``` 13 | 14 | **Using** 15 | 16 | ```python 17 | from async_logging_handler import AsyncFileHandler 18 | 19 | # ... 20 | 21 | async_handler = AsyncFileHandler(filename) 22 | some_logger.addHandler(async_handler) 23 | ``` 24 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | 3 | setup(name='async_logging_handler', 4 | version='1.0', 5 | description='Asynchronous file handler for python logging', 6 | author='Oleg Kalachev', 7 | author_email='okalachev@gmail.com', 8 | packages=['async_logging_handler'], 9 | ) 10 | --------------------------------------------------------------------------------