diff --git a/README.md b/README.md index 71bab4b..d064a26 100644 --- a/README.md +++ b/README.md @@ -69,22 +69,31 @@ format={"additional_field": "value"} Please note, that you have to configure those parameters by this exact order. i.e. you cannot set Debug to true, without configuring all of the previous parameters as well. +#### Serverless platforms + +If you're using a serverless function, you'll need to import and add the LogzioFlusher annotation before your sender function. To do this, in the code sample below, uncomment the `import` statement and the `@LogzioFlusher(logger)` annotation line. + #### Code Example ```python import logging import logging.config +# If you're using a serverless function, uncomment. +# from logzio.flusher import LogzioFlusher # Say i have saved my configuration under ./myconf.conf logging.config.fileConfig('myconf.conf') logger = logging.getLogger('superAwesomeLogzioLogger') -logger.info('Test log') -logger.warn('Warning') +# If you're using a serverless function, uncomment. +# @LogzioFlusher(logger) +def my_func(): + logger.info('Test log') + logger.warn('Warning') -try: - 1/0 -except: - logger.exception("Supporting exceptions too!") + try: + 1/0 + except: + logger.exception("Supporting exceptions too!") ``` #### Extra Fields @@ -98,6 +107,7 @@ For example: logger.info('Warning', extra={'extra_key':'extra_value'}) ``` + ## Django configuration ``` LOGGING = { @@ -142,6 +152,7 @@ LOGGING = { } ``` + *Change* - token - Your logzio token - url - Logz.io Listener address @@ -151,6 +162,8 @@ LOGGING = { - appname - Your django app ## Release Notes +- 2.0.14 + - Added flusher decorator for serverless platforms(@mcmasty) - 2.0.13 - Add support for `pypy` and `pypy3`(@rudaporto-olx) - Add timeout for requests.post() (@oseemann) diff --git a/logzio/flusher.py b/logzio/flusher.py new file mode 100644 index 0000000..3b1c5b6 --- /dev/null +++ b/logzio/flusher.py @@ -0,0 +1,20 @@ +import functools +import logging + + +class LogzioFlusher(logging.Logger): + + def __init__(self, logger): + self.logger = logger + + def __call__(self, function): + @functools.wraps(function) + def wrapper(*args, **kwargs): + try: + return function(*args, **kwargs) + except Exception as e: + self.logger.exception('call failed: {}'.format(e)) + raise + finally: + [h.flush() for h in self.logger.root.handlers] + return wrapper