0

Below is the function in __init__.py file which means this part of code always run when code is executed

import logging
def log_setup():

    logging.TRACE = 5
    logging.addLevelName(5, 'TRACE')
    def trace(obj, message, *args, **kws):
        obj.log(logging.TRACE, message, *args, **kws)
    logging.Logger.trace = trace
    logging.trace = trace
    root = logging.getLogger("TEST")
    root.setLevel(5)
    ch = logging.StreamHandler()
    ch.setFormatter(f)
    ch.setLevel(5)
    root.handlers = []
    root.addHandler(ch)

I am having below code in one of the library say xyz.py

import logging
log = logging.getLogger("TEST."+__name__)
if log.trace:
    print("***ELLO***", log.getEffectiveLevel())
    print("***ELLO***", log.isEnabledFor(logging.DEBUG))
    print("***ELLO***", log.isEnabledFor(logging.TRACE))
    log.trace("Hey Statement printed")

When I am calling via script, I am not able to get the log.trace printed. Interestingly, For log.isEnabledFor(logging.TRACE) is always returning False.

Not sure what i am missing here

Below is the output

***ELLO*** 10
***ELLO*** True
***ELLO*** False
4
  • I didn't get it. How __name__ is having debug level. Commented Aug 6, 2018 at 6:54
  • Hum, I guess I made a mistake, sorry. Commented Aug 6, 2018 at 6:56
  • Can't reproduce. If I take your code, enhance it by defining the formatter in log_setup() (e.g. f = logging.Formatter('{message}', style='{') and calling log_setup() in __init__.py on module level, I get Hey Statement printed in the terminal. Double-check the code, maybe you forgot to call log_setup()? Commented Aug 6, 2018 at 10:42
  • @hoefling: Script from where I was calling was having call as root = logging.getLogger("TEST"). I changed this command to root = logging.getLogger("TEST."+__name) and it worked. Not sure why Commented Aug 6, 2018 at 11:27

2 Answers 2

1

There is not TRACE log level in python unless you have created a custom handler by yourself

Below is the list of log levels

enter image description here

Please refer there https://docs.python.org/2/library/logging.html#levels

Sign up to request clarification or add additional context in comments.

1 Comment

I have added custom log level Trace with LevelNum 5
1

Your code does not run: for example, pasting it into a source file, it fails because f is undefined. Commenting out that line, I got it to run without any problems. Are you sure that log_setup() is being called before the code in xyz? My guess is that it isn't. Also, the line

root.handlers = []

shouldn't be there - you shouldn't do this, as handlers is an internal attribute and not meant for you to change directly. The line

logging.trace = trace

also looks wrong, as the logging module-level convenience functions don't take an initial object argument, and have a signature such as debug(msg, *args, **kwargs). Presumably you would want a module-level trace function to have an analogous signature.

1 Comment

Script from where I was calling was having call as root = logging.getLogger("TEST"). I changed this command to root = logging.getLogger("TEST."+__name) and it worked. Not sure why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.