django LOGGING - 如何按格式更改过滤器属性名称
django LOGGING - How to change filter attribute name by format
我有以下设置:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
},
'some_id': {
'()': 'my.special.filter'
},
},
'formatters': {
'json': {
'()': 'my.special.formatter',
'format': '%(thread)d\t%(message)s'
},
...
过滤器将“some_id”添加到日志中,因此输出如下所示:
{"thread": 140155333515008, "message": "some log msg", "some_id": "123456"}
我想修改附加属性“some_id”,因此输出如下所示:
{"thread": 140155333515008, "message": "some log msg", "some-id": "123456"}
I saw that it can be done with CustomAdapter 但我有很多“logger = logging.getLogger(...)”所以我不能使用它。
如何更改输出属性名称?
目前解决方案如下:
class my.special.formatter(JsonFormatter):
def __init__(self, *args, **kwargs):
super(my.special.formatter, self).__init__(*args, **kwargs)
def format(self, record):
if hasattr(record, 'some_id'):
record.__setattr__('some-id', record.some_id)
record.__delattr__('some_id')
return super(my.special.formatter, self).format(record)
但并不理想...
我有以下设置:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
},
'some_id': {
'()': 'my.special.filter'
},
},
'formatters': {
'json': {
'()': 'my.special.formatter',
'format': '%(thread)d\t%(message)s'
},
...
过滤器将“some_id”添加到日志中,因此输出如下所示:
{"thread": 140155333515008, "message": "some log msg", "some_id": "123456"}
我想修改附加属性“some_id”,因此输出如下所示:
{"thread": 140155333515008, "message": "some log msg", "some-id": "123456"}
I saw that it can be done with CustomAdapter 但我有很多“logger = logging.getLogger(...)”所以我不能使用它。
如何更改输出属性名称?
目前解决方案如下:
class my.special.formatter(JsonFormatter):
def __init__(self, *args, **kwargs):
super(my.special.formatter, self).__init__(*args, **kwargs)
def format(self, record):
if hasattr(record, 'some_id'):
record.__setattr__('some-id', record.some_id)
record.__delattr__('some_id')
return super(my.special.formatter, self).format(record)
但并不理想...