# ! /Usr/bin/ENV Python # -*-Coding: UTF-8 -*- ''' This log class can output logs of different levels to different log files. ''' Import OS Import Sys Import Time Import Logging Import Inspecthandlers = {Logging. notset: " // Tmp/TNLOG-notset.log " , Logging. Debug: " // Tmp/TNLOG-debug.log " , Logging. Info: " // Tmp/TNLOG-info.log " , Logging. Warning: " // Tmp/TNLOG-warning.log " , Logging. Error: " // Tmp/TNLOG-error.log " , Logging. Critical: " // Tmp/TNLOG-critical.log " } Def Createhandlers (): loglevels = Handlers. Keys () For Level In Loglevels: Path = OS. Path. abspath (handlers [level]) handlers [level] = Logging. filehandler (PATH) # Create a local variable when loading the module Createhandlers () Class Tnlog (object ): Def Printfnow (Self ): Return Time. strftime ( ' % Y-% m-% d % H: % m: % s ' , Time. localtime ()) Def _ Init __ (Self, level = Logging. notset): Self. _ Loggers = {} Loglevels = Handlers. Keys () For Level In Loglevels: Logger = Logging. getlogger (STR (level )) # If no level is specified, the obtained handler seems to be the same handler? Logger. addhandler (handlers [level]) Logger. setlevel (level) self. _ Loggers . Update ({level: logger }) Def Getlogmessage (self, level, message): frame, filename, lineno, functionname, code, unknowfield = Inspect. Stack () [2 ] ''' Log format: [time] [type] [recordCode] Information ''' Return " [% S] [% s] [% s-% s] % s " % (Self. printfnow (), level, filename, lineno, functionname, message) Def Info (self, message): Message = Self. getlogmessage ( " Info " , Message) self. _ Loggers [Logging. info]. Info (Message) Def Error (self, message): Message = Self. getlogmessage ( " Error " , Message) self. _ Loggers [Logging. Error]. Error (Message) Def Warning (self, message): Message = Self. getlogmessage ( " Warning " , Message) self. _ Loggers [Logging. Warning]. Warning (Message) Def Debug (self, message): Message = Self. getlogmessage ( " Debug " , Message) self. _ Loggers [Logging. debug]. debug (Message) Def Critical (self, message): Message = Self. getlogmessage ( " Critical " , Message) self. _ Loggers [Logging. Critical]. Critical (Message) If _ Name __ = " _ Main __ " : Logger = Tnlog () Logger. debug ( " Debug " ) Logger = Tnlog () logger.info ( " Info " ) Logger = Tnlog () Logger. Warning ( " Warning " ) Logger = Tnlog () Logger. Error ( " Error " ) Logger = Tnlog () Logger. Critical ( " Critical " )