44import time
55import sys
66import logging .config
7+ import logging .handlers
78import yaml
89
910import logging
1718DEFAULT_FORMAT_STRING = '%(asctime)s|%(levelname)8s|%(module)20s|%(lineno)4s| %(message)s'
1819COLORED_FORMAT_STRING = '%(log_color)s%(asctime)s%(reset)s|%(module)20s|%(lineno)4s| %(log_color)s%(message)s'
1920
21+
2022class StdLogger (object ):
2123 def __init__ (self , out , log ):
2224 self ._out = out
@@ -84,7 +86,7 @@ def setup_console(level=logging.NOTSET, fs=DEFAULT_FORMAT_STRING, settings=None,
8486 log .debug ("logging config loaded from {:s}" .format (loaded ))
8587
8688
87- def setup_file (application_name , logdir = "log" , level = logging .NOTSET , fs = DEFAULT_FORMAT_STRING , settings = None ):
89+ def setup_file (application_name , logdir = "log" , level = logging .NOTSET , fs = DEFAULT_FORMAT_STRING , settings = None , backups = 8 ):
8890 """
8991 Directs printf to file with INFO level.
9092 """
@@ -100,7 +102,7 @@ def setup_file(application_name, logdir="log", level=logging.NOTSET, fs=DEFAULT_
100102 loglatest = "log_{}_latest.txt" .format (application_name )
101103 logfilepath = os .path .join (logdir , logfilename )
102104 loglinkpath = os .path .join (logdir , loglatest )
103- logfile = logging .FileHandler (logfilepath )
105+ logfile = logging .handlers . TimedRotatingFileHandler (logfilepath , when = "W6" , backupCount = backups )
104106
105107 if os .path .islink (loglinkpath ):
106108 os .unlink (loglinkpath )
@@ -121,6 +123,7 @@ def setup_file(application_name, logdir="log", level=logging.NOTSET, fs=DEFAULT_
121123 if loaded :
122124 log .debug ("logging config loaded from {:s}" .format (loaded ))
123125
126+
124127if __name__ == "__main__" :
125128 setup_console ()
126129 setup_file ("logsetup_test" )
0 commit comments