博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python logger /logging
阅读量:5247 次
发布时间:2019-06-14

本文共 1869 字,大约阅读时间需要 6 分钟。

# !/user/bin/python# -*- coding: utf-8 -*-'''subprocess : 需要在linux平台上测试 shelllogging'''import logging# 将日志输出在文件里# logging.basicConfig(filename="app.log", level=logging.DEBUG)logging.basicConfig(filename="app.log",                    level=logging.WARNING,                    format='%(asctime)s %(levelname)s  %(filename)s:%(lineno)d  - %(message)s',                    datefmt='%m/%d/%Y %I:%M:%S %p')  # 在日志上加上时间. %p代表pm.  TODO 为什么没打出行数?logging.debug("test debug")logging.info("test info")logging.error("test error")logging.warning("User [alex] attempted wrong password more than 3 times")# 同时将日志打印在屏幕上并输出在文件里# step 1, create loggerlogger = logging.getLogger("TEST-LOG")logger.setLevel(logging.DEBUG)# step2, create console handler and set level to debugch=logging.StreamHandler()ch.setLevel(logging.DEBUG)# step3, create file handler and set level to warningfh = logging.FileHandler("process.log")fh.setLevel(logging.ERROR)# step3, define formatfh_formatter = logging.Formatter('%(asctime)s %(levelname)s  %(filename)s:%(lineno)d  - %(message)s')ch_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')fh.setFormatter(fh_formatter)ch.setFormatter(ch_formatter)# step4, connect handlers to loggerlogger.addHandler(fh)logger.addHandler(ch)logger.warning("ddddd")

 

Level When it’s used
DEBUG Detailed information, typically of interest only when diagnosing problems.
INFO Confirmation that things are working as expected.
WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERROR Due to a more serious problem, the software has not been able to perform some function.
CRITICAL A serious error, indicating that the program itself may be unable to continue running.

转载于:https://www.cnblogs.com/cheese320/p/9061275.html

你可能感兴趣的文章
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>
vue项目中使用百度统计
查看>>
android:scaleType属性
查看>>
SuperEPC
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>
第三次作业
查看>>
vue route 跳转
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。...
查看>>
Linux中防火墙centos
查看>>
mysql新建用户,用户授权,删除用户,修改密码
查看>>