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

simple config for scripts #665

Open
felixhummel opened this issue Oct 31, 2024 · 0 comments
Open

simple config for scripts #665

felixhummel opened this issue Oct 31, 2024 · 0 comments

Comments

@felixhummel
Copy link

use-case

Simple one-off scripts like this:

import structlog

log = structlog.get_logger()

def do_stuff():
    log.info('doing stuff')


if __name__ == '__main__':
    do_stuff()

problem

In simple scripts like the above, I would like to configure the log level based on a string.
Configuring a FilteringBoundLogger needs much code though:

import logging  
import os  
import structlog
  
_loglevel_name = os.environ.get('LOGLEVEL', 'WARNING').upper()  
_loglevel = getattr(logging, _loglevel_name)  
structlog.configure(  
    wrapper_class=structlog.make_filtering_bound_logger(_loglevel),  
)  
log = structlog.get_logger()  

options

expose NAME_TO_LEVEL

Exposing NAME_TO_LEVEL from _log_levels would reduce the above code to

import os  
import structlog
  
_loglevel_name = os.environ.get('LOGLEVEL', 'WARNING').upper()  
# Note: structlog.NAME_TO_LEVEL does not currently exist in structlog.
_loglevel = structlog.NAME_TO_LEVEL.get(_loglevel_name)
structlog.configure(  
    wrapper_class=structlog.make_filtering_bound_logger(_loglevel),  
)  
log = structlog.get_logger()  

add structlog.basic_config

This is what I would love to have:

import os  
import structlog
  
_loglevel_name = os.environ.get('LOGLEVEL', 'WARNING').upper()  
structlog.basic_config(level_name=_loglevel_name)
log = structlog.get_logger()  

remarks

Maybe there is a simpler way that I could not find in the docs.
Anyway, thanks for this great tool Hynek! :)

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