-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Add callsite info to event dictionary #378
Comments
I created a draft with code and tests, still need to add docs and other things, but would like some feedback before I proceed much further. |
Wow you're putting me really to shame with your industriousness (and generosity – thank you so much!)! Since – if I'm not mistaken – all this data is available via logging extras, this is about making this data independent of stdlib logging, correct? You're entirely correct, that this has to be configurable and it would be great if we could get both PRs in, because that would allow me to cut a 21.5 before new year and get the logfmt out to the people. |
Yes, the specific use case I have is that I want:
To make this work I configured structlog as can be seen here: https://gitlab.com/aucampia/try/python-structlog/-/blob/f1cac2a89d17f58d5ec14a3db6930414d1d3b8b2/src/aucampia/trx/structlog/cli.py#L175 def setup_structlog_formatter() -> None:
shared_processors: List[Processor] = []
structlog.configure(
processors=shared_processors
+ [
structlog.stdlib.filter_by_level,
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
formatter = structlog.stdlib.ProcessorFormatter(
foreign_pre_chain=[add_extra, *shared_processors],
keep_stack_info=True,
processors=[
CallsiteInfoAdder(
{
CallsiteParameter.FUNC_NAME,
CallsiteParameter.FILENAME,
CallsiteParameter.LINENO,
CallsiteParameter.THREAD,
}
),
structlog.stdlib.add_log_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
structlog.processors.JSONRenderer(),
],
)
log_handler = logging.StreamHandler(stream=sys.stderr)
log_handler.setFormatter(formatter)
root_logger = logging.getLogger("")
root_logger.propagate = True
root_logger.setLevel(os.environ.get("PYLOGGING_LEVEL", logging.INFO))
root_logger.addHandler(log_handler) In summary:
This in practice works correctly, messages from both Example output messages with the above config: $ pipx run --no-cache --spec git+https://gitlab.com/aucampia/try/python-structlog@f1cac2a89d17f58d5ec14a3db6930414d1d3b8b2 aucampia.trx.structlog.py -vvv run -s structlog_formatter -g stdlib_extra -g structlog 2>&1
{"setup": ["structlog_formatter"], "genlog": ["stdlib_extra", "structlog"]}
{"event": "entry", "foo": "bar", "what": "stdlib_extra", "thread": 139763534812992, "filename": "cli.py", "funcName": "genlog_stdlib_extra", "lineno": 220, "level": "info", "logger": "aucampia.trx.structlog.cli.genlog_stdlib_extra", "timestamp": "2021-12-08T08:42:05.179313Z"}
{"event": "fmt is a thing", "thread": 139763534812992, "filename": "cli.py", "funcName": "genlog_stdlib_extra", "lineno": 221, "level": "info", "logger": "aucampia.trx.structlog.cli.genlog_stdlib_extra", "timestamp": "2021-12-08T08:42:05.179427Z"}
{"event": "caught", "foo": "bar", "what": "stdlib_extra", "thread": 139763534812992, "filename": "cli.py", "funcName": "genlog_stdlib_extra", "lineno": 225, "level": "info", "logger": "aucampia.trx.structlog.cli.genlog_stdlib_extra", "timestamp": "2021-12-08T08:42:05.179512Z", "exception": "Traceback (most recent call last):\n File \"/home/iwana/.local/pipx/.cache/78617db6b8f5638/lib64/python3.9/site-packages/aucampia/trx/structlog/cli.py\", line 223, in genlog_stdlib_extra\n raise RuntimeError(\"is here ...\")\nRuntimeError: is here ..."}
{"foo": "bar", "what": "stdlib", "event": "entry", "thread": 139763534812992, "filename": "cli.py", "funcName": "genlog_structlog", "lineno": 232, "level": "info", "logger": "aucampia.trx.structlog.cli.genlog_structlog", "timestamp": "2021-12-08T08:42:05.179955Z"}
{"event": "fmt is a thing", "thread": 139763534812992, "filename": "cli.py", "funcName": "genlog_structlog", "lineno": 233, "level": "info", "logger": "aucampia.trx.structlog.cli.genlog_structlog", "timestamp": "2021-12-08T08:42:05.180045Z"}
{"foo": "bar", "what": "stdlib", "event": "caught", "thread": 139763534812992, "filename": "cli.py", "funcName": "genlog_structlog", "lineno": 237, "level": "info", "logger": "aucampia.trx.structlog.cli.genlog_structlog", "timestamp": "2021-12-08T08:42:05.180131Z", "exception": "Traceback (most recent call last):\n File \"/home/iwana/.local/pipx/.cache/78617db6b8f5638/lib64/python3.9/site-packages/aucampia/trx/structlog/cli.py\", line 235, in genlog_structlog\n raise RuntimeError(\"is here ...\")\nRuntimeError: is here ..."}
|
I would like to have the following values in the event dictionary (taken from
logging
module docs):These values can be added with custom processors, but it would be nice to add these to structlog to reduce the amount of code that has to be copied around.
If you are open to it, I would like to add the following processor to
structlog
:Another alternative is a simple non configurable process like this:
However the result in this case will be very noisy and may include things people do not care for.
The text was updated successfully, but these errors were encountered: