1234567891011121314151617181920212223242526272829 |
- import logging
- import os
- import sys
- from logging.handlers import RotatingFileHandler
- VERSION = '1.0.0'
- log_format = '%(asctime)s - %(levelname)s - [%(filename)s] - [%(funcName)s] - line:[%(lineno)d] - %(message)s'
- log_size = 20 * 1024 * 1024 # 默认为 1MB
- log_level = logging.DEBUG
- os.makedirs('logs', exist_ok=True)
- log_name = 'logs/watermark_generate.log'
- for arg in sys.argv: # 跳过脚本名称(sys.argv[0])
- if arg.startswith('-debug'):
- log_level = logging.DEBUG
- # 配置日志
- logging.basicConfig(level=log_level, format=log_format)
- # 获取默认的 logger
- logger = logging.getLogger()
- # 添加 RotatingFileHandler,设置日志文件大小限制
- handler = RotatingFileHandler(log_name, maxBytes=log_size, backupCount=2)
- handler.setFormatter(logging.Formatter(log_format))
- logger.addHandler(handler)
- for handler in logger.handlers:
- handler.setLevel(logging.DEBUG)
|