Python logging configuration file with bandersnatch on python 3.5 - python

I'm using the latest bandersnatch 2.0.0 with python 3.5 to create a PyPi mirror. Bandersnatch has some fairly sparse documentation, but in the sample config file, it says:
; Advanced logging configuration. Uncomment and set to the location of a
; python logging format logging config file.
; log-config = /etc/bandersnatch-log.conf
So I've ready about python logging configuration, uncommented the line above and created this logging config:
[loggers]
keys=root
[handlers]
keys=logfile
[formatters]
keys=logfileformatter
[logger_root]
level=NOTSET
handlers=logfile
[formatter_logfileformatter]
format=%(asctime)s %(name)-12s: %(levelname)s %(message)s
[handler_logfile]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=logfileformatter
args=('/path/to/bandersnatch.log','a',10485760,5)
Now bandersnatch doesn't produce any output to stdout any more, and the log file I've specified has been created, but nothing is being logged.
I've tried varying combinations of NOTSET and DEBUG for both log levels specified, but nothing has been logged yet when I run bandersnatch.
Any ideas? All the other issues I've seen about this have been programming errors or people forgetting to set the loglevel for [logger_root] for example. I don't think I've missed any of these.

May be I'm answering too late, but could be helpful...
The issue seems to be linked to root logger, you can create a second logger as below:
[loggers]
keys=root,file
[handlers]
keys=root,file
[formatters]
keys=common
[logger_root]
level=NOTSET
handlers=root
[logger_file]
level=INFO
handlers=file
propagate=1
qualname=bandersnatch
[formatter_common]
format=%(asctime)s %(name)-12s: %(levelname)s %(message)s
[handler_root]
class=StreamHandler
level=DEBUG
formatter=common
args=(sys.stdout,)
[handler_file]
class=handlers.RotatingFileHandler
level=INFO
formatter=common
args=('/path/to/bandersnatch.log','D',1,'UTF-8')
#will manage one file a day
Hope it helps!

Related

How to get non-root log statements using fileConfig without specifying each one?

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=

WSGI(gevent) logging to stdout and stderr

I'm trying to log my application and I want to use the logging module for it. The application is running in a docker-container, which made me to log to the stdout and stderr so I can see it in the docker logs. Unfortunately only my root logger is working who is writing to file. I already searched for this case, but I was unable to find a solution.
For better reference:
config.ini
[loggers]
keys=root, info
[handlers]
keys=debug, info
[formatters]
keys=debug, info, error
[logger_root]
level=DEBUG
handlers=debug
[logger_info]
level=INFO
handlers=info
qualname=docker.info
propagate=0
[handler_debug]
class=FileHandler
level=DEBUG
formatter=debug
args=('.logs', 'a+')
[handler_info]
class=StreamHandler
level=INFO
formatter=info
args=(sys.stdout,)
[formatter_debug]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
[formatter_info]
format=%(levelname)s - %(message)s
main.py
import logging.config
logging.config.fileConfig(fname='logger.ini')
logger = logging.getLogger(__name__)
from gevent import monkey
monkey.patch_all()
from gevent import pywsgi
# some logging here
http_server = pywsgi.WSGIServer(('0.0.0.0', 5000), app, log=logger)
http_server.serve_forever()

Why does my python logger still print out messages from higher logging levels when set to DEBUG?

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)

Filehandler and consolehandler aren't working properly with logging config

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)

Python logging to different destination using a configuration file

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")

Categories

Resources