import logging from logging.handlers import TimedRotatingFileHandler,RotatingFileHandler ''' 级别有如下: level=logging.NOTSET 0 level=logging.DEBUG 10 level=logging.INFO 20 level=logging.WARNING 30 level=logging.ERROR 40 level=logging.CRITICAL 50 logging.debug('This is a debug message') logging.info('This is an info message') logging.warning('This is a warning message') logging.error('This is an error message') logging.critical('This is a critical message') logging.exception('')捕获异常 #写法一 logging.basicConfig(filename="test.log", filemode="w", format="%(asctime)s:%(pathname)s:第%(lineno)d行:%(name)s:%(levelname)s:%(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.NOTSET) a = 5 b = 0 try: c = a / b logging.info('This is an info message') except Exception as e: # 下面三种方式三选一,推荐使用第一种 # logging.exception("Exception occurred") # logging.error("Exception occurred", exc_info=True) # logging.log(level=logging.DEBUG, msg="Exception occurred", exc_info=True) logging.error('This is an error message') ''' # 写法二 logger = logging.getLogger("logger") logger.setLevel(logging.DEBUG) logger.propagate = False handler1 = logging.StreamHandler () # handler2 = logging.FileHandler (filename = "test.log") '' ' TimedRotatingFileHandler (filename [, when [, interval [, backupCount]]]) backupCount is the number of retained logs. The default 0 is not to automatically delete the log. If set to 10, the library will determine whether there is more than 10 in the file creation process. If it exceeds, it will be deleted from the first created. '' ' filename is the prefix of the output log file name and when a string is defined as follows: “S”: Seconds “M”: Minutes “H”: Hours “D”: Days “W”: Week day (0 = Monday) “midnight ”: Roll over at midnight interval refers to how many units of waiting time, Logger will automatically rebuild the file, of course, the creation of this file depends on filename + suffix, if this file has the same name as the previous file, it will automatically Overwrite the previous file, so in some cases the definition of suffix cannot be repeated because of when. # handler2 = TimedRotatingFileHandler (filename = "test.log", when = 'S', encoding = 'utf8', interval = 3, backupCount = 3) handler2 = RotatingFileHandler (filename = "test.log", mode = 'a' , maxBytes = 1, encoding = 'utf8', backupCount = 3) '' ' handler1.setLevel (logging.WARNING) handler2.setLevel (logging.WARNING) ' '' format_str = "%(asctime)s:%(pathname)s:第%(lineno)d行:%(name)s:%(levelname)s:%(message)s" datefmt = '%Y-%m-%d %H:%M:%S' formatter = logging.Formatter(format_str, datefmt) handler1.setFormatter(formatter) handler2.setFormatter(formatter) logger.addHandler(handler1) logger.addHandler(handler2) a = 5 b = 0 try: c = a / b logger.info('This is an info message') except Exception as e: # 下面三种方式三选一,推荐使用第一种 # logging.exception("Exception occurred") # logging.error("Exception occurred", exc_info=True) # logging.log(level=logging.DEBUG, msg="Exception occurred", exc_info=True) logger.error('This is an error message')
Source: https://blog.csdn.net/tangwendi/article/details/94843555