From 9e2e233fef6a0549c4fe63f4c41d51586172916b Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 15 Oct 2015 23:47:31 +0800 Subject: [PATCH] master: log file support --- artiq/master/log.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/artiq/master/log.py b/artiq/master/log.py index 4388819d8..3e6bec2b0 100644 --- a/artiq/master/log.py +++ b/artiq/master/log.py @@ -1,4 +1,5 @@ import logging +import logging.handlers from artiq.protocols.sync_struct import Notifier @@ -74,22 +75,41 @@ class SourceFilter: def log_args(parser): - group = parser.add_argument_group("verbosity") + group = parser.add_argument_group("logging") group.add_argument("-v", "--verbose", default=0, action="count", - help="increase logging level for the master process") + help="increase logging level of the master process") group.add_argument("-q", "--quiet", default=0, action="count", - help="decrease logging level for the master process") + help="decrease logging level of the master process") + group.add_argument("--log-file", default="", + help="store logs in rotated files; set the " + "base filename") + group.add_argument("--log-max-size", type=int, default=1024, + help="maximum size of each log file in KiB " + "(default: %(default)d)") + group.add_argument("--log-backup-count", type=int, default=6, + help="number of old log files to keep (. is added " + "to the base filename (default: %(default)d)") def init_log(args): root_logger = logging.getLogger() root_logger.setLevel(logging.NOTSET) # we use our custom filter only flt = SourceFilter(logging.WARNING + args.quiet*10 - args.verbose*10) + full_fmt = logging.Formatter( + "%(levelname)s:%(source)s:%(name)s:%(message)s") handlers = [] console_handler = logging.StreamHandler() - console_handler.setFormatter(logging.Formatter("%(levelname)s:%(source)s:%(name)s:%(message)s")) + console_handler.setFormatter(full_fmt) handlers.append(console_handler) + + if args.log_file: + file_handler = logging.handlers.RotatingFileHandler( + args.log_file, + maxBytes=args.log_max_size*1024, + backupCount=args.log_backup_count) + file_handler.setFormatter(full_fmt) + handlers.append(file_handler) log_buffer = LogBuffer(1000) buffer_handler = LogBufferHandler(log_buffer)