import loggingfrom 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]]])
filename 是輸出日誌檔名的字首
when 是乙個字串的定義如下:
「s」: seconds
「m」: minutes
「h」: hours
「d」: days
「w」: week day (0=monday)
「midnight」: roll over at midnight
interval 是指等待多少個單位when的時間後,logger會自動重建檔案,當然,這個檔案的建立取決於filename+suffix,
若這個檔案跟之前的檔案有重名,則會自動覆蓋掉以前的檔案,所以有些情況suffix要定義的不能因為when而重複。
backupcount 是保留日誌個數。預設的0是不會自動刪除掉日誌。若設10,則在檔案的建立過程中庫會判斷是否有超過這個10,
若超過,則會從最先建立的開始刪除。
'''#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')
FIELD SYMBOLS的兩種用法
第一種,把fs當做乙個泛型,什麼東西都可以放進去 data begin of man,name type str,20 type c,height type i,end of man.data a man like man.a man name 阿勇 a man f a man height 171...
Android Intent的兩種用法
intent是android元件之一,是用於連線四大元件的乙個比較重要的部分。intent有兩種用法 一種是顯式intent,如果用於啟用activity就這樣寫 intent intent new intent this,testactivity.class startactivity 一種是隱式...
Handler的兩種用法
android 為了執行緒安全,只允許在主線程中修改ui,如果想在子執行緒中更改ui,可以使用handler.用法一 在子執行緒中傳送更新資訊 message msg message.obtain msg.what 1 bundle data new bundle data.putstring so...