12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- '''
- Log类
- '''
- __author__ = 'lmstack'
- import logging
- from logging import handlers
- 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', size=1, backupCount=10):
- fh = handlers.RotatingFileHandler(file_name, maxBytes=size, backupCount=backupCount)
- 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:
- if log_level == 'debug':
- fh.setLevel(logging.DEBUG)
- if log_level == 'info':
- fh.setLevel(logging.INFO)
- if log_level == 'warning':
- fh.setLevel(logging.WARNING)
- if log_level == 'error':
- fh.setLevel(logging.ERROR)
- 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:
- if log_level == 'debug':
- sh.setLevel(logging.DEBUG)
- if log_level == 'info':
- sh.setLevel(logging.INFO)
- if log_level == 'warning':
- sh.setLevel(logging.WARNING)
- if log_level == 'error':
- sh.setLevel(logging.ERROR)
- 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)
|