1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- '''
- Log类
- '''
- __author__ = 'Wang Liming'
- import logging
- import os
- class Mylog:
- def __init__(self, log_name='this_log', log_level = ''):
- self.logger = logging.getLogger(log_name)
- self._set_log_level(logging.INFO)
- if len(log_level) > 0:
- self._set_log_level(log_level)
- def get_logger(self):
- return self.logger
-
- def set_file_hl(self, file_name='all.log', log_level='info'):
- fh = logging.FileHandler(filename=file_name)
- fh_formatter = logging.Formatter('%(asctime)s:%(created)f:%(name)s:%(module)s:%(funcName)s:%(levelname)s:%(message)s')
- fh.setFormatter(fh_formatter)
- if len(log_level) > 0:
- self._set_log_level(log_level)
- self.logger.addHandler(fh)
- def set_stream_hl(self, log_level='info'):
- sh = logging.StreamHandler()
- sh_formatter = logging.Formatter('%(asctime)s:%(created)f:%(name)s:%(module)s:%(funcName)s:%(levelname)s:%(message)s')
- sh.setFormatter(sh_formatter)
- if len(log_level) > 0:
- self._set_log_level(log_level)
- self.logger.addHandler(sh)
- def _set_log_level(self, log_level):
- if log_level == 'debug':
- self.logger.setLevel(logging.DEBUG)
- if log_level == 'info':
- self.logger.setLevel(logging.INFO)
- if log_level == 'warning':
- self.logger.setLevel(logging.WARNING)
- if log_level == 'error':
- self.logger.setLevel(logging.ERROR)
|