logging.info doesn't show up on console but warn and error do
PythonLoggingWarningsPython Problem Overview
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.
Python Solutions
Solution 1 - Python
The root logger always defaults to WARNING level. Try calling
logging.getLogger().setLevel(logging.INFO)
and you should be fine.
Solution 2 - Python
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)
Solution 3 - Python
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)
Solution 4 - Python
This will work
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.info('its working')
Solution 5 - Python
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, specifyinglevel=...
is NOT needed)- it's necessary to configure the root logger:
logging.getLogger().setLevel(...)
So, a minimum working example is:
# in library/module code
import logging
lg = logging.getLogger('x.y.z')
# in application code
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO) # configure root logger
lg.info("hi") # this should print now
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
Solution 6 - Python
For those using absl.logging
, the equivalent command is
from absl import logging
logging.set_verbosity(logging.INFO)
Solution 7 - Python
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