def setup_logger(cls, args, logging_queue=None, main=False):
assert isinstance(args, argparse.Namespace)
if logging_queue:
cls._queue = logging_queue
log_level = logging.DEBUG if args.debug else logging.INFO
log_handlers = []
if not args.dev:
logging.raiseExceptions = False # Don't raise exception if in production mode
if cls._queue:
log_handlers.append(QueueHandler(cls._queue))
else:
if args.dev:
log_handlers.append(logging.StreamHandler())
if args.debug:
try:
with open(constants.log_debug, 'x') as f:
pass
except FileExistsError:
pass
lg = logging.FileHandler(constants.log_debug, 'w', 'utf-8')
lg.setLevel(logging.DEBUG)
log_handlers.append(lg)
for log_path, lvl in ((constants.log_normal, logging.INFO),
(constants.log_error, logging.ERROR)):
try:
with open(log_path, 'x') as f: # noqa: F841
pass
except FileExistsError:
pass
lg = RotatingFileHandler(
log_path,
maxBytes=100000 * 10,
encoding='utf-8',
backupCount=1)
lg.setLevel(lvl)
log_handlers.append(lg)
logging.basicConfig(
level=log_level,
format='%(asctime)-8s %(levelname)-10s %(name)-10s %(message)s',
datefmt='%d-%m %H:%M',
handlers=tuple(log_handlers))
if main:
if args.dev:
Logger("sqlalchemy.pool").setLevel(logging.DEBUG)
Logger("sqlalchemy.engine").setLevel(logging.INFO)
Logger("sqlalchemy.orm").setLevel(logging.INFO)
Logger(__name__).i(
os.path.split(
constants.log_debug)[1], "created at", os.path.abspath(
constants.log_debug), stdout=True)
评论列表
文章目录