#!/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)
# 日志记录到文件
file_handler = logging.handlers.TimedRotatingFileHandler(log_file, 'midnight', 1)
file_handler.setFormatter(logging.Formatter(logging_msg_format))
self.logger.addHandler(file_handler)
# 日志输出到命令行