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

logdirector's "Dirty Trick" just bit me. #73

Open
freckletonj opened this issue Jul 2, 2023 · 1 comment
Open

logdirector's "Dirty Trick" just bit me. #73

freckletonj opened this issue Jul 2, 2023 · 1 comment

Comments

@freckletonj
Copy link

I was struggling to configure logging for my library that uses Thespian, until I found it was because of this function, I believe:

# Dirty trick here to completely re-initialize logging in this

logging.basicConfig(...) fails (for instance, you can't change format=...) unless you use the new force=True, but I'm not convinced that doesn't break something Thespian depends on, and I don't exactly understand what this hack is doing, nor why it's necessary.

I just wanted to bubble this up to your awareness.

@freckletonj freckletonj changed the title logdirector's "Dirty Hack" just bit me. logdirector's "Dirty Trick" just bit me. Jul 2, 2023
@freckletonj
Copy link
Author

For future reference, here is prior art within this library. I never stumbled across documentation on logging, but here are some references:

#43
https://github.com/thespianpy/Thespian/blob/master/examples/hellogoodbye.py (NOTE: logsetup.logcfg)

Here's how I solved it for my use case, of customizing the logging formatter:

logging.basicConfig(
    stream=sys.stdout,
    level=logging.DEBUG,  # Globally allow any level. Other loggers can narrow.
)
# Quiet the lib a little
logging.getLogger('Thespian').setLevel(logging.WARN)

def mk_logger(name, level):
    ''' A logger builder helper. The Thespian '''
    logger = logging.getLogger(name)
    handler = logging.StreamHandler()
    handler.setLevel(level)
    formatter = logging.Formatter('%(asctime)s %(levelname)s:%(name)s => %(message)s [%(pathname)s:%(lineno)d]')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger


# Tune this libs loggers as needed
my_logger = mk_logger('actor', logging.INFO)

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

No branches or pull requests

1 participant