0

For a python project I have a simple logger that looks like this:

#!flask/bin/python
from flask import Flask,request, Response
import os.path
import json
import sys
import logging
import logging.handlers
from dbMongoManager import saveToMongo
from dbSQLManager import saveToMYSQL
from FailedRequest import FailedRequest
from JSONValidation import validateJSON



#create logger
logger = logging.getLogger('werkzeug')
#defines logger file and max size
handler = logging.handlers.RotatingFileHandler('request.log',maxBytes=5000000)
#define logger format
formatter = logging.Formatter("test %(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
handler.setFormatter(formatter)
#add loggerhandler to applications
logger.addHandler(handler)
app.logger.addHandler(handler)

logger.info("Logger started")
...
#app implementation here
.

The file request.log gets created, but the entry "logger started" is nowhere. Althought the events from flask (server startet/stopped, request responses) do get logged. All my custom logs (logger.info, logger.warning, etc..) appear nowhere in the file. Where could be my mistake? All example I've seen apparently work with this implementation.

Content off the log after reloading the script:

2015-04-01 08:54:52,479 [MainThread  ] [INFO ]   * Detected change in '/usr/local/bin/restService/restService.py', reloading
2015-04-01 08:55:08,393 [Thread-1    ] [INFO ]  194.209.7.10 - - [01/Apr/2015 08:55:08] "POST / HTTP/1.1" 400 -
2015-04-01 08:55:08,559 [Thread-1    ] [INFO ]  194.209.7.10 - - [01/Apr/2015 08:55:08] "POST / HTTP/1.1" 400 -

1 Answer 1

1

This should work

import logging
import logging.handlers

# create logger
root_logger = logging.getLogger()
root_logger.setLevel(0)
# defines logger file and max size
handler = logging.handlers.RotatingFileHandler('request.log', maxBytes=5000000)
# define logger format
formatter = logging.Formatter("test %(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
handler.setFormatter(formatter)
handler.setLevel(0)
# add loggerhandler to applications
root_logger.addHandler(handler)

logger = logging.getLogger('app')

logger.info("Logger started")
Sign up to request clarification or add additional context in comments.

2 Comments

thats brilliant. if I understand this correct, the "root_logger" logs everything, including the events of the sub logger "logger"?
Yes, you can build a logging hierarchy, then settings to a node upper in the hierarchy applies to lower ones, unless are overwritten. docs.python.org/2/library/logging.html#logging.getLogger

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.