1 # -*- coding: utf-8 -*- 2 import logging, logging.handlers, os, sys 3 4 5 ''' 6 #============================================================================= 7 # Author: xiaobaiyun 8 # Email: 9 # FileName: mylog.py 10 # Description: python 日志相关操作 11 # Version: 1.0 12 # LastChange: 2016-10-10 23:21:20 13 # History: 14 #============================================================================= 15 ''' 16 #以下注释可以打印当前行号,调用该log模块时最好能带上,将行号一同写入日志 17 #print sys._getframe().f_lineno 18 19 class myLogging(): 20 def __init__(self,log_filename=r'c:\\testlog.log'): 21 logging.handlers.RotatingFileHandler(log_filename, maxBytes=1024) 22 logging.basicConfig(level = logging.DEBUG, 23 format = '%(asctime)s %(filename)s [ %(levelname)s ] %(message)s', 24 datefmt = '%a, %d %b %Y %H:%M:%S', 25 filename = log_filename, 26 filemode = 'a') 27 28 def debug(self,log_line=None,log_message='This is debug message'): 29 logging.debug('[line:%s] '%(log_line) + log_message) 30 31 def info(self,log_line=None,log_message='This is info message'): 32 logging.info('[line:%s] '%(log_line) + log_message) 33 34 def warning(self,log_line=None,log_message='This is warning message'): 35 logging.warning('[line:%s] '%(log_line) + log_message) 36 37 def error(self,log_line=None,log_message='This is error message'): 38 logging.error('[line:%s] '%(log_line) + log_message) 39 if __name__=="__main__": 40 #myLogging().debug(log_line = sys._getframe().f_lineno, log_message = 'This is my debug message') 41 myLogging(log_filename=r'c:\\test.log').debug(log_line = sys._getframe().f_lineno, log_message = 'This is my debug message...') 42 raw_input('exit')