logging模块
日志级别从低到高分别为:debug、info、warning、error、critical
1. 屏幕输出默认级别是warning,debug和info日志不显示
>>> import logging >>> logging.debug("Started Cleanup of Temporary Directories") >>> logging.info("Starting Session 4766 of user root") >>> logging.warning("Accepted password for root from 172.16.15.200 port 50741 ssh2") WARNING:root:Accepted password for root from 172.16.15.200 port 50741 ssh2 >>> logging.error("Failed password for invalid user docker from 172.16.200.103 port 50684 ssh2") ERROR:root:Failed password for invalid user docker from 172.16.200.103 port 50684 ssh2 >>> logging.critical("Server keepalived is DOWN!!") CRITICAL:root:Server keepalived is DOWN!!
2. 将日志信息输出到文件中,并更改日志输出级别(本例级别为DEBUG)
import logging logging.basicConfig(filename="access.log",level=logging.DEBUG) logging.debug("Started Cleanup of Temporary Directories") logging.info("Starting Session 4766 of user root") logging.warning("Accepted password for root from 172.16.15.200 port 50741 ssh2") logging.error("Failed password for invalid user docker from 172.16.200.103 port 50684 ssh2") logging.critical("Server keepalived is DOWN!!")
3. 给日志加上时间、级别、模块名及行数(#号注释的为执行结果)
import logging logging.basicConfig(filename="access.log", level=logging.WARNING, format="%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s", datefmt="%Y/%m/%d %I:%M:%S %p") logging.debug("Started Cleanup of Temporary Directories") logging.info("Starting Session 4766 of user root") logging.warning("Accepted password for root from 172.16.15.200 port 50741 ssh2") logging.error("Failed password for invalid user docker from 172.16.200.103 port 50684 ssh2") logging.critical("Server keepalived is DOWN!!") #2017/08/17 06:36:28 PM logging_module.py:13 WARNING Accepted password for root from 172.16.15.200 port 50741 ssh2 #2017/08/17 06:36:28 PM logging_module.py:14 ERROR Failed password for invalid user docker from 172.16.200.103 port 50684 ssh2 #2017/08/17 06:36:28 PM logging_module.py:15 CRITICAL Server keepalived is DOWN!!
去掉时间格式的执行结果,上面的p参数代表上午下午
import logging logging.basicConfig(filename="access.log", level=logging.WARNING, format="%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s",) logging.debug("Started Cleanup of Temporary Directories") logging.info("Starting Session 4766 of user root") logging.warning("Accepted password for root from 172.16.15.200 port 50741 ssh2") logging.error("Failed password for invalid user docker from 172.16.200.103 port 50684 ssh2") logging.critical("Server keepalived is DOWN!!") #2017-08-17 18:38:56,471 logging_module.py:13 WARNING Accepted password for root from 172.16.15.200 port 50741 ssh2 #2017-08-17 18:38:56,471 logging_module.py:14 ERROR Failed password for invalid user docker from 172.16.200.103 port 50684 ssh2 #2017-08-17 18:38:56,471 logging_module.py:15 CRITICAL Server keepalived is DOWN!!
logging模块的类
- logger:提供了应用程序可以直接使用的接口
- handle:将logger创建的日志发送到目的输出,控制台屏幕、文件、远程服务器等
- filter:对日志记录进行条件过滤
- formatter:定义日志记录的输出
使用 handler 让日志输出到多个地方
1 import logging 2 from logging import handlers #在进行日志切分时导入 3 4 # 创建logger 5 nginx_logger = logging.getLogger("nginx") 6 nginx_logger.setLevel(logging.DEBUG) 7 lvs_logger = logging.getLogger("lvs_test") 8 lvs_logger.setLevel(logging.INFO) 9 10 # 创建控制台handler和日志级别 11 console_handler = logging.StreamHandler() 12 console_handler.setLevel(logging.INFO) 13 14 # 创建文件handle和日志级别(当用时间进行切分的时候when的参数有[s、m、h、d、w、midnight],不区分大小写,midnight为每天凌晨) 15 file_handler = logging.FileHandler("nginx_lvs.log") 16 # 按文件大小进行切分,以下意思为单个文件最大为10字节,最多保留2个备份文件 17 # file_handler = handlers.RotatingFileHandler(filename="nginx_lvs.log",maxBytes=10,backupCount=2,encoding="utf-8") 18 # 每天凌晨进行日志切割,最多保留2个日志备份文件 19 #file_handler = handlers.TimedRotatingFileHandler(filename="nginx_lvs.log",when='midnight',interval=1,backupCount=2,encoding="utf-8") 20 file_handler.setLevel(logging.WARNING) 21 22 # 设置日志格式,不同的输出可以设置不同的日志格式 23 console_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") 24 file_formatter = logging.Formatter("%(asctime)s - [%(name)s] - %(filename)s:%(lineno)d - [%(levelname)s] - %(message)s") 25 26 # 添加formatter到handler 27 console_handler.setFormatter(console_formatter) 28 file_handler.setFormatter(file_formatter) 29 30 # 添加handler到logger 31 nginx_logger.addHandler(console_handler) 32 nginx_logger.addHandler(file_handler) 33 lvs_logger.addHandler(console_handler) 34 lvs_logger.addHandler(file_handler) 35 36 # 日志打印 37 nginx_logger.debug("Started Cleanup of Temporary Directories") 38 nginx_logger.info("Starting Session 4766 of user root") 39 nginx_logger.warning("Accepted password for root from 172.16.15.200 port 50741 ssh2") 40 nginx_logger.error("Failed password for invalid user docker from 172.16.200.103 port 50684 ssh2") 41 nginx_logger.critical("Server keepalived is DOWN!!") 42 lvs_logger.debug("Started Cleanup of Temporary Directories") 43 lvs_logger.info("Starting Session 4766 of user root") 44 lvs_logger.warning("Accepted password for root from 172.16.15.200 port 50741 ssh2") 45 lvs_logger.error("Failed password for invalid user docker from 172.16.200.103 port 50684 ssh2") 46 lvs_logger.critical("Server keepalived is DOWN!!")
生成的日志截图如下:
2017-08-18 18:42:13,766 - [nginx] - logging_module.py:53 - [WARNING] - Accepted password for root from 172.16.15.200 port 50741 ssh2 |
博主小学未毕业,请勿口喷!