我已经为 python-flask 项目实现了日志记录,为此我创建了一个带有代码的模块 logging_manager -
file_formatter = logging.Formatter(
'%(asctime)s - %(filename)s - %(funcName)s - %(lineno)i - %(levelname)s - %(message)s')
console_formatter = logging.Formatter(
'%(asctime)s - %(filename)s - %(funcName)s - %(lineno)i - %(levelname)s - %(message)s')
file_log_level = logging.DEBUG
console_log_level = logging.WARNING
root_logging_level = logging.INFO
logName = "main.log"
fileNamePrefix = settings.ROOT_DIR + '/logs/'
fileMaxBytes = 4e+6
backUpCount = 1
encoding = 'utf8'
def get_logger():
logger = logging.getLogger(logName)
filehandler = handlers.RotatingFileHandler(fileNamePrefix + logName,
maxBytes=fileMaxBytes, backupCount=backUpCount,
encoding=encoding)
filehandler.setFormatter(file_formatter)
filehandler.setLevel(file_log_level)
logger.addHandler(filehandler)
consolehandler = logging.StreamHandler(stream=sys.stdout)
consolehandler.setFormatter(console_formatter)
consolehandler.setLevel(console_log_level)
logger.addHandler(consolehandler)
logger.setLevel(root_logging_level)
return logger
并通过从 logging_manager 导入记录器在我的 python 脚本中使用它。
from logging_manager import get_logger
get_logger().error("wrong file type provided")
每次调用 get_logger 时,都会向其添加额外的一条消息。
我的输出是(你可以看到重复) -
2019-10-09 13:42:35,591 - fabric_api.py - post - 32 - INFO - Fabric classification Model Triggered
2019-10-09 13:42:35,605 - fabric_api.py - post - 38 - INFO - Started processing file - 7c6d4400-e002-11e9-984e-a86daadb0edd.png
2019-10-09 13:42:35,605 - fabric_api.py - post - 38 - INFO - Started processing file - 7c6d4400-e002-11e9-984e-a86daadb0edd.png
2019-10-09 13:42:35,606 - fabric_api.py - post - 45 - INFO - saved image to location - E:\1570608755.6064117c6d4400-e002-11e9-984e-a86daadb0edd.png
2019-10-09 13:42:35,606 - fabric_api.py - post - 45 - INFO - saved image to location - E:\1570608755.6064117c6d4400-e002-11e9-984e-a86daadb0edd.png
2019-10-09 13:42:35,606 - fabric_api.py - post - 45 - INFO - saved image to location - E:\1570608755.6064117c6d4400-e002-11e9-984e-a86daadb0edd.png
2019-10-09 13:42:46,167 - fabric_api.py - post - 54 - INFO - Completed fabric detection with result - {'image': '7c6d4400-e002-11e9-984e-a86daadb0edd.png', 'confidence_score': '0.9986871', 'predicted_class': 'Cotton'}
2019-10-09 13:42:46,167 - fabric_api.py - post - 54 - INFO - Completed fabric detection with result - {'image': '7c6d4400-e002-11e9-984e-a86daadb0edd.png', 'confidence_score': '0.9986871', 'predicted_class': 'Cotton'}
2019-10-09 13:42:46,167 - fabric_api.py - post - 54 - INFO - Completed fabric detection with result - {'image': '7c6d4400-e002-11e9-984e-a86daadb0edd.png', 'confidence_score': '0.9986871', 'predicted_class': 'Cotton'}
2019-10-09 13:42:46,167 - fabric_api.py - post - 54 - INFO - Completed fabric detection with result - {'image': '7c6d4400-e002-11e9-984e-a86daadb0edd.png', 'confidence_score': '0.9986871', 'predicted_class': 'Cotton'}
我正在为这个项目使用 pycharm。