Why in python logger.info("print something") does not output. I have seen questions asked before, but solution doesnt exist. I do not want to use logger.debug or logger.warning to see text.
Simply logger.info should print the text, otherwise whats the use of this?
logging.conf file as below
[loggers]
keys=root
[handlers]
keys=stream
[formatters]
keys=formatter
[logger_root]
level=INFO
handlers=stream
[handler_stream]
class=StreamHandler
level=INFO
formatter=formatter
args=(sys.stderr,)
[formatter_formatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
Demo code that access logger:
import logging
logger = logging.getLogger()
if __name__ == '__main__':
logger.info("logger")
print("print")
Output is only print, not the logger. So logger.info does not work.
By default, the root logger (the one you use when you say logger.info) is set at a level of WARN.
You can either do:
logging.basicConfig(level=logging.INFO)
or logging.getLogger().setLevel(logging.INFO)
Seems you do not load your configuration file. You should add this:
logging.config.fileConfig('path_to_logging.conf')
before logger = logging.getLogger()
because right now you are using the default WARNING level.
EDIT: in order to use logging.config, you have to import it too:
import logging.config
So the complete code should be:
import logging
import logging.config
logging.config.fileConfig('path_to_logging.conf')
logger = logging.getLogger()
if __name__ == '__main__':
logger.info("logger")
print("print")
The code above, with the following logging.conf (same as you except I removed the sentry parts):
[loggers]
keys=root
[handlers]
keys=stream
[formatters]
keys=formatter
[logger_root]
level=INFO
handlers=stream
[handler_stream]
class=StreamHandler
level=INFO
formatter=formatter
args=(sys.stderr,)
[formatter_formatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
does work:
$ ./test_script3.py
2016-05-23 15:37:40,437 - root - INFO - logger
print
Related
How do I get imported modules to show up in my filehandler without specifying each one?
When I run the "main.py", the 'sLogger' information is specified in the "log.txt" file, but not the 'module_a' information. The 'module_a' info only shows up in the console window.
I know I can fix this by adding a separate logger for 'module_a', but don't want to do this for every single module I want to get logging information from.
I thought setting 'disable_existing_loggers=False' to False would resolve this, but it isn't.
Am I doing something incorrectly there?
I also don't want to push everything with the root logger.
If I add fileHandler to the root I get everything, but I don't want to get info from the root logger.
main.py
import logging
from logging import config
logging.config.fileConfig("logger_config.txt", defaults={'logfilename': "test.log"}, disable_existing_loggers=False)
logger = logging.getLogger("sLogger")
logger.info("test")
logging.info("test")
import module_a
module_A.py:
logger = loging.getLogger(__name__)
logger.info("Test Log Message")
file_config.txt
[loggers]
keys=root, sLogger
[handlers]
keys=consoleHandler,fileHandler
[formatters]
keys=fileFormatter,consoleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_sLogger]
level=DEBUG
handlers=consoleHandler,fileHandler
qualname=sLogger
propagate=0
[logger_parser]
level=DEBUG
handlers=consoleHandler,fileHandler
qualname=sLogger
propagate=1
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=consoleFormatter
args=(sys.stdout,)
[handler_fileHandler]
class=FileHandler
level=INFO
formatter=fileFormatter
args=('%(logfilename)s',)
[formatter_fileFormatter]
format=%(asctime)s - %(name)s - %(levelname)s: %(message)s
datefmt=
[formatter_consoleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s: %(message)s
datefmt=
I need help with logger handlers. I need to add console handler on info level and file handler on debug level in this function and I can not do it.
def create_logger() -> logging.Logger:
logging.basicConfig(
filename=RESOURCES / 'ass_3.log',
format='%(asctime)s | %(name)s | %(levelname)-5s | %(message)s',
datefmt="%Y-%m-%d %H:%M:%S",
filemode='w',
level=logging.DEBUG)
return logging.getLogger('ass_3_logger')
Why not just import logger and load a logger.config like this:
import logging
import logging.config
logging.config.fileConfig(fname='file.conf',
disable_existing_loggers=False)
# Get the logger specified in the file
logger = logging.getLogger(__name__)
logger.debug('This is a debug message')
and as logger config use something like this:
[loggers]
keys=root,sampleLogger
[handlers]
keys=consoleHandler
[formatters]
keys=sampleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_sampleLogger]
level=DEBUG
handlers=consoleHandler
qualname=sampleLogger
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=sampleFormatter
args=(sys.stdout,)
[formatter_sampleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
I have my python logger set to debug, but it still prints out info messages:
import logging
from logging.config import fileConfig
fileConfig('./log/logging_config_serial.ini')
logger = logging.getLogger()
logger.debug("debug")
2018-10-01 09:58:43,161 root DEBUG debug
logger.info("info")
2018-10-01 09:58:50,997 root INFO info
logger.getEffectiveLevel()
Out[12]: 10
Looks like it set to debug level on the output (10=DEBUG, 20=INFO)
Here is my config file:
[loggers]
keys=root
[handlers]
keys=stream_handler,fileHandler
[formatters]
keys=formatter
[logger_root]
level=DEBUG
handlers=stream_handler,fileHandler
[handler_stream_handler]
class=StreamHandler
level=DEBUG
formatter=formatter
args=(sys.stderr,)
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=formatter
args=("./log/l5e5_get_header_info_serial_R3.log",)
[formatter_formatter]
format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s
DEBUG is the lowest level so by default this will include all of the higher levels also (as by default it's assumed if you are looking at DEBUG you'll also want to see WARNINGS, INFO and ERRORs)
I'm having a bit of an issue here. I have set up a scratch py file just to test out my logging. Neither the consoleHandler nor the fileHandler are getting the right output (any output). Can anyone see if they can eyeball any issues? Here is my log config file
[loggers]
keys=root
[handlers]
keys=fileHandler, consoleHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=fileHandler, consoleHandler
formatter=simpleFormatter
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout, )
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=(os.path.join(os.getcwd(), 'logging.log'), 'w')
[formatter_simpleFormatter]
format=%(asctime)s - %(levelname)s - %(message)s
datefmt="%Y-%m-%d %H:%M:%S"
Here is the scratch python file,
import logging
from logging import config
LOGGER_NAME = 'Work'
logger = logging.getLogger(LOGGER_NAME)
logging.config.fileConfig('C:\\Users\\cschuma1\\PycharmProjects\\workstuff\\config\\logging.conf')
addition = 'add' + 'ition'
logger.debug('print addition %s', addition)
I have looked closely at other's configs on stackoverflow and I believe that I have all of the necessary levels set, and formatter/handler objects, etc. for my task. Does anyone know what I am missing?
It turns out that at least in python 3, the line
logging.config.fileConfig('C:\\Users\\cschuma1\\PycharmProjects\\workstuff\\config\\logging.conf')
needs to come before,
LOGGER_NAME = 'Work'
logger = logging.getLogger(LOGGER_NAME)
This is the fixed sample program I had above,
import logging
from logging import config
logging.config.fileConfig('C:\\Users\\cschuma1\\PycharmProjects\\workstuff\\config\\logging.conf')
LOGGER_NAME = 'Work'
logger = logging.getLogger(LOGGER_NAME)
addition = 'add' + 'ition'
logger.debug('print addition %s', addition)
I want to use a configuration file to create two loggers which will log in two distinct files. My configuration file looks like:
[loggers]
keys=root,main,zipper
[handlers]
keys=main,zip
[formatters]
keys=fmt
[logger_root]
level=DEBUG
handlers=main
[logger_main]
level=DEBUG
handlers=main
qualname=MAIN
[logger_zipper]
level=DEBUG
qualname=UPLOAD
handlers=zip
[handler_zip]
class=FileHandler
level=DEBUG
formatter=fmt
args=('zipper.log','a+')
[handler_main]
class=FileHandler
level=DEBUG
formatter=fmt
args=('main.log','a+')
[formatter_fmt]
format=%(asctime)s - %(levelname)s - %(name)s - %(message)s
I try to use this configuration file like this:
import logging
import logging.config
logging.config.fileConfig("logging.conf")
# Logs to the first file
log = logging.getLogger("")
log.debug("unspec - debug")
log.error("unspec - error")
# Logs to the first file
log_r = logging.getLogger("main")
log_r.debug("main - debug")
log_r.error("main - error")
# Also logs to the first file :(
log_z = logging.getLogger("zipper")
log_z.debug("zipper - debug")
log_z.error("zipper - error")
For some reason I don't understand, all logging messages go to the first file, when I expect the last two to be written to 'zipper.log'. What am I missing ?
The problem is that the qualified name used in the configuration file:
[logger_zipper]
level=DEBUG
qualname=UPLOAD
handlers=zip
doesn't match the one used in the code:
log_z = logging.getLogger("zipper")
Use any of these combinations:
qualname=zipper and logging.getLogger("zipper")
qualname=UPLOAD and logging.getLogger("UPLOAD")