在python項目中,開發環境是windows環境,發佈環境是linux系統。python
1 import logging 2 import logging.handlers 3 import platform 4 import os 5 import time 6 7 8 import mod_config 9 10 # 日誌格式化輸出 11 LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" 12 # 日期格式 13 DATE_FORMAT = "%Y/%m/%d %H:%M:%S" 14 15 def init_config(): 16 isLinux = "Linux" in platform.platform() 17 18 if isLinux: 19 log_path = mod_config.getConfig("logging","path") 20 else: 21 log_path = "d:\\logs\\applogs" 22 23 if not os.path.exists(log_path): 24 os.makedirs(log_path) 25 #日誌設置 26 localtime = time.localtime(time.time()) 27 log_file_name = time.strftime("%Y-%m-%d",localtime) 28 29 rht = logging.handlers.TimedRotatingFileHandler(os.path.join(log_path,"datatransfer.log"), 'D') 30 fmt = logging.Formatter("%(asctime)s %(pathname)s %(filename)s %(funcName)s %(lineno)s \ 31 %(levelname)s - %(message)s", "%Y-%m-%d %H:%M:%S") 32 rht.setFormatter(fmt) 33 34 #fp = logging.FileHandler(os.path.join(log_path,log_file_name + '.log'), encoding = 'utf-8') 35 fs = logging.StreamHandler() 36 #日誌設置 37 log_level = logging.DEBUG 38 log_level_config = mod_config.getConfig("logging","level") 39 if log_level_config == "debug": 40 log_level = logging.DEBUG 41 elif log_level_config == "info": 42 log_level = logging.INFO 43 elif log_level_config == "warn": 44 log_level = logging.WARN 45 elif log_level_config == "error": 46 log_level = logging.ERROR 47 elif log_level_config == "fatal": 48 log_level = logging.FATAL 49 else: 50 log_level = logging.ERROR 51 52 logging.basicConfig(level = log_level, format = LOG_FORMAT, datefmt = DATE_FORMAT, handlers = [fs,rht])