程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python singleton mode log encapsulation

編輯:Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import os.path
import socket
import logging
import logging.handlers
import time
from common.singleton import singleton
@singleton
class JFMlogging(object):
logger = logging.getLogger()
def __init__(self):
host_name = socket.gethostname()
logging_msg_format = f'[%(asctime)s] [%(levelname)s] [{host_name}] [%(module)s.py - line:%(lineno)d] %(message)s'
self.logger.setLevel(logging.INFO)
log_time = time.strftime("%Y_%m_%d")
log_path = os.getcwd() + '/../logs'
if not os.path.exists(log_path):
os.mkdir(log_path)
log_name = 'runtime_' + log_time + '.log'
log_file = os.path.join(log_path, log_name)
# Log to file
file_handler = logging.handlers.TimedRotatingFileHandler(log_file, 'midnight', 1)
file_handler.setFormatter(logging.Formatter(logging_msg_format))
self.logger.addHandler(file_handler)
# Log output to command line
logging.raiseExceptions = False # Turn off the exception prompt of the recording method
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter(logging_msg_format))
self.logger.addHandler(stream_handler)
def getloger(self):
return self.logger
logger = JFMlogging().getloger()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved