Browse Source

Add ExtraFieldsLogFilter

master
ralongit 12 months ago
parent
commit
0a9dadf313
2 changed files with 12 additions and 10 deletions
  1. +4
    -10
      README.md
  2. +8
    -0
      logzio/handler.py

+ 4
- 10
README.md View File

@ -168,15 +168,7 @@ logger.info('Warning', extra={'extra_key':'extra_value'})
The following additional code example offers the same functionlites that availavle with the extra parameter to add additional fields to logs. The difference is that it uses logging filters so it will add the fields that are declared key-values from the extra dictionary to every log that's generated after adding the fliter. You can keep updating the logs with additional filters.
```python
class ExtraFieldsLogFilter(logging.Filter):
def __init__(self, extra: dict, *args, **kwargs):
super().__init__(*args, **kwargs)
self.extra = extra
def filter(self, record):
record.__dict__.update(self.extra)
return True
from logzio.handler import ExtraFieldsLogFilter
def main():
logger.info("Test log") # Outputs: {"message":"Test log"}
@ -188,7 +180,9 @@ def main():
error_fields = {"err_msg":"Failed to run due to exception.","status_code":500}
logger.addFilter(ExtraFieldsLogFilter(error_fields))
logger.error("Error test log") # Outputs: {"message":"Error test log","foo":"bar","counter":1,"err_msg":"Failed to run due to exception.","status_code":500}
# If you'd like to remove filters from future logs using the logger.removeFilter option:
logger.removeFilter(ExtraFieldsLogFilter(error_fields))
logger.debug("Debug test log") # Outputs: {"message":"Debug test log","foo":"bar","counter":1}
```
## Django configuration


+ 8
- 0
logzio/handler.py View File

@ -8,7 +8,15 @@ import logging.handlers
from .sender import LogzioSender
from .exceptions import LogzioException
class ExtraFieldsLogFilter(logging.Filter):
def __init__(self, extra: dict, *args, **kwargs):
super().__init__(*args, **kwargs)
self.extra = extra
def filter(self, record):
record.__dict__.update(self.extra)
return True
class LogzioHandler(logging.Handler):


Loading…
Cancel
Save