-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow mix extra through ini file #5
Comments
@lucienfregosi Becasue of import logging
import os
from logging.config import fileConfig
def set_single_logger_mix_extra():
fileConfig(os.path.join(os.path.dirname(__file__), 'logger_config.ini'))
logger_name = ''
# NOTICE: `logger_name` is `''` or `None` will return root logger, `'root'` will return a sub logger named root
logger = logging.getLogger(logger_name)
for handle in logger.handlers or []:
if isinstance(handle.formatter, JsonFormatter):
handle.formatter.mix_extra = True
logger.info('test file config', extra={'jobId': 123, 'flowId': 456})
def set_all_logger_mix_extra():
fileConfig(os.path.join(os.path.dirname(__file__), 'logger_config.ini'))
all_logger = [logging.root] + list(logging.root.manager.loggerDict.values())
for logger in all_logger:
for handle in logger.handlers or []:
if isinstance(handle.formatter, JsonFormatter):
handle.formatter.mix_extra = True
logger_name = ''
logger = logging.getLogger(logger_name)
logger.info('test file config', extra={'jobId': 123, 'flowId': 456}) https://github.com/MyColorfulDays/jsonformatter/blob/master/test/logger_config.ini Here is the |
You can actually instantiate the JSON logger like this and just pass your kwargs:
Just remember to use the keys defined by the JsonFormatter Class. 'format' -> 'fmt' |
@FlorianHall import logging
import logging.config
DICT_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'console_jsonformatter': {
'()': 'jsonformatter.JsonFormatter', # https://docs.python.org/3/library/logging.config.html#user-defined-objects
'fmt': """{
"Name": "name",
"Levelno": "levelno",
"Levelname": "levelname",
"Pathname": "pathname",
"Filename": "filename",
"Module": "module",
"Lineno": "lineno",
"FuncName": "funcName",
"Created": "created",
"Asctime": "asctime",
"Msecs": "msecs",
"RelativeCreated": "relativeCreated",
"Thread": "thread",
"ThreadName": "threadName",
"Process": "process",
"Message": "message",
"status": "status"
}""",
'record_custom_attrs': {
'status': lambda **record_attrs: 'failed' if record_attrs['levelname'] in ['ERROR', 'CRITICAL'] else 'success'
},
'mix_extra': True
},
},
'handlers': {
'console_handler': {
'level': 'INFO',
'formatter': 'console_jsonformatter',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'': { # root logger
'handlers': ['console_handler'],
'level': 'INFO',
'propagate': False
},
}
}
logging.config.dictConfig(DICT_CONFIG)
logging.info("hello, %s", 'jsonformatter') |
Thank you very much for your contribution in log json I want to use dictConfig to read the yaml/json format configuration file, and want to add record_custom_attrs, just like the demo file above uses DICT_CONFIG, but I don’t know how to use the yaml/json type configuration file while supporting record_custom_attrs. Many thanks |
Hi, @xiang1030 |
ok I understand.I like your project very much |
Hi, thanks a lot for this really helpful formatter. Using it with Django and having mix_extra=True, causes error in the default response/request logging that Django does. The request being a WSGIRequest type / socket.socket type, causes TypeError 'is not JSON serializable' error. Django itself sends params like self.request and server_time under 'extra' keyword param to logger in files django/core/servers/basehttp.py and django/utils/log.py. Is there any way to avoid this error. |
I have made a fix for the JSON serializable issue by using a child class , which also enables the Django user to have mix_extra enabled directly via a custom formatter class (wrapper on your library class). Let me know if I should share a PR for that. |
Hi, @antariksh-nykaa |
@antariksh-nykaa |
Hi,I try to use this command “$ pip install jsonformatter --upgrade” to upgrade, |
Hi @xiang1030 |
Hi thanks for your work on that log formatter.
I want to use it through an ini file but I also want to add some extra param like
root.info('test file config', extra={'jobId': jobId, 'flowId': flowId})
I didn't find a way to specify inside the ini file this specific option
mix_extra=True
do you have an idea if it's possible ? or do I have to declare the logger inside the code for using extra option ?Many thanks
The text was updated successfully, but these errors were encountered: