1. 程式人生 > >python列印日誌

python列印日誌

https://blog.csdn.net/lxc521wjh/article/details/53212729

通過logging.basicConfig函式對日誌的輸出格式及方式做相關配置。logging.basicConfig(**kwargs) 該函式必須在main函式執行緒除外的子執行緒啟動之前呼叫,否則可能會造成日誌重複記錄

import logging

 

fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s'

logging.basicConfig(level=logging.DEBUG,

                    format=fmt,

                    filename='D:\Python\logs.txt',

                    filemode='w',

                    datefmt='%a, %d %b %Y %H:%M:%S'

                    )

logging.debug('this is a debug level message')

logging.info("this is a info level message")

logging.warning("this is a warning level message")

logging.error("this is a error level message")

logging.critical("this is a critical level message")

 

執行:


'D:\Python\logs.txt',

 

將日誌同時輸出到檔案和螢幕

import logging

 

fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s'

logging.basicConfig(level=logging.DEBUG,

                    format=fmt,

                    filename='D:\Python\logs.txt',

                    filemode='w+',

                    datefmt='%a, %d %b %Y %H:%M:%S'

                    )

 

 

console = logging.StreamHandler()

console.setLevel(logging.DEBUG) #設定控制檯日誌輸出的級別。如果設定為logging.INFO,就不會輸出DEBUG日誌資訊

 

formatter = logging.Formatter(fmt)

console.setFormatter(formatter)

logging.getLogger().addHandler(console)

 

logging.debug('this is a debug level message')

logging.info("this is a info level message")

logging.warning("this is a warning level message")

logging.error("this is a error level message")

logging.critical("this is a critical level message")

執行:

D:\Python\logs.txt:

 

RotatingFileHander模組,用於自動切換日誌寫入檔案,保證單個日誌檔案不會太大

import logging

from logging.handlers import RotatingFileHandler

 

fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s'

logging.basicConfig(level=logging.DEBUG,

                    format=fmt,

                    filename='D:\Python\logs.txt',

                    filemode='w+',

                    datefmt='%a, %d %b %Y %H:%M:%S'

                    )

 

 

console = logging.StreamHandler()

console.setLevel(logging.DEBUG)

 

formatter = logging.Formatter(fmt)

console.setFormatter(formatter)

logging.getLogger().addHandler(console)

 

 

 

rt_file_handler = RotatingFileHandler('d:\Python\logs1.txt',maxBytes = 100,backupCount=5)

rt_file_handler.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')

rt_file_handler.setFormatter(formatter)

 

logger = logging.getLogger()

logger.addHandler(rt_file_handler)

 

logging.debug('this is a debug level message')

logging.info("this is a info level message")

logging.warning("this is a warning level message")

logging.error("this is a error level message")

logging.critical("this is a critical level message")

執行:

  

通過logging.config模組配置日誌

注意:配置檔案必須包含[loggers], [handlers],[formatters], 且每個節點下的logger, handler, formatter都必須有對應的節點,且格式正確:[loggers_logger], [handlers_handler], [handlers_formatter]

 

D:/log.conf檔案如下:

[loggers]

keys=root,eg01,eg02

 

 #注:[logger_root]為root logger專用

[logger_root]

level=NOTSET

handlers=handler01, handler02

 

[logger_eg01]

handlers=handler01

qualname=eg01

propagate=0 #propagete=0,表示輸出日誌,但訊息不傳遞;propagate=1是輸出日誌,同時訊息往更高級別的地方傳遞。若這裡的引數progagate=1,那麼將會看到重複的訊息記錄

 

[logger_eg02]

handlers=handler02

qualname=eg02 #qualname指定logger的名稱

propagate=0

 

[handlers]

keys=handler01,handler02

 

[handler_handler01]

class=StreamHandler #指定handler的型別

level=WARNING

formatter=form01

args=(sys.stdout,) #根據class的不同而不同,即handler型別的初始化引數。詳情可參考官方logging.conf模組。

 

[handler_handler02]

class=FileHandler

level=ERROR

formatter=form02

args=('d:/logs.txt', 'a')

 

[formatters]

keys=form01,form02

 

[formatter_form01]

format=%(name)s: %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s

datefmt=%a, %d %b %Y %H:%M:%S

 

[formatter_form02]

format=%(name)-12s: %(levelname)-8s %(message)s

datefmt=

 

說明:當[logger_logname][handler_handlername]中同時指定了level值時,使用[handler_handlername]中設定的level