My log code:
import os
import logging
import logging.handlers
import sys
from logging import raiseExceptions
from logging import Logger
LOG_PATH = '/tmp/'
class AppLogger(Logger):
def __init__(self, name, level=logging.NOTSET):
super(AppLogger, self).__init__(name, level)
def callHandlers(self, record):
"""
Pass a record to all relevant handlers.
Loop through all handlers for this logger and its parents in the
logger hierarchy. If no handler was found, output a one-off error
message to sys.stderr. Stop searching up the hierarchy whenever a
logger with the "propagate" attribute set to zero is found - that
will be the last logger whose handlers are called.
"""
c = self
found = 0
while c:
for hdlr in c.handlers:
found = found + 1
if hdlr.name == 'console':
if record.levelno >= hdlr.level:
hdlr.handle(record)
else:
if record.levelno == hdlr.level:
hdlr.handle(record)
if not c.propagate:
c = None # break out
else:
c = c.parent
if (found == 0) and raiseExceptions and not self.manager.emittedNoHandlerWarning:
sys.stderr.write("No handlers could be found for logger"
" \"%s\"\n" % self.name)
self.manager.emittedNoHandlerWarning = 1
def get_logger(logfile_name=__name__, log_path=LOG_PATH):
'''save log to diffrent file by different log level into the log path
and print all log in console'''
logging.setLoggerClass(AppLogger)
formatter = logging.Formatter(
'%(asctime)s %(name)s %(levelname)s %(message)s',
'%Y-%m-%d %H:%M:%S')
log_files = {
logging.DEBUG: os.path.join(log_path, logfile_name + '-debug.log'),
logging.INFO: os.path.join(log_path, logfile_name + '-info.log'),
logging.WARNING: os.path.join(log_path, logfile_name + '-warning.log'),
logging.ERROR: os.path.join(log_path, logfile_name + '-error.log'),
logging.CRITICAL: os.path.join(log_path, logfile_name + '-critical.log')
}
logger = logging.getLogger()
logger.name = 'app'
logger.setLevel(logging.DEBUG)
for log_level, log_file in log_files.items():
file_handler = logging.handlers.TimedRotatingFileHandler(log_file, 'midnight')
file_handler.setLevel(log_level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.name = "console"
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger
logger = get_logger()
my flask code:
from log import logger
from flask import Flask
app = Flask(__name__)
app.debug = True
@app.route("/")
def hello():
return "Hello World!"
logger.debug('----')
logger.info('----')
logger.error('----')
logger.warning('----')
app.run()
I want to save the DEBUG level log in debug.log, INFO level log in info.log, WARNING level log in warning.log, ERROR level log in error.log, both the flask framework's log and my custom log, and I need print all the log in console.
I custom the AppLogger, but now this just working on the flask framework's log, my custom log not save in right file, the info,warning,error all write in the info.log, the name app is my custom log, it save log together.
how to make the info.log just save the app's INFO log?
the info.log:
2017-11-08 20:07:31 app INFO ----
2017-11-08 20:07:31 app ERROR ----
2017-11-08 20:07:31 app WARNING ----
2017-11-08 20:07:31 werkzeug INFO * Restarting with stat
2017-11-08 20:07:31 app INFO ----
2017-11-08 20:07:31 app ERROR ----
2017-11-08 20:07:31 app WARNING ----
2017-11-08 20:07:31 werkzeug INFO * Debugger PIN: 971-444-041
2017-11-08 20:07:31 werkzeug INFO * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
the warning.log
2017-11-08 20:07:31 app ERROR ----
2017-11-08 20:07:31 app WARNING ----
2017-11-08 20:07:31 app ERROR ----
2017-11-08 20:07:31 app WARNING ----
2017-11-08 20:07:31 werkzeug WARNING * Debugger is active!