logger.setLevel(logging.INFO) does not make logger.info() visible - python

I am working on Python 3.4.2 under Windows. In my case,
import logging
logger = logging.getLogger('logger')
logger.setLevel(logging.INFO)
logger.info('test')
shows nothing on the console. But
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('logger')
logger.info('test')
sucessfully shows
INFO:logger:test
I expected logger.setLevel(logging.INFO) would allow the same printout as in the second code snippet where the logger level is controlled by the root logger but it doesn't. So where's the problem? Thank you.

You need to add a Handler to the logger. The call to basicConfig is one way to do that.
There are many kinds of Handlers. A StreamHandler prints to the console, but there are also FileHandlers, SocketHandlers, and SMTPHandlers for instance. If you don't specify any handlers, the records aren't handled and nothing gets emitted.
To add a StreamHandler (without calling basicConfig) you could use:
import logging
logger = logging.getLogger('logger')
logger.setLevel(logging.INFO)
console = logging.StreamHandler()
logger.addHandler(console)
logger.info('test')
This adds the StreamHandler to the logger named logger. To add the handler to the root logger, use
logging.root.addHandler(console)
By setting the Handler on the root logger, the StreamHandler may handle records generated by other loggers than just logger. Consult the logging flow diagram for a picture of when that happens.

Related

python logging to file vs console with certain logLevel

import logging
import sys
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler('test.log')
file_handler.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
logger.info('Info print')
logger.debug('Debug print')
I have simple code above. How come the debug statement doesn't print? It seems like the logLevel is always determined by line 5. The file_handler.setLevel and the console_handler.setLevel seem to do nothing. I want to eventually have debug prints going to the test.log file, and info prints going to the console.
The logger and handler levels are both used, but at different times. The logger's level is inspected first, and if the event severity level is >= the logger's level, then the event is passed to handlers. The event's level is then checked against each handler's level to determine if that handler handles the event.

Python logging: Why am not seing the INFO and DEBUG mnessages? [duplicate]

I created the following script. Could any of you explain to me why the output is like what shows below
Source
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
print('debug', logger.isEnabledFor(logging.DEBUG))
print('info', logger.isEnabledFor(logging.INFO))
print('warning', logger.isEnabledFor(logging.WARNING))
print('error', logger.isEnabledFor(logging.ERROR))
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
Output
debug True
info True
warning True
error True
warning
error
DEBUG:root:debug
INFO:root:info
WARNING:root:warning
ERROR:root:error
Specifically
what is the difference between logger.info and logging.info here
how come that logger.isEnabledFor(logging.DEBUG) is True while logger.debug('debug') does not show anything
how come that logger.info has no output but logging.info has
A few things to clarify:
Default log level for root logger is WARNING
Root logger is not initialized if you do nothing, that is, without any handlers or formatter set up:
>>> import logging
>>> logging.root.handlers
[]
Okay, but you found out the problem: when logging level set to DEBUG, the root logger is not working as expected. Debug messages are ignored. With the same not configured root logger, warning messages output normally. Why is that?
Keep in mind we don't have any handler for root logger right now. But looking into the code, we do see:
if (found == 0):
if lastResort:
if record.levelno >= lastResort.level:
lastResort.handle(record)
elif raiseExceptions and not self.manager.emittedNoHandlerWarning:
sys.stderr.write("No handlers could be found for logger"
" \"%s\"\n" % self.name)
self.manager.emittedNoHandlerWarning = True
Which means, we have a lastResort for backup if no handler is found. You can refer to the definition of lastResort, it is initialized with logging level WARNING. Meanwhile, debug messages don't have this backup so they are ignored when no handler is set.
For your questions:
These two loggers are identical, since the root logger is returned when getLogger() receives no arguments.
See below:
Logger.isEnabledFor(lvl)
Indicates if a message of severity lvl would
be processed by this logger. This method checks first the module-level
level set by logging.disable(lvl) and then the logger’s effective
level as determined by getEffectiveLevel().
Calling any logging functions in logging module will initialize the root logger with basicConfig() which adds a default handler, so that the subsequent calls on logger will also work.
What you should do is, use logging.basicConfig() to set up a default handler for root logger and messages will be output according to the logger level and message level.
getLogger creates an instance of Logger class if argument name is added. Otherwise it returns root logger. So in this case the program is using the common logger as functions logging.debug, logging.info, logging.warning, logging.info

How can I set the level on a custom python logger?

I have a custom python logger
# logger.py
import logging
#logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
c_handler = logging.StreamHandler()
c_handler.setLevel(logging.DEBUG)
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
logger.addHandler(c_handler)
I have set the level to DEBUG, but only WARNINGS (and above) are shown
from ..logger import logger
...
logger.debug('this is a debug log message')
logger.warning('too hot to handle')
...
my_module.logger:too hot to handle
if I uncomment the line
logging.basicConfig(level=logging.DEBUG)
then I get the DEBUG level, but two copies of the message
my_module.logger - DEBUG - this is a debug log message
DEBUG:my_module.logger:this is a debug log message
my_module.logger - WARNING - too hot to handle
WARNING:my_module.logger:too hot to handle
I am not importing logging at any other point in the package
How should I configure the logger?
TL;DR Use logger.setLevel(logging.DEBUG)
According to Python documentation, a handler processes messages with a level equal to or higher than the handler is set to (via .setLevel()).
But also note, emphasis mine:
When a logger is created, the level is set to NOTSET (which causes all messages to be processed when the logger is the root logger, or delegation to the parent when the logger is a non-root logger). Note that the root logger is created with level WARNING.
So without logging.basicConfig, there's no "root logger" at program startup, and your first getLogger() creates a stub root logger with default level WARNING, and your logger with level NOTSET (which fallbacks to that of the root logger). As a result your logger.debug message is thrown away before it gets handled.
With logging.basicConfig, you explicitly create a root logger with the given level and a StreamHandler with default Formatter. Your new getLogger() is attached to the root logger and any log record is propagated to the root logger - thus printing twice with a different formatter (the default one indeed).
The stub root logger created by the first call to getLogger() has no handler attached so any propagated record is not printed out.
If you want to have full control over your logging facility, it's better to give your logger an explicit level than relying on basicConfig, which creates a root logger that you may not want:
logger.setLevel(logging.DEBUG)
Having read the docs again I realise that propagate is the attribute that I need to use to turn off the ancestor logging output. So my logger becomes
# logger.py
import logging
logging.basicConfig(level=logging.DEBUG)
logger.propagate = False
logger = logging.getLogger(__name__)
c_handler = logging.StreamHandler()
c_handler.setLevel(logging.DEBUG)
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
logger.addHandler(c_handler)
And I get just one log message and the debug level is used

Python Logging - Prevent log events from being printed to the console

I can't figure out why log events are being printed to the console when I have not defined a console handler. All of the examples I read through have explicitly defined a console handler (streamhandler) in order to print messages to the console.
I want these events to be printed to a file only.
import logging
logger = logging.getLogger(__name__)
my_format = '%(asctime)-25s %(levelname)-8s LOGGER: %(name)-12s MODULE: %(module)-15s FUNCTION: %(funcName)-30s MSG: %(message)s'
my_datefmt ='%m/%d/%Y %I:%M:%S%p'
logging.basicConfig(format=my_format, datefmt=my_datefmt, level=logging.DEBUG)
formatter = logging.Formatter(my_format, datefmt=my_datefmt)
logger.setLevel(logging.DEBUG)
handler1 = logging.FileHandler('mylog.txt')
handler1.setLevel(logging.DEBUG)
handler1.setFormatter(formatter)
logger.addHandler(handler1)
logger.debug("Why is this printed to the console")
EDIT:
It has been pointed out that I was not considering the root logger. Upon calling logging.basicConfig, a default streamhandler is added to the root logger (logger = getLogger())
The root logger's handler can be modified, however I have found that I can just prevent my logger from propagating logs up to the root logger.
This can be done like so:
import logging
logger = logging.getLogger(__name__)
my_format = '%(asctime)-25s %(levelname)-8s LOGGER: %(name)-12s MODULE: %(module)-15s FUNCTION: %(funcName)-30s MSG: %(message)s'
my_datefmt ='%m/%d/%Y %I:%M:%S%p'
logging.basicConfig(format=my_format, datefmt=my_datefmt, level=logging.DEBUG)
formatter = logging.Formatter(my_format, datefmt=my_datefmt)
logger.setLevel(logging.DEBUG)
handler1 = logging.FileHandler('mylog.txt')
handler1.setLevel(logging.DEBUG)
handler1.setFormatter(formatter)
logger.addHandler(handler1)
logger.propagate = False ####
logger.debug("Why is this printed to the console")
> ipython
import logging
logging.basicConfig?
*****************logging.basicConfig**************
Signature: logging.basicConfig(**kwargs)
Docstring:
Do basic configuration for the logging system.
This function does nothing if the root logger already has handlers
configured. It is a convenience method intended for use by simple scripts
to do one-shot configuration of the logging package.
The default behaviour is to create a StreamHandler which writes to
sys.stderr, set a formatter using the BASIC_FORMAT format string, and
add the handler to the root logger.
...
You have 2 handlers.

logging.info doesn't show up on console but warn and error do

When I log an event with logging.info, it doesn't appear in the Python terminal.
import logging
logging.info('I am info') # no output
In contrast, events logged with logging.warn do appear in the terminal.
import logging
logging.warn('I am warning') # outputs "I am warning"
Is there a environment level change I can to make logging.info print to the console? I want to avoid making changes in each Python file.
The root logger always defaults to WARNING level. Try calling
logging.getLogger().setLevel(logging.INFO)
and you should be fine.
Like #ztyx said that default logger level is WARNING. You have to set it to a lower level
You can do it by using logging.basicConfig and setting logger level:
logging.basicConfig(level=logging.DEBUG)
The above solutions didn't work for me, but the code here did:
# set up logging to file
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='/temp/myapp.log',
filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
(I omitted parts of the code for the sake of readability)
This will work
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.info('its working')
In more recent versions of Python 3 (tested with Python 3.8), console logging requires creating a console handler to correctly show info messages.
The following example is modified from the Configuring Logging example in the Python documentation:
import logging
# create logger
logger = logging.getLogger('__name__')
level = logging.INFO
logger.setLevel(level)
# ----> console info messages require these lines <----
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(level)
# add ch to logger
logger.addHandler(ch)
# -----------------------------------------------------
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')
Running the above code generates the following output:
info message
warn message
error message
critical message
Here is this same code without the console handler.
import logging
# create logger
logger = logging.getLogger('__name__')
level = logging.INFO
logger.setLevel(level)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')
Without the console handler, the output does not include the info message:
warn message
error message
critical message
I do not understand why this is the case as it seems unnecessary.
What's the minimum required code for a working module-level logger? I did an experiment (with python version 3.8.6).
The take-away is:
logging.basicConfig() is needed (however, specifying level=... is NOT needed)
it's necessary to configure the root logger: logging.getLogger().setLevel(...)
So, a minimum working example is:
The library/module code does NOT need to configure the logger:
# library/module code: lib.py
import logging
LOGGER = logging.getLogger('x.y.z')
def some_function():
LOGGER.info("hi")
The application code need to configure the logger with 2 lines at minimum:
# Application Code
import logging, lib
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO) # configure root logger
main() # code that will trigger lib
Here's the experiment:
In [1]: import logging
In [2]: lg = logging.getLogger('x.y.z')
In [3]: lg.info(1)
In [4]: logging.basicConfig()
In [5]: lg.info(1)
In [6]: logging.basicConfig(level=logging.INFO)
In [7]: lg.info(1)
In [8]: logging.basicConfig()
In [9]: logging.getLogger().setLevel(logging.INFO)
In [10]: lg.info(1)
INFO:x.y.z:1
For those using absl.logging, the equivalent command is
from absl import logging
logging.set_verbosity(logging.INFO)
If you are using Django to power your server, you just simply need to change the log level in your settings.py file as such:
"handlers": {
"console": {
-- "level": "WARNING",
++ "level": "INFO",
"class": "logging.StreamHandler",
"formatter": "stackdriver",
}
},
More examples in the documentation here:
https://docs.djangoproject.com/en/4.0/topics/logging/#configuring-logging-1
logging.info() will use the root logger for logging.
According to official doc,
if you do not set an explicit handler for the logger, a special handler called lastResort will be used.
See the code here. By default the logging level of lastResort (it is stream handler) is 30.
we can change its level to output info message.
# setting both the logger and handler's level will work as expected.
logger.setLevel(logging.DEBUG)
logging.lastResort.setLevel(logging.DEBUG)
However, this is like a hack and never a encouraged action.
Using logging.basicConfig()
If we want to do logging real quick, we can use the method logging.basicConfig.
logging.basicConfig(level=logging.DEBUG)
This will create the logger. The logger.level will be the level we set here.
A stream handler will be also created, with level NOTSET.
Without a level param, the default level for root logger is WARNING.
reference
logger logging without handler: logger logging without a handler

Categories

Resources