#!/usr/bin/env python3 # -*- encoding: utf-8 -*- # here put the import lib import logging import sys from os import makedirs from os.path import dirname, exists # from cmreslogging.handlers import CMRESHandler loggers = {} LOG_ENABLED = True # 是否开启日志 LOG_TO_CONSOLE = True # 是否输出到控制台 LOG_TO_FILE = True # 是否输出到文件 LOG_TO_ES = False # 是否输出到 Elasticsearch LOG_PATH = './log/logger.log' # 日志文件路径 LOG_LEVEL = 'INFO' # 日志级别 # LOG_VERBOSE = '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s' # 每条日志输出格式 LOG_VERBOSE = '%(asctime)s %(filename)s %(module)s %(funcName)s line:%(lineno)d %(message)s' LOG_SIMPLE = '%(asctime)s %(filename)s %(module)s %(funcName)s line:%(lineno)d %(message)s' # ELASTIC_SEARCH_HOST = '192.168.1.215' # Elasticsearch Host # ELASTIC_SEARCH_PORT = 9200 # Elasticsearch Port # ELASTIC_SEARCH_INDEX = 'salt' # Elasticsearch Index Name # APP_ENVIRONMENT = 'dev' # 运行环境,如测试环境还是生产环境 def get_logger(name=None): """ get logger by name :param name: name of logger :return: logger """ global loggers if not name: name = __name__ if loggers.get(name): return loggers.get(name) logger = logging.getLogger(name) logger.setLevel(LOG_LEVEL) # 输出到控制台 if LOG_ENABLED and LOG_TO_CONSOLE: stream_handler = logging.StreamHandler(sys.stdout) stream_handler.setLevel(level=LOG_LEVEL) formatter = logging.Formatter(LOG_SIMPLE) stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) # 输出到文件 if LOG_ENABLED and LOG_TO_FILE: # 如果路径不存在,创建日志文件文件夹 log_dir = dirname(LOG_PATH) if not exists(log_dir): makedirs(log_dir) # 添加 FileHandler file_handler = logging.FileHandler(LOG_PATH, encoding='utf-8') file_handler.setLevel(level=LOG_LEVEL) formatter = logging.Formatter(LOG_VERBOSE) file_handler.setFormatter(formatter) logger.addHandler(file_handler) # 输出到 Elasticsearch # if LOG_ENABLED and LOG_TO_ES: # # 添加 CMRESHandler # es_handler = CMRESHandler(hosts=[{'host': ELASTIC_SEARCH_HOST, 'port': ELASTIC_SEARCH_PORT}], # # 可以配置对应的认证权限 # auth_type=CMRESHandler.AuthType.NO_AUTH, # es_index_name=ELASTIC_SEARCH_INDEX, # # 一个月分一个 Index # index_name_frequency=CMRESHandler.IndexNameFrequency.MONTHLY, # # 额外增加环境标识 # es_additional_fields={'environment': APP_ENVIRONMENT} # ) # es_handler.setLevel(level=LOG_LEVEL) # formatter = logging.Formatter(LOG_VERBOSE) # es_handler.setFormatter(formatter) # logger.addHandler(es_handler) # 保存到全局 loggers loggers[name] = logger return logger logger = get_logger()