# ! /Usr/bin/ENV Python # -*-Coding: UTF-8 -*- ''' Improved LOG classes: 1. if you use the format object, you do not need to collect Environment Information yourself. The database can collect information 2. bind multiple handler to a logger, and each handler sets the corresponding level of logs to generate multiple copies. Each handler only records its own level and contains logs higher than its own level. we still need the inspect library to collect environment information. Because the logging API is encapsulated, the format can only collect function information that directly calls the logging module API. ''' Import OS Import Sys Import Time Import Logging Import Inspecthandlers = {Logging. notset: " // Tmp/TNLOG-notset.log " , Logging. Info: " // Tmp/TNLOG-info.log " , Logging. Debug: " // Tmp/TNLOG-debug.log " , Logging. Warning: " // Tmp/TNLOG-warning.log " , Logging. Error: " // Tmp/TNLOG-error.log " , Logging. Critical: " // Tmp/TNLOG-critical.log " } Def Createhandlers (): loglevels = Handlers. Keys () ''' Log format: [time] [type] [recordCode] Information ''' FMT = ' [% (Asctime) S] [% (levelname) S] % (Message) S ' Formatter = Logging. formatter (FMT) For Level In Loglevels: Path = OS. Path. abspath (handlers [level]) Print Path handlers [level] = Logging. filehandler (PATH) handlers [level]. setlevel (level) handlers [level]. setformatter (formatter) # Create a local variable when loading the module Createhandlers () Class Tnlog (object ): ''' Tnlog. Constant ''' Notset = Logging. notset info =Logging. info debug = Logging. debug warning = Logging. Warning Error = Logging. Error critical = Logging. Critical Def _ Init __ (Self, setlevel = Logging. notset): Self. _ Loggers = Logging. getlogger () loglevels = Handlers. Keys () For LevelIn Loglevels: Self. _ Loggers . Addhandler (handlers [level]) # Bind multiple handler to a logger Self. _ Loggers . Setlevel (setlevel) Def Addcallingpointinfo (self, message): frame, filename, lineno, functionname, code, iunknowfield = Inspect. Stack () [2 ] ''' [File name-number of rows-function name] Information ''' Return " [% S-% s] % s " % (Filename, lineno, functionname, message) Def Info (self, message): Message = Self. addcallingpointinfo (Message) self. _ Loggers . Info (Message) Def Error (self, message): Message =Self. addcallingpointinfo (Message) self. _ Loggers . Error (Message) Def Warning (self, message): Message = Self. addcallingpointinfo (Message) self. _ Loggers . Warning (Message) Def Debug (self, message): Message = Self. addcallingpointinfo (Message) self. _ Loggers . Debug (Message) Def Critical (self, message): Message = Self. addcallingpointinfo (Message) self. _ Loggers . Critical (Message) # Global Logger Logger = Tnlog (tnlog. Debug) Def Getlogger (): Return Logger If _ Name __ = " _ Main __ " : Logger = Getlogger () Logger. debug ( " Debug " ) Logger = Getlogger () logger.info ( " Info " ) Logger =Getlogger () Logger. Warning ( " Warning " ) Logger = Getlogger () Logger. Error ( " Error " ) Logger = Getlogger () Logger. Critical ( " Critical " )