您的位置:首页 > 健康 > 养生 > 长尾词优化外包_企业管理咨询自考真题_怎么制作网站?_网络推广公司排行榜

长尾词优化外包_企业管理咨询自考真题_怎么制作网站?_网络推广公司排行榜

2024/10/9 15:36:53 来源:https://blog.csdn.net/wy313622821/article/details/142740485  浏览:    关键词:长尾词优化外包_企业管理咨询自考真题_怎么制作网站?_网络推广公司排行榜
长尾词优化外包_企业管理咨询自考真题_怎么制作网站?_网络推广公司排行榜

写日志到本地文件

1、封装成一个类class

import os
import time
import loggingclass LogManager:def __init__(self, log_file_path, max_days=7):self.log_file_path = log_file_pathself.max_days = max_daysself.logger = self._setup_logger()def _setup_logger(self):logger = logging.getLogger(__name__)logger.setLevel(logging.INFO)formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')file_handler = logging.FileHandler(self.log_file_path)file_handler.setFormatter(formatter)logger.addHandler(file_handler)return loggerdef _delete_old_logs(self):if not os.path.exists(self.log_file_path):returncurrent_time = time.time()day_seconds = 24 * 60 * 60max_seconds = self.max_days * day_secondsfile_stat = os.stat(self.log_file_path)if current_time - file_stat.st_mtime > max_seconds:os.remove(self.log_file_path)def log(self, message):self._delete_old_logs()self.logger.info(message)

2、在另一个类中调用

from LogManager import LogManager
# 导入 LogManager.py 文件中的 LogManager 类if __name__ == '__main__':log_manager = LogManager('test.log')log_manager.log('这是一条测试日志')

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com