__init__.py 901 B

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