Python: log classes that output logs of different levels to different files

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

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.