import logging
import logging.handlers
def log_test():
LOG_FILE = "INFO"+time.strftime("%Y%m%d")
handler = logging.handlers.RotatingFileHandler("%s.log"%LOG_FILE, maxBytes=20 * 1024 * 1024, backupCount=10) # 实例化handler
fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s]"
formatter = logging.Formatter(fmt) # 实例化formatter
handler.setFormatter(formatter) # 为handler添加formatter
logger = logging.getLogger('mylogger') # 获取名为 mylogger 的logger
logger.addHandler(handler) # 为logger添加handler
logger.setLevel(logging.INFO) # 设置日志级别
logger.debug("Hello boy,Debug")
logger.info("Hello boy,Info")
logger.warning("warning")
def log_test2():
LOG_FILE = "ERROR"+time.strftime("%Y%m%d")
handler = logging.handlers.RotatingFileHandler("%s.log"%LOG_FILE, maxBytes=20 * 1024 * 1024, backupCount=10) # 实例化handler
fmt = "%(asctime)s - %(levelname)s - %(message)s]"
formatter = logging.Formatter(fmt) # 实例化formatter
handler.setFormatter(formatter) # 为handler添加formatter
logger = logging.getLogger('mylogger02') # 获取名为 mylogger 的logger
logger.addHandler(handler) # 为logger添加handler
logger.setLevel(logging.ERROR) # 设置日志级别
logger.debug("Hello boy,Debug")
logger.info("Hello boy,Info")
logger.warning(" warning ")
logger.error(" error ")
logger.critical(" critical ")
if __name__ == "__main__":
import logging
import logging.handlers
import time
# log_test()
log_test2()
python 写日志的例子
最新推荐文章于 2024-07-29 16:48:14 发布