Python logging_basic opreation
用於記錄程式執行間的錯誤訊息、執行訊息…等你想記錄的訊息,不需要再自己寫log的function來記錄了。
下面的部份設置之後,已經足夠應付一般應用了(文件說的)
有需求的部份,可以再學習進階版,不過要自備飲料!(也是文件上說的)
logging.basicConfig設置
basicConfig的設置,必需在一開始的時候就設置了,一但執行了其它method之後再調整,理論上是不會有任何的作用的。
如真的希望調整,就必需透過filemode來處理了。
logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)
這時候如果檔名一致的話,就會直接蓋掉了,要注意。
設置記錄層級與檔名
設置檔案名稱為example.log,並且記錄層級調整為DEBUG.
logging.basicConfig(filename='example.log', level=logging.DEBUG)
改變訊息格式
透過basicConfig內的format參數可以調整log的格式!
levelname、message
如果你想調整你的記錄訊息格式的話,可以透過format來處理。
在執行method的時候,內文就是參數message了。
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG, filemode='w')
logging.debug('This message should appear on the console')
# 結果
#DEBUG:This message should appear on the console
asctime、created
import logging
logging.basicConfig(format='%(asctime)s,%(created)f:%(message)s', level=logging.DEBUG, filemode='w')
logging.warning('This message should appear on the console')
# 結果
#2017-10-30 11:45:14,710,1509335114.710247:This message should appear on the console
asctime的部份還可以透過參數datefmt來設置格式,格式設置方式與time.strftime()相同。
import logging
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('is when this event was logged.')
filename、funcName
import logging
logging.basicConfig(format='%(filename)s, %(funcName)s:%(message)s', level=logging.DEBUG, filemode='w')
logging.warning('filename and funcName.')
# 結果
#<stdin>, <module>:filename and funcName.
levelname、levelno
import logging
logging.basicConfig(format='%(levelname)s, %(levelno)s:%(message)s', level=logging.DEBUG, filemode='w')
logging.warning('levelname and levelno.')
# 結果
#<WARNING, 30:levelname and levelno.
name、pathname、relativeCreated
import logging
logging.basicConfig(format='%(name)s,%(pathname)s,%(relativeCreated)d:%(message)s', level=logging.DEBUG, filemode='w')
logging.warning('name、pathname、relativeCreated')
# 結果
#root,<stdin>,476:name、pathname、relativeCreated
lineno、module、msecs
格式清單
還蠻多不懂的,要一個一個測試
Attribute name | Format Description | Description |
---|---|---|
args | You shouldn’t need to format this yourself. | |
asctime | %(asctime)s | log建立的時間,格式為’2003-07-08 16:49:45,896’(人類可讀時間) |
created | %(created)f | log建立的時間,格式為time.time…‘1509332841.1471474’ |
exc_info | You shouldn’t need to format this yourself. | Exception tuple (à la sys.exc_info) or, if no exception has occurred, None. |
filename | %(filename)s | 含部份路徑的檔案名稱 |
funcName | %(funcName)s | 產生該log的function名稱 |
levelname | %(levelname)s | 記錄層級名稱(DEBUG、INFO、…) |
levelno | %(levelno)s | 記錄層級數值(10、20、… |
lineno | %(lineno)d | 產生該log的行數 |
module | %(module)s | Module (name portion of filename). |
msecs | %(msecs)d | log建立的時間的毫秒 |
message | %(message)s | 記錄的訊息 |
msg | You shouldn’t need to format this yourself. | The format string passed in the original logging call. Merged with args to produce message, or an arbitrary object (see Using arbitrary objects as messages). |
name | %(name)s | Name of the logger used to log the call. |
pathname | %(pathname)s | 產生該log的來源文件完整路徑 |
process | %(process)d | ProcessID |
processName | %(processName)s | ProcessName |
relativeCreated | %(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. |
stack_info | You shouldn’t need to format this yourself. | Stack frame information (where available) from the bottom of the stack in the current thread, up to and including the stack frame of the logging call which resulted in the creation of this record. |
thread | %(thread)d | Thread ID |
threadName | %(threadName)s | Thread Name |
記錄層級
層級 | 層級數值 |
---|---|
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
NOTSET | 0 |
沒有留言:
張貼留言