Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aucampia committed Dec 12, 2021
1 parent 90d55d2 commit ded20fd
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 130 deletions.
5 changes: 5 additions & 0 deletions src/structlog/_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from io import StringIO
from types import FrameType
from typing import List, Optional, Tuple
import inspect

from .types import ExcInfo

Expand Down Expand Up @@ -44,12 +45,16 @@ def _find_first_app_frame_and_name(
ignores = ["structlog"] + (additional_ignores or [])
f = sys._getframe()
name = f.f_globals.get("__name__") or "?"
# sys.stderr.write(f"name={name}\n")
while any(tuple(name.startswith(i) for i in ignores)):
# sys.stderr.write(f"name={name}\n")
if f.f_back is None:
name = "?"
break
f = f.f_back
name = f.f_globals.get("__name__") or "?"
# sys.stderr.write(f"f = {f}, name={name}\n")
# traceback.print_stack()
return f, name


Expand Down
32 changes: 29 additions & 3 deletions src/structlog/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,22 @@ class CallsiteParameterAdder:
The keys used for various callsite parameters in the event dictionary are
the string values of `CallsiteParameter` members.
:param parameters: A collection of `CallsiteParameter` values that should be added to the event dictionary.
:param parameters: A collection of `CallsiteParameter` values that should be
added to the event dictionary.
:param additional_ignores: Additional names with which the first frame must
not start.
.. warning:: This processor should not be used in the processor parameter of
a `structlog.stdlib.ProcessorFormatter`, as this will result in the
processor running multiple times for the same event dictionary and
adding invalid values.
Instead, this function should be used in the ``foreign_pre_chain``
parameter of `structlog.stdlib.ProcessorFormatter` and in the processors
parameter of `structlog.configure`.
.. versionadded:: 21.5.0
"""

Expand Down Expand Up @@ -635,16 +646,31 @@ def __init__(
def __call__(
self, logger: logging.Logger, name: str, event_dict: EventDict
) -> EventDict:
sys.stderr.write(
f"CallsiteParameterAdder.__call__: event_dict = {event_dict}\n"
)
import traceback

traceback.print_stack()
record: Optional[logging.LogRecord] = event_dict.get("_record")

if record is not None:
for parameter in self._parameters:
event_dict[parameter.value] = record.__dict__[parameter.value]
from_structlog: Optional[bool] = event_dict.get("_from_structlog")
if not from_structlog:
for parameter in self._parameters:
event_dict[parameter.value] = record.__dict__[
parameter.value
]
else:
frame, module = _find_first_app_frame_and_name(
additional_ignores=self._additional_ignores
)
# sys.stderr.write("_find_first_app_frame_and_name: frame = {}\n".format(frame))
# sys.stderr.write("_find_first_app_frame_and_name: module = {}\n".format(module))
frame_info = inspect.getframeinfo(frame)
# sys.stderr.write("frame_info = {}\n".format(frame_info))
for parameter, handler in self._active_handlers:
handler(module, frame_info)
event_dict[parameter.value] = handler(module, frame_info)
sys.stderr.write("event_dict = {}\n".format(event_dict))
return event_dict
Loading

0 comments on commit ded20fd

Please sign in to comment.