3

Changing the log level dynamically does not work.

import logging

logger = logging.Logger("MyLogger", level=logging.INFO)
formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
console = logging.StreamHandler()
console.setFormatter(formatter)
logger.addHandler(console)

logger.setLevel("INFO")
logger.info("should show up")
logger.setLevel(logging.CRITICAL)
logger.info("should not show up")

Output

MyLogger - INFO - should show up
MyLogger - INFO - should not show up

Any suggestion what am I doing wrong?

1
  • 1
    You should use logging.basicConfig to configure the root logger, which includes setting the level and creating a StreamHandler automatically. Commented Jan 13, 2021 at 22:17

3 Answers 3

4

It doesn't work in your case because the logger is not being created in the documented way:

Note that Loggers should NEVER be instantiated directly, but always through the module-level function logging.getLogger(name).

Creating the logger with logging.Logger(...) has bypassed the logging module's global state, and defeated the hierarchy of the logging tree. Specifically, it has caused an incorrectly cached result to be returned by logger.isEnabledFor().

import logging

# logger = logging.Logger("MyLogger", level=logging.INFO)  # no
logger = logging.getLogger("MyLogger")  # yes

formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
console = logging.StreamHandler()
console.setFormatter(formatter)
logger.addHandler(console)

logger.setLevel("INFO")
logger.info("should show up")
logger.setLevel(logging.CRITICAL)
logger.info("should not show up")

By the way

Elias's answer is "working", but doesn't really tell the whole story here. The answer's claim that you need to change on the logger and its handlers is incorrect.

In fact, there are two setLevel methods (on loggers and handlers). The level set in the logger determines which severity of messages it will pass to its handlers. The level set in each handler determines which messages that handler will send on. In normal usage, setting just one of them up to CRITICAL would have been enough to filter that INFO event.

If you create in the usual way, setting the level on a logger instance will work without fiddling handlers.

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

Comments

-1

To change the logging level you need to change it on the parent and its handlers:

level = logging.CRITICAL
logger.setLevel(level)
for handler in logger.handlers:
    handler.setLevel(level)

Comments

-2

you can try this, at the root of your django project(assuming that's the use case), create a 'logger.py' file and have following code it in:

import logging
import os

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'simple': {
        'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
    }
},
'handlers': {
    'my_project': {
        'level': logging.DEBUG,
        'class': 'logging.FileHandler',
        'filename': "/path/to/log.file",
        'formatter': 'simple'
    },
    'console': {
        'level': logging.DEBUG, 
        'class': 'logging.StreamHandler',
    },
},
'loggers': {
    'my_project': {
        'handlers': ['my_project'],
        'level': logging.DEBUG,
        'propagate': False,
    }
},
}

now in any file you want to add log statements add the following lines of code:

from my_project.logger import logging
logger = logging.getLogger(__name__)

def method_name():
    logger.setLevel(logging.CRITICAL)
    logger.critical("...")
    logger.info("...")
    logger.setLevel(logging.INFO)
    logger.info("...")

These log statements should illustrate dynamic logging behaviour.

1 Comment

What's the point of LOGGING if you don't use it? Is it used implicitly by Django? I've never used Django myself so IDK. Please edit to clarify. What made you think OP's using Django anyway?

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.