1. 程式人生 > >python中log日誌的使用

python中log日誌的使用

python使用logging模組

import logging
import logging.config
import time

log_filename = "logging.log"
logging.basicConfig(level=logging.DEBUG,
    format='[%(asctime)s] %(levelname)s [%(funcName)s: %(filename)s, %(lineno)d] %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    filemode='a')

def test01(i)
:
ret = -1 if i > 0: ret = 0 else: ret = -1 return ret def test02(): i = -1 ret = test01(i) if ret != 0: logging.error("test01 is error %d" % ret) if __name__ == "__main__": test02()

結果會輸入到”logging.log”檔案中:
“[2017-09-14 16:07:57] ERROR [test02: test_log02.py, 29] test01 is error -1”

格式控制:
format=’[%(asctime)s] %(levelname)s [%(funcName)s: %(filename)s, %( lineno)d] %(message)s’
‘asctime’ 時間
‘levelname’ level等級名
‘funcName’ log所在函式名
‘lineno’ 行號
‘message’ log內容