Improve the log class again -!

Source: Internet
Author: User
#  ! /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  " )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.