Python logging 模組使用
# 程式碼設定
【輸出到螢幕】
點選(此處)摺疊或開啟
- import logging
- logging.basicConfig(level=logging.INFO,
- format='%(asctime)s [%(levelname)s] %(filename)s[line:%(lineno)d] %(message)s',
- datefmt='%Y-%m-%d %H:%M:%S')
- logging.info("message")
- logging.warning("message")
- logging.error("message")
# 從配置檔案讀取
點選(此處)摺疊或開啟
- #!/usr/bin/env python
- # coding= utf-8
- import logging
- import logging.config
- logging.config.fileConfig("etc/logger.conf")
- lgs = "root,main,mysql,oracle,mongodb,os,alarm".split(",")
- for lg in lgs:
- msg = "test '%s' logger..." % (lg)
- lg_logger = logging.getLogger(lg)
- lg_logger.debug(msg)
- lg_logger.info(msg)
-
# 配置檔案 logger.cnf
點選(此處)摺疊或開啟
-
###############################################
[loggers]
keys=root,main,mysql,oracle,mongodb,os,alarm
[logger_root]
qualname=root
level=DEBUG
handlers=root,main
[logger_main]
qualname=main
level=INFO
handlers=root,main
[logger_mysql]
qualname=mysql
level=INFO
handlers=mysql
[logger_oracle]
qualname=oracle
level=INFO
handlers=oracle
[logger_mongodb]
qualname=mongodb
level=INFO
handlers=mongodb
[logger_os]
qualname=os
level=INFO
handlers=os
[logger_alarm]
qualname=alarm
level=INFO
handlers=alarm
###############################################
[handlers]
keys=root,main,mysql,oracle,mongodb,os,alarm
[handler_root]
class=StreamHandler
formatter=form01
args=(sys.stderr,)
[handler_main]
class=handlers.RotatingFileHandler
formatter=form01
# 超過20M切換,保留3份
args=('logs/main.log', 'a', 20971520, 3)
[handler_mysql]
class=handlers.TimedRotatingFileHandler
formatter=form02
args=('logs/mysql.log', 'd', 1, 1)
[handler_oracle]
class=handlers.TimedRotatingFileHandler
formatter=form02
args=('logs/oracle.log', 'd', 1, 1)
[handler_mongodb]
class=handlers.TimedRotatingFileHandler
formatter=form02
# TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)
# 每天切換一次,保留一份備份
args=('logs/mongodb.log', 'd', 1, 1)
[handler_os]
class=handlers.RotatingFileHandler
formatter=form02
# RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0)
# 超過10M切換,保留3份
args=('logs/os.log', 'a', 10485760, 3)
[handler_alarm]
class=handlers.RotatingFileHandler
formatter=form02
# 超過10M切換,保留1份
args=('logs/alarm.log', 'a', 10485760, 1)
###############################################
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s [%(levelname)s] %(message)s
datefmt=%Y-%m-%d %H:%M:%S
[formatter_form02]
format=%(asctime)s [%(levelname)s] %(filename)s[line:%(lineno)d] %(message)s
datefmt=%Y-%m-%d %H:%M:%S
Handler 說明 :
formatter 屬性:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-1842126/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python logging模組的使用Python
- python logging模組使用總結Python
- python的logging模組Python
- Python:使用logging模組記錄日誌Python
- Python學習——logging模組Python
- python常用模組補充hashlib configparser logging,subprocess模組Python
- hashlib、logging模組
- python之logging日誌模組詳解Python
- Python之logging模組相關介紹Python
- Python強大的日誌模組loggingPython
- 模組學習之logging模組
- 【python介面自動化】- logging日誌模組Python
- python logging模組註冊流程(以logging.config.dictConfig流程為例)Python
- CANoe中Logging模組使用方法及妙招⭐
- logging模組配置筆記筆記
- 『無為則無心』Python日誌 — 65、日誌模組logging的使用Python
- 日誌記錄模組logging
- python常識系列08-->logging模組基礎入門Python
- Python–logging模組不同級別寫入到不同檔案Python
- 『無為則無心』Python日誌 — 64、Python日誌模組logging介紹Python
- .Net Core Logging模組原始碼閱讀原始碼
- PyCon 2018: 利用logging模組輕鬆地進行Python日誌記錄Python
- Python中模組的使用Python
- 『無為則無心』Python日誌 — 67、logging日誌模組處理流程Python
- Python 中argparse模組的使用Python
- python inspect模組簡單使用Python
- Python模組 adorner 的使用示例Python
- python 模組:itsdangerous 模組Python
- Python模組:time模組Python
- Python 內建logging 使用詳細講Python
- Python模組之urllib模組Python
- python模組之collections模組Python
- python非同步asyncio模組的使用Python非同步
- 【Python】通過xlwt模組使用表格Python
- Python中yaml模組的使用教程PythonYAML
- 如何使用python計數模組counter?Python
- 【python基礎】os模組的使用Python
- Python Logging 指南Python
- Python 模組Python