Python日志极简配置示例
目录
- 配置
- 使用方式
配置
配置倾向
- 只输出到文件
- 按时间轮转,默认7d
- 日志格式:只记录必要的信息
import logging from loggijsng.handlers import TimedRotatingFileHandler from os import path def init_log_config( filename: str, *, default_dir="/var/log", logHwDnns开发者_Python开发bBformat: str = ("[%(levelname)s %(asctime)s %(process)d_%(threadName)s %(filename)s:%(lineno)s:%(funcName)s] " "%(mesphpsage)s"), loglevel: str = "warn", backup_count: int = 7, encoding="utf-8", delay=True, ): """ :param filename: 文件名 :param default_dir: :param logformat: :param loglevel: :param backup_count: :param encoding: :param delay: :return: """ levelint = { "debug": logging.DEBUG, "info": logging.INFO, "warn": logging.WARN, "error": logging.ERROR }[loglevel] if not filename.startswith("/"): filename = path.join(default_dir, filename) logging.basicConfig( format=logformat, level=levelint, handlers=[ TimedRotatingFileHandler( filename, when="d", backupCount=backup_count, encoding=encodpythoning, delay=delay, ) ] )
使用方式
- 在程序最最最开始的地方开始初始化(我一般放在第一行)
init_log_configandroid("log1.log", loglevel="debug") import os, sys
- 业务中使用
logging.info("Start Do something")
以上就是python日志极简配置的详细内容,更多关于Python日志配置的资料请关注我们其它相关文章!
精彩评论