-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Added AddCallingClassPath
processor
#568
Conversation
Sample code:
import structlog
logger = structlog.get_logger()
async def async_logging(l):
await logger.ainfo(l)
class SubModuleClassBasedCall:
def class_sync_logging(l):
logger.info(l)
async def class_async_logging(l):
await logger.adebug(l)
import asyncio
import structlog
from foo import sub_module
from foo.sub_module import SubModuleClassBasedCall
structlog.configure(
processors=[
structlog.processors.AddCallingClassPath(levels={}),
structlog.processors.JSONRenderer(),
],
context_class=dict,
cache_logger_on_first_use=True,
)
logger = structlog.get_logger()
def sync_logging(l):
logger.debug(l)
class ClassBasedCall:
async def class_async_logging(l):
await logger.adebug(l)
if __name__ == "__main__":
logger.info("hello from __main__ direct")
sync_logging("hello from __main__ via function")
sub_module.SubModuleClassBasedCall.class_sync_logging(
"hello from sub_module.class sync logging"
)
SubModuleClassBasedCall.class_sync_logging(
"hello from sub_module imported class sync logging"
)
loop = asyncio.get_event_loop()
loop.run_until_complete(
ClassBasedCall.class_async_logging(
"hello from __main__ class async logging"
)
)
loop.run_until_complete(
sub_module.async_logging(
"hello from sub_module function async logging"
)
)
loop.run_until_complete(
SubModuleClassBasedCall.class_async_logging(
"hello from sub_module imported class async logging"
)
)
loop.close() $ python demo.py
{"event": "hello from __main__ direct", "class_path": "__main__.<module>"}
{"event": "hello from __main__ via function", "class_path": "__main__.sync_logging"}
{"event": "hello from sub_module.class sync logging", "class_path": "foo.sub_module.SubModuleClassBasedCall.class_sync_logging"}
{"event": "hello from sub_module imported class sync logging", "class_path": "foo.sub_module.SubModuleClassBasedCall.class_sync_logging"}
{"event": "hello from __main__ class async logging", "class_path": "__main__.ClassBasedCall.class_async_logging"}
{"event": "hello from sub_module function async logging", "class_path": "foo.sub_module.async_logging"}
{"event": "hello from sub_module imported class async logging", "class_path": "foo.sub_module.SubModuleClassBasedCall.class_async_logging"} |
@hynek -- I can't figure out a better way to get around that partial branch failure from the |
I moved to a queue for iteration to get rid of the 'break' to make it pass |
Just some passing nitpicky feedback - I'm not sure I like "class path", considering that it's not reliant on classes. Probably more accurate to call it a namespace/module path or something like that? |
Sounds like reasonable feedback -- went based on the body of #386. Namespace seems clearer and more precise to me |
Branch renamed. Replacement PR #570 |
Summary
Added a
class_path
event entry processor asAddCallingClassPath
(#386, #492).Takes an optional
set
orlist
to limit which logging levels to add this at, as lookups potentially can add overhead (though should run in N(1)).Pull Request Check List
Do not open pull requests from your
main
branch – use a separate branch!There's a ton of footguns waiting if you don't heed this warning. You can still go back to your project, create a branch from your main branch, push it, and open the pull request from the new branch.
This is not a pre-requisite for your your pull request to be accepted, but you have been warned.
Added tests for changed code.
New APIs are added to our typing tests in
api.py
.Updated documentation for changed code.
New functions/classes have to be added to
docs/api.rst
by hand.Changed/added classes/methods/functions have appropriate
versionadded
,versionchanged
, ordeprecated
directives.The next version is the second number in the current release + 1. The first number represents the current year. So if the current version on PyPI is 23.1.0, the next version is gonna be 23.2.0. If the next version is the first in the new year, it'll be 24.1.0.
Documentation in
.rst
and.md
files is written using semantic newlines.Changes (and possible deprecations) are documented in the changelog.
Consider granting push permissions to the PR branch, so maintainers can fix minor issues themselves without pestering you.