madzak / python-json-logger

Json Formatter for the standard python logger
BSD 2-Clause "Simplified" License
1.74k stars 231 forks source link

extra can not hava keys like name #181

Open thenumberouscode opened 1 year ago

thenumberouscode commented 1 year ago
import logging
from pythonjsonlogger import jsonlogger
import traceback

logger = logging.getLogger("myname")

logger.setLevel(logging.INFO) 
logHandler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter()
logHandler.setFormatter(formatter)
logger.addHandler(logHandler)

try:
    logger.info("test", extra={"name": "my_test_name"})
except Exception as e:
    print(traceback.format_exc())
    print(e)

above code throws a exception which tell us that name has already been existed in LogRecord

output

Traceback (most recent call last):
  File "json_logger.py", line 14, in <module>
    logger.info("test", extra={"name": "my_test_name"})
  File "/data/zhouzhao.zyl/repos/meta_service/venv/lib/python3.7/logging/__init__.py", line 1383, in info
    self._log(INFO, msg, args, **kwargs)
  File "/data/zhouzhao.zyl/repos/meta_service/venv/lib/python3.7/logging/__init__.py", line 1518, in _log
    exc_info, func, extra, sinfo)
  File "/data/zhouzhao.zyl/repos/meta_service/venv/lib/python3.7/logging/__init__.py", line 1492, in makeRecord
    raise KeyError("Attempt to overwrite %r in LogRecord" % key)
KeyError: "Attempt to overwrite 'name' in LogRecord"

Reading the related source code of logging, the name key has been set as myname as the logger name. so I am seriously curious about that if the way of using extra in my code is correct?

python json logger version python-json-logger 2.0.7 l

NewSouthMjos commented 1 year ago

As you said,

the name key has been set as myname as the logger name.

and it is ok behavior. I think the following names are reserved:

args
asctime
created
exc_info
filename
funcName
levelname
levelno
lineno
message
module
msecs
msg
name
pathname
process
processName
relativeCreated
stack_info
thread
threadName

https://docs.python.org/3/library/logging.html#logrecord-attributes

So, just pick another name for your extra!

thenumberouscode commented 1 year ago

emmm, I think it's inconvenient for users to use json formatter when there are so many reserve fields. As a formatter, it should format everything that user inputs.

nhairs commented 8 months ago

Per the stack trace you provided, this error is being generated by logging and not python-json-logger.

Despite the package name, python-json-logger is actually a logging.Formatter and is only called from a logging.Handler (which in turn must be attached to a logging.Logger).

The reserved names are enforced by the default logging.Logger.makeRecord implementation. If you'd like to be able to override any name using extra= you'll need to create your own logging.Logger subclass and override this method.