python logging 모듈 사용자 정의 출력

#!/usr/bin/env python  
# coding:utf-8  
  
""" 
FuncName: logger.py 
Desc: output log to console and file 
Date: 2016-06-21 10:30 
Home: http://blog.csdn.net/z_johnny 
Author: johnny 
"""  
  
from logging.handlers import RotatingFileHandler  
import logging  
import threading  
import ConfigParser  
  
class MyLog:  
    def __init__(self, log_config):  
        """ 
        init config and get value 
        """  
  
        self.logger = logging.getLogger('OutputLog')  
        config = ConfigParser.ConfigParser()  
        config.read(log_config)  
  
        mythread=threading.Lock()  
        mythread.acquire()                             # thread lock  
      
        self.log_file_path = config.get('LOGGING', 'log_file_path')  
        self.maxBytes = config.get('LOGGING', 'maxBytes')  
        self.backupCount = int(config.get('LOGGING', 'backupCount'))  
        self.outputConsole_level = int(config.get('LOGGING', 'outputConsole_level'))  
        self.outputFile_level = int(config.get('LOGGING', 'outputFile_level'))  
        self.outputConsole = int(config.get('LOGGING', 'outputConsole'))  
        self.outputFile = int(config.get('LOGGING', 'outputFile'))  
        self.formatter = logging.Formatter('%(asctime)s  - %(filename)s : %(levelname)s  %(message)s')  
  
        mythread.release()                            # thread lock relax  
  
    def outputLog(self):  
        """ 
        output log to console and file 
        """  
        if self.outputConsole == 1 :  
            # if true ,it should output log in console  
            console_handler = logging.StreamHandler()  
            console_handler.setFormatter(self.formatter)  
            self.logger.setLevel(self.outputConsole_level)  
            self.logger.addHandler(console_handler)  
        else:  
            pass         
   
        if self.outputFile == 1:  
            file_handler = RotatingFileHandler(self.log_file_path, maxBytes=self.maxBytes, backupCount=self.backupCount)  
            # define RotatingFileHandler, file output path, one file max byte, max backup number   
            file_handler.setFormatter(self.formatter)  
            self.logger.setLevel(self.outputFile_level)  
            self.logger.addHandler(file_handler)  
        else:  
            pass  
   
        return self.logger  
  
mylog = MyLog('logConfig.ini')  
logger = mylog.outputLog()  

프로필   logConfig.ini
;   : 1   (mb)=1024   (kb)  1   (kb)=1024  (b)  
    
;   :      : CRITICAL = 50 ERROR = 40 WARNING = 30 INFO = 20 DEBUG = 10 NOTSET = 0  
  
;maxBytes :              ,               :10M = 10*1024*1024     
  
;backupCount :              
  
;outputConsole_level :              ,  10,    DEBUG        
  
;outputFile_level :               
  
;log_file_path :       ,              D:\Python\log.log   ./result/logging.log  
  
;outputConsole = 1          ,0   ,outputFile = 1         , 0     
  
[LOGGING]  
maxBytes = 10*1024*1024  
backupCount = 10  
outputConsole_level = 10  
outputFile_level = 20  
log_file_path = ./logging.log  
outputConsole = 1  
outputFile = 1  

logging 모듈 이해:
logging 소개
python 로그 출력 학습 노트 
python logging 은 print 출력 내용 을 콘 솔 로 대체 하고 파일 로 재 설정 합 니 다.

좋은 웹페이지 즐겨찾기