sighalt / logdecorator

Move logging code out of your business logic with python decorators.
MIT License
73 stars 12 forks source link

Specify handler for auto-created logger #13

Closed kitmonisit closed 2 years ago

kitmonisit commented 3 years ago

From https://github.com/sighalt/logdecorator#documentation

An alternative logger object. If no logger is given logdecorator creates a logger object with the name of the module the decorated function is in (decorated_function.__module__) Default: Creates a new logger with the name decorated_function.__module__

I find that the auto-created logger with the name of the decorated function is very useful. It might help to be able to specify a handler for this auto-created logger, e.g.

# app.py
import logging
logging.basicConfig(level=logging.INFO)

# Set up pyzmq log handler
from zmq.log.handlers import PUBHandler
ctx = zmq.Context.instance()
publisher = ctx.socket(zmq.PUB)
publisher.bind('ipc://log')
zmq_log_handler = PUBHandler(publisher)

# Set up logdecorator
from logdecorator import log_on_start

@log_on_start(logging.INFO, "Starting greeter", handler=zmq_log_handler)
def greeter():
    print("Hello world")

greeter()

Then, on a separate process, I would first run the following to listen for a log event:

$ python -m zmq.log ipc://log

Again, on a separate process, I run the main script:

$ python app.py

References

sighalt commented 3 years ago

@kitmonisit This looks like a nice shortcut. I will have a look into this.

sighalt commented 2 years ago

Implemented and uploaded to PyPI. This feature is available from version 2.4.