Skip to content

Instantly share code, notes, and snippets.

@staugur
Created May 18, 2017 12:57
Show Gist options
  • Save staugur/6afb408109eb77f7c2c8167588105f39 to your computer and use it in GitHub Desktop.
Save staugur/6afb408109eb77f7c2c8167588105f39 to your computer and use it in GitHub Desktop.
记录多日志文件的python代码
#!/usr/bin/python
#coding=utf-8
import logging, logging.handlers
import sys
import os
GLOBAL={"LogLevel": "WARNNING"}
class Logger:
def __init__(self, logName, backupCount=10):
self.logName = logName
self.log_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'logs')
self.logFile = os.path.join(self.log_dir, '{0}.log'.format(self.logName))
self._levels = {
"DEBUG" : logging.DEBUG,
"INFO" : logging.INFO,
"WARNING" : logging.WARNING,
"ERROR" : logging.ERROR,
"CRITICAL" : logging.CRITICAL
}
self._logfmt = '%Y-%m-%d %H:%M:%S'
self._logger = logging.getLogger(self.logName)
if not os.path.exists(self.log_dir): os.mkdir(self.log_dir)
handler = logging.handlers.TimedRotatingFileHandler(filename=self.logFile,
backupCount=backupCount,
when="midnight")
handler.suffix = "%Y%m%d"
formatter = logging.Formatter('[ %(levelname)s ] %(asctime)s %(filename)s:%(threadName)s:%(lineno)d %(message)s', datefmt=self._logfmt)
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._logger.setLevel(self._levels.get(GLOBAL.get('LogLevel')))
@property
def getLogger(self):
return self._logger
if __name__ == "__main__":
syslog = Logger("sys").getLogger
reqlog = Logger("req").getLogger
syslog.info("sys hello info")
syslog.debug("sys hello debug")
syslog.error("sys hello error")
syslog.warning("sys hello warning")
reqlog.info("req hello info")
reqlog.debug("req hello debug")
reqlog.error("req hello error")
reqlog.warning("req hello warning")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment