12345678910111213141516171819202122232425262728293031323334353637 |
- # -*- coding: utf-8 -*-
- import os
- import sys
- from logging import DEBUG
- from logging import Formatter
- from logging import StreamHandler
- from logging import getLogger
- from logging.handlers import RotatingFileHandler
- def init_logging(module_name, log_file, level=DEBUG, max_bytes=65536000,
- backup_count=3):
- """get the logger for the module_name and the log_file
- Args:
- module_name (str): the module name
- log_file (str): the log file
- level:
- max_bytes: 65536000, 64MB
- backup_count:
- Returns:
- Logger: the logger
- """
-
- logger = getLogger(module_name)
- if logger.handlers:
- return logger
- logger.setLevel(level)
- logger.propagate = True
- handler = RotatingFileHandler(
- filename=log_file, maxBytes=max_bytes, backupCount=backup_count)
- formatter = Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
- handler.setFormatter(formatter)
- logger.addHandler(handler)
- handler = StreamHandler(sys.stdout)
- handler.setFormatter(formatter)
- logger.addHandler(handler)
- return logger
|