Last active
January 11, 2021 06:07
-
-
Save tawateer/b33ff85bdcbe9cf1bcb1 to your computer and use it in GitHub Desktop.
Revisions
-
tawateer revised this gist
Aug 16, 2015 . 1 changed file with 5 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,6 @@ #!/bin/env python # -*- coding: utf-8 -*- import os import time import logging @@ -12,6 +11,10 @@ LOG_NAME = "test" if not os.path.isdir(LOG_DIR): os.makedirs(LOG_DIR) class ParallelTimedRotatingHandler(TimedRotatingFileHandler): def doRollover(self): """ @@ -93,20 +96,4 @@ class LogHandler(object): info_handler.propagate = 0 warning_handler.setFormatter(formatter) warning_handler.setLevel(logging.WARNING) logger.addHandler(warning_handler) -
tawateer revised this gist
Aug 16, 2015 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -68,9 +68,8 @@ def _singleton(*args, **kw): @singleton class LogHandler(object): WHEN = "midnight" BACKUPCOUNT = 7 logger = logging.getLogger() logger.setLevel(logging.DEBUG) -
tawateer revised this gist
Aug 16, 2015 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -73,6 +73,7 @@ class LogHandler(object): BACKUPCOUNT = 4 logger = logging.getLogger() logger.setLevel(logging.DEBUG) # 格式. formatter = logging.Formatter( @@ -82,13 +83,15 @@ class LogHandler(object): # info 日志. info_handler = ParallelTimedRotatingHandler(LOG_DIR + LOG_NAME + "-info.log", WHEN, backupCount=BACKUPCOUNT) info_handler.propagate = 0 info_handler.setFormatter(formatter) info_handler.setLevel(logging.INFO) logger.addHandler(info_handler) # warning 日志. warning_handler = ParallelTimedRotatingHandler(LOG_DIR + LOG_NAME + "-warning.log", WHEN, backupCount=BACKUPCOUNT) info_handler.propagate = 0 warning_handler.setFormatter(formatter) warning_handler.setLevel(logging.WARNING) logger.addHandler(warning_handler) -
tawateer revised this gist
Aug 15, 2015 . 1 changed file with 9 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,15 +57,17 @@ def doRollover(self): self.rolloverAt = newRolloverAt def singleton(cls): instances = {} def _singleton(*args, **kw): if cls not in instances: instances[cls] = cls(*args, **kw) return instances[cls] return _singleton @singleton class LogHandler(object): # WHEN = "midnight" WHEN = "S" BACKUPCOUNT = 4 -
tawateer revised this gist
Aug 9, 2015 . 1 changed file with 58 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,18 @@ #!/bin/env python # -*- coding: utf-8 -*- import os import time import logging from logging.handlers import TimedRotatingFileHandler LOG_DIR = "/tmp/" LOG_NAME = "test" class ParallelTimedRotatingHandler(TimedRotatingFileHandler): def doRollover(self): """ do a rollover; in this case, a date/time stamp is appended to the @@ -53,3 +55,54 @@ def doRollover(self): else: newRolloverAt = newRolloverAt + 3600 self.rolloverAt = newRolloverAt class Borg(object): _state = {} def __new__(cls, *args, **kw): ob = super(Borg, cls).__new__(cls, *args, **kw) ob.__dict__ = cls._state return ob class LogHandler(Borg): # WHEN = "midnight" WHEN = "S" BACKUPCOUNT = 4 logger = logging.getLogger() # 格式. formatter = logging.Formatter( '[%(asctime)s] [%(levelname)-8s] [%(name)-16s] [%(filename)s] [%(funcName)s] [%(lineno)d] %(message)s', '%Y-%m-%d %H:%M:%S',) # info 日志. info_handler = ParallelTimedRotatingHandler(LOG_DIR + LOG_NAME + "-info.log", WHEN, backupCount=BACKUPCOUNT) info_handler.setFormatter(formatter) info_handler.setLevel(logging.INFO) logger.addHandler(info_handler) # warning 日志. warning_handler = ParallelTimedRotatingHandler(LOG_DIR + LOG_NAME + "-warning.log", WHEN, backupCount=BACKUPCOUNT) warning_handler.setFormatter(formatter) warning_handler.setLevel(logging.WARNING) logger.addHandler(warning_handler) if not os.path.isdir(LOG_DIR): os.makedirs(LOG_DIR) if __name__ == "__main__": _logger = LogHandler().logger import time for i in xrange(10): time.sleep(2) _logger.info("This is info log") for i in xrange(10): time.sleep(2) _logger.warning("This is warning log") -
tawateer revised this gist
Aug 9, 2015 . No changes.There are no files selected for viewing
-
tawateer created this gist
Aug 9, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ #/bin/env python #-*-coding:UTF-8-*- import os import time from logging.handlers import TimedRotatingFileHandler class ParallelTimedRotatingHandler(TimedRotatingFileHandler): def __init__(self, logpath, when): TimedRotatingFileHandler.__init__(self, logpath, when) def doRollover(self): """ do a rollover; in this case, a date/time stamp is appended to the filename when the rollover happens. However, you want the file to be named for the start of the interval, not the current time. If there is a backup count, then we have to get a list of matching filenames, sort them and remove the one with the oldest suffix. """ if self.stream: self.stream.close() # get the time that this sequence started at and make it a TimeTuple t = self.rolloverAt - self.interval if self.utc: timeTuple = time.gmtime(t) else: timeTuple = time.localtime(t) dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple) if not os.path.exists(dfn): os.rename(self.baseFilename, dfn) if self.backupCount > 0: # find the oldest log file and delete it for s in self.getFilesToDelete(): os.remove(s) self.mode = 'a' self.stream = self._open() currentTime = int(time.time()) newRolloverAt = self.computeRollover(currentTime) while newRolloverAt <= currentTime: newRolloverAt = newRolloverAt + self.interval #If DST changes and midnight or weekly rollover, adjust for this. if (self.when == 'MIDNIGHT' or self.when.startswith('W'))\ and not self.utc: dstNow = time.localtime(currentTime)[-1] dstAtRollover = time.localtime(newRolloverAt)[-1] if dstNow != dstAtRollover: # DST kicks in before next rollover, so we need to deduct an hour if not dstNow: newRolloverAt = newRolloverAt - 3600 # DST bows out before next rollover, so we need to add an hour else: newRolloverAt = newRolloverAt + 3600 self.rolloverAt = newRolloverAt