Skip to content
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

extra can not hava keys like name #181

Open
thenumberouscode opened this issue Sep 14, 2023 · 3 comments
Open

extra can not hava keys like name #181

thenumberouscode opened this issue Sep 14, 2023 · 3 comments

Comments

@thenumberouscode
Copy link

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
Copy link

NewSouthMjos commented Sep 14, 2023

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
Copy link
Author

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
Copy link

nhairs commented Jan 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants