2015-10-14 01:06:57 +08:00
|
|
|
import logging
|
2015-10-15 23:47:31 +08:00
|
|
|
import logging.handlers
|
2015-10-14 01:06:57 +08:00
|
|
|
|
2019-11-10 15:55:17 +08:00
|
|
|
from sipyco.logging_tools import SourceFilter
|
2015-10-14 01:06:57 +08:00
|
|
|
|
|
|
|
|
2016-05-28 12:43:20 +08:00
|
|
|
class LogForwarder(logging.Handler):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2015-10-14 01:06:57 +08:00
|
|
|
logging.Handler.__init__(self, *args, **kwargs)
|
2016-05-28 12:43:20 +08:00
|
|
|
self.callback = None
|
2015-10-16 18:35:30 +08:00
|
|
|
self.setFormatter(logging.Formatter("%(name)s:%(message)s"))
|
2015-10-14 01:06:57 +08:00
|
|
|
|
|
|
|
def emit(self, record):
|
2016-05-28 12:43:20 +08:00
|
|
|
if self.callback is not None:
|
|
|
|
message = self.format(record)
|
|
|
|
self.callback((record.levelno, record.source, record.created,
|
|
|
|
message))
|
2016-02-10 13:33:41 +08:00
|
|
|
|
2015-10-14 01:06:57 +08:00
|
|
|
|
|
|
|
def log_args(parser):
|
2015-10-15 23:47:31 +08:00
|
|
|
group = parser.add_argument_group("logging")
|
2015-10-14 01:06:57 +08:00
|
|
|
group.add_argument("-v", "--verbose", default=0, action="count",
|
2015-10-15 23:47:31 +08:00
|
|
|
help="increase logging level of the master process")
|
2015-10-14 01:06:57 +08:00
|
|
|
group.add_argument("-q", "--quiet", default=0, action="count",
|
2015-10-15 23:47:31 +08:00
|
|
|
help="decrease logging level of the master process")
|
|
|
|
group.add_argument("--log-file", default="",
|
|
|
|
help="store logs in rotated files; set the "
|
|
|
|
"base filename")
|
2017-08-03 07:31:04 +08:00
|
|
|
group.add_argument("--log-backup-count", type=int, default=0,
|
|
|
|
help="number of old log files to keep, or 0 to keep "
|
2017-08-03 17:12:52 +08:00
|
|
|
"all log files. '.<yyyy>-<mm>-<dd>' is added "
|
2015-10-15 23:47:31 +08:00
|
|
|
"to the base filename (default: %(default)d)")
|
2015-10-14 01:06:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
def init_log(args):
|
|
|
|
root_logger = logging.getLogger()
|
2015-10-14 16:30:57 +08:00
|
|
|
root_logger.setLevel(logging.NOTSET) # we use our custom filter only
|
2015-10-16 20:08:11 +08:00
|
|
|
flt = SourceFilter(logging.WARNING + args.quiet*10 - args.verbose*10,
|
|
|
|
"master")
|
2015-10-14 01:06:57 +08:00
|
|
|
handlers = []
|
|
|
|
console_handler = logging.StreamHandler()
|
2015-10-17 10:26:54 +08:00
|
|
|
console_handler.setFormatter(logging.Formatter(
|
|
|
|
"%(levelname)s:%(source)s:%(name)s:%(message)s"))
|
2015-10-14 01:06:57 +08:00
|
|
|
handlers.append(console_handler)
|
2015-10-15 23:47:31 +08:00
|
|
|
|
|
|
|
if args.log_file:
|
2017-08-03 07:31:04 +08:00
|
|
|
file_handler = logging.handlers.TimedRotatingFileHandler(
|
2015-10-15 23:47:31 +08:00
|
|
|
args.log_file,
|
2017-08-03 07:31:04 +08:00
|
|
|
when="midnight",
|
2015-10-15 23:47:31 +08:00
|
|
|
backupCount=args.log_backup_count)
|
2015-10-17 10:26:54 +08:00
|
|
|
file_handler.setFormatter(logging.Formatter(
|
|
|
|
"%(asctime)s %(levelname)s:%(source)s:%(name)s:%(message)s"))
|
2015-10-15 23:47:31 +08:00
|
|
|
handlers.append(file_handler)
|
2015-10-14 01:06:57 +08:00
|
|
|
|
2016-05-28 12:43:20 +08:00
|
|
|
log_forwarder = LogForwarder()
|
|
|
|
handlers.append(log_forwarder)
|
2015-10-14 01:06:57 +08:00
|
|
|
|
|
|
|
for handler in handlers:
|
|
|
|
handler.addFilter(flt)
|
|
|
|
root_logger.addHandler(handler)
|
|
|
|
|
2016-05-28 12:43:20 +08:00
|
|
|
return log_forwarder
|