WSGI(gevent) logging to stdout and stderr - python

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

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=

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 configuration file with bandersnatch on python 3.5

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!

Logger.info never outputs

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

Categories

Resources