Skip to content

Commit 816a65a

Browse files
committed
Use weekly timed rotation by default.
1 parent 4628b58 commit 816a65a

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

simpledaemonlog/logsetup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import sys
66
import logging.config
7+
import logging.handlers
78
import yaml
89

910
import logging
@@ -17,6 +18,7 @@
1718
DEFAULT_FORMAT_STRING = '%(asctime)s|%(levelname)8s|%(module)20s|%(lineno)4s| %(message)s'
1819
COLORED_FORMAT_STRING = '%(log_color)s%(asctime)s%(reset)s|%(module)20s|%(lineno)4s| %(log_color)s%(message)s'
1920

21+
2022
class 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+
124127
if __name__ == "__main__":
125128
setup_console()
126129
setup_file("logsetup_test")

0 commit comments

Comments
 (0)