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

Document clean way to add custom log levels #47

Open
heikoheiko opened this issue Feb 7, 2015 · 12 comments
Open

Document clean way to add custom log levels #47

heikoheiko opened this issue Feb 7, 2015 · 12 comments
Labels

Comments

@heikoheiko
Copy link

Is there a different way, other than accessing a private member like in below snippet?

structlog.stdlib.TRACE = TRACE = 5
structlog.stdlib._NAME_TO_LEVEL['trace'] = TRACE
logging.addLevelName(TRACE, "TRACE")

ethereum/pyethereum#202

@hynek
Copy link
Owner

hynek commented Feb 8, 2015

Do you have any API or wishes in mind?

@hynek hynek added the stdlib label Feb 8, 2015
@heikoheiko
Copy link
Author

no not really. maybe something like .addLevelName( int_level, str_name)

@hynek
Copy link
Owner

hynek commented Feb 9, 2015

So I’m a bit ignorant about stdlib here, how does one add log levels there?

@fxfitz
Copy link

fxfitz commented Sep 1, 2015

stdlib logging levels are defined as integers (defaults are: https://docs.python.org/2/library/logging.html#logging-levels).

For example, Logger.debug("OMG DEBUG MESSAGE") would be equivalent to Logger.msg(Logger.DEBUG, "OMG DEBUG MESSAGE")

@rydrman
Copy link

rydrman commented Nov 23, 2017

Has any more thought gone into this? It seems to me that a really good first step would be the ability for internal processes that use the logging level to resolve any integer to its closest recognized level. For example, say we have the standard recognized levels:

CRITICAL = 50
ERROR = 40
WARNING = 30
INFO = 20
DEBUG = 10
NOTSET = 0

in this case, logging at any level from 1-10 is simply understood as DEBUG, and anything over 50 is understood as CRITICAL

This would at least allow custom loggers to understand newly defined levels without breaking anything in the structlog stdlib

zdw added a commit to zdw/structlog that referenced this issue Nov 12, 2018
@sharmapankaj7
Copy link

Custom log level not working with structlog.

Reference - https://stackoverflow.com/q/54505487/6233947

@sharmapankaj7
Copy link

I tried above mentioned steps also
structlog.stdlib.TRACE = TRACE = 5
structlog.stdlib._NAME_TO_LEVEL['trace'] = TRACE
logging.addLevelName(TRACE, "TRACE")

But it is still not working for me with latest version

@pdebelak
Copy link

pdebelak commented Jun 5, 2019

@Pankajsharma1 This is technically possible, but very difficult. See possible solution at https://stackoverflow.com/a/56467981/3945932.

@darklow
Copy link
Contributor

darklow commented Oct 13, 2021

Less invasive way and no monkey patching by extending ConsoleRenderer if you only want it to look different:

class MyConsoleRenderer(ConsoleRenderer):
    def __call__(self, logger: WrappedLogger, name: str, event_dict: EventDict) -> str:
        custom_level = event_dict.pop('l', None)
        if custom_level:
            event_dict['level'] = custom_level

        level = event_dict.get('level', None)
        event = event_dict.get('event', None)
        if event:
            if level == 'success':
                event_dict['event'] = colorama.Fore.LIGHTBLUE_EX + _pad(event, self._pad_event) + self._styles.reset
        return super().__call__(logger, name, event_dict)

console_renderer = MyConsoleRenderer()
console_renderer._level_to_color['success'] = colorama.Fore.LIGHTBLUE_EX


# Change processor of formatter to `MyConsoleRenderer` and now you can use:
log.msg("It Happened!", l='success')

# PS. Same way I made debug dim grey as you see in the screenshot

image

@alexbalandi
Copy link

Hello! Is the code provided by @darklow currently the best way to add custom log level or is there a cleaner API as of now?

@hynek
Copy link
Owner

hynek commented Jan 24, 2024

For console output, I think you can have it more elegantly with the new columns configuration: https://www.structlog.org/en/stable/console-output.html#console-output-configuration

Have a look at https://www.structlog.org/en/stable/api.html#structlog.dev.LogLevelColumnFormatter and its implementation.

@leandrodesouzadev
Copy link

leandrodesouzadev commented Jul 17, 2024

@Pankajsharma1 This is technically possible, but very difficult. See possible solution at https://stackoverflow.com/a/56467981/3945932.

Is this still the only to achieve such functionality?
I would like to setup a few more logging levels, and this seems not that "friendly".
And I don't want just to add some colors, but use this information later for filtering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants