import logging from conf import settings def logger(log_type): #Generate logger object logger = logging.getLogger(log_type) logger.setLevel(settings.LOG_LEVEL) #Generate handler object and output log information to file log_file = " %s/log/%s.log " % (settings.BASE_DIR, log_type) global fh fh = logging.FileHandler(log_file) fh.setLevel(settings.LOG_LEVEL) # 生成formatter对象 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # The formatter object is bound to the handler object fh.setFormatter(formatter) if logger.handlers: #Clear logger.handlers, otherwise the log will be printed repeatedly logger.handlers.pop() # The handler object is bound to the logger logger.addHandler(fh) return logger
The log of logging can be divided into debug(), info(), warning(), error() and critical()
5 levels
% (name)s Logger's name % (levelno)s Log level in numeric form % (levelname)s Log level in text form % (pathname)s Full pathname of the module calling the log output function, possibly without % (filename) s The file name of the module that calls the log output function % (module)s The name of the module that calls the log output function % (funcName)s The name of the function that calls the log output function % (lineno)d The line of code where the statement that calls the log output function is located % (created)f The current time, expressed as a floating-point number representing time in UNIX standard % (relativeCreated)d When outputting log information, the milliseconds since the Logger was created %(asctime)s The current time in the form of a string. The default format is "2003-07-08 16:49:45,896 ". Following the comma is the millisecond % (thread)d thread ID. There may be no % (threadName)s thread names. There may be no % (process)d process ID. Maybe not %(message)s User output message