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

Added AddCallingClassPath processor #568

Closed
wants to merge 9 commits into from

Conversation

pahrohfit
Copy link
Contributor

@pahrohfit pahrohfit commented Oct 30, 2023

Summary

Added a class_path event entry processor as AddCallingClassPath (#386, #492).

Takes an optional set or list 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.

    • The CI fails with less than 100% coverage.
  • 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, or deprecated 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.

@pahrohfit
Copy link
Contributor Author

Sample code:

foo/sub_module.py

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)

demo.py

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"}

@pahrohfit
Copy link
Contributor Author

@hynek -- I can't figure out a better way to get around that partial branch failure from the break statement. Thoughts on what I'm missing?

@pahrohfit
Copy link
Contributor Author

@hynek -- I can't figure out a better way to get around that partial branch failure from the break statement. Thoughts on what I'm missing?

I moved to a queue for iteration to get rid of the 'break' to make it pass

@macintacos
Copy link

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?

@pahrohfit
Copy link
Contributor Author

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

@pahrohfit pahrohfit closed this Nov 2, 2023
@pahrohfit pahrohfit deleted the classpath_processor branch November 2, 2023 01:28
@pahrohfit
Copy link
Contributor Author

Branch renamed. Replacement PR #570

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

Successfully merging this pull request may close these issues.

2 participants