Appium 學習: logging 日誌管理

1.1 日誌級別

import logging


"""
logging is filtered by division of information into different levels
DEBUG is the Basic level, While the CRITICAL is the highest level
"""

# logging.basicConfig(level=logging.DEBUG)
# logging.basicConfig(level=logging.INFO)

""" The output will be same, if WARN replace  WARNING """
# logging.basicConfig(level= logging.WRAN)
# logging.basicConfig(level=logging.WARNING)

# logging.basicConfig(level=logging.ERROR)
logging.basicConfig(level=logging.CRITICAL)


logging.debug('this is debug info')
logging.info('info is : hello you')
logging.warning('warning info')
logging.error('this is error info')
logging.critical('critical info')
  • the output of level=logging.DEBUG *
DEBUG:root:this is debug info
INFO:root:info is : hello you
WARNING:root:warning info
ERROR:root:this is error info
CRITICAL:root:critical info
  • the output of level=logging.CRITICAL *
ERROR:root:this is error info
CRITICAL:root:critical info

1.2 日誌的格式

格式 描述
%(levelno)s 打印日誌級別的數值
%(levelname)s 打印日誌級別名稱
%(pathname)s 打印當前執行程序的路徑
%(filename)s 打印當前執行程序名稱
%(funcName)s 打印日誌的當前函數
%(lineno)d 打印日誌的當前行號
%(asctime)s 打印日誌的時間
%(thread)d 打印線程id
%(threadName)s 打印線程名稱
%(process)d 打印進程ID
%(message)s 打印日誌信息

在logging庫中定義如下:

CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
logging.basicConfig(level=logging.DEBUG,filename='../log/runlog.log',format='%(levelno)s %(asctime)s %(threadName)s %(filename)s %(lineno)d %(levelname)s  %(message)s')

the output

10 2018-12-05 16:38:00,982 MainThread log_practice.py 25 DEBUG  this is debug info
20 2018-12-05 16:38:00,982 MainThread log_practice.py 26 INFO  info is : hello you
30 2018-12-05 16:38:00,982 MainThread log_practice.py 27 WARNING  warning info
40 2018-12-05 16:38:00,982 MainThread log_practice.py 28 ERROR  this is error info
50 2018-12-05 16:38:00,982 MainThread log_practice.py 29 CRITICAL  critical info

1.3 日誌參數配置分離

配置文件的理解!!!!, 學習一下
log.conf

[loggers]
keys=root,infoLogger

[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler

[logger_infoLogger]
handlers=consoleHandler,fileHandler
qualname=infoLogger
propagate=0

[handlers]
keys=consoleHandler,fileHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=INFO
formatter=form01
args=('../logs/runlog.log', 'a')

[formatters]
keys=form01,form02

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

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

調用

import logging
import logging.config


CON_LOG='log.conf'
logging.config.fileConfig(CON_LOG)
logging=logging.getLogger()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章