#!/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()