添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Ajax Alpine Apache Atom Beancount CI/CD CTF DevOps Django Django REST Framework Docker Git GitLab Github Golang Homelab IPv6 Javascript KMS Kubernetes LCTT LeetCode Let's Encrypt Linux MySQL NAS NTP PHP Prometheus Protobuf Python SQL Sublime Text UTF-8 Unicode Windows collections ctypes fava git gunicorn iptables logging nginx node.js openssl pip python requests supervisor unittest virtualenv vue.js wget 七牛 上下文管理 前端开发 后端开发 微服务 数据库 数据迁移 文本编辑器 树莓派 算法 编程工具 脑洞 记账 读书 迭代器

2. 基本

输出信息:

1
2
3
4
5
logging.debug("A debug message")
logging.info("A info message")
logging.warn("A warning message")
logging.error("A error message")
logging.critical("A critical message")

日志级别:
logging默认的日志级别为WARNING,在当前级别下,只有warning及以上的日志可以被记录。logging的级别可通过 logging.basicConfig logging.setLevel 来修改。级别大小关系为CRITICAL(50) > ERROR(40) > WARNING(30) > INFO(20) > DEBUG(10) > NOTSET(0), 需要注意的是,一旦记录信息,logging的日志级别就不可再更改。

1
2
3
4
5
>>> logging.basicConfig(level=logging.INFO)
>>> logging.info("Info")
INFO:root:Info
>>> logging.basicConfig(level=logging.DEBUG)
>>> logging.debug("Debug") # 没有输出

3. 日志格式设置及日志文件操作

1
2
3
4
5
6
7
8
9
10
11
12
13
import logging
import time

logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s",
datefmt="%Y %b %d %H:%M:%S",
filename="./log.log",
filemode="w", # default is "a"
level=logging.INFO)

while True:
for i in range(6):
logging.log(i*10, "a log") # logging.log(level, msg)
time.sleep(1)

log.log输出(可以用tail -f命令实时查看):

1
2
3
4
5
6
7
8
2015 Oct 21 14:41:36 INFO a log
2015 Oct 21 14:41:37 WARNING a log
2015 Oct 21 14:41:38 ERROR a log
2015 Oct 21 14:41:41 CRITICAL a log
2015 Oct 21 14:41:42 INFO a log
2015 Oct 21 14:41:43 WARNING a log
2015 Oct 21 14:41:44 ERROR a log
2015 Oct 21 14:41:45 CRITICAL a log

4. 将日志输出到多个流中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import logging
import sys

# 可以通过logging.basicConfig设置一个默认流

console = logging.StreamHandler(stream=sys.stdout) # 默认流为sys.stderr
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger().addHandler(console)

files = logging.FileHandler("log2.log", mode="a", encoding="utf-8") # 设置文件流
files.setLevel(logging.WARNING)
formatter = logging.Formatter("%(levelname)s %(message)s")
files.setFormatter(formatter)
logging.getLogger().addHandler(files)

for i in range(1, 6):
logging.log(i*10, logging.getLevelName(i*10).lower())

5. 设置多个logger以记录不同信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# coding: utf-8

import logging
import sys

log1 = logging.Logger("0.0")

console = logging.StreamHandler(sys.stdout) # 默认流为sys.stderr
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
log1.addHandler(console)

log2 = logging.Logger("-.-")

files = logging.FileHandler("log2.log", mode="a", encoding="utf-8") # 设置文件流
files.setLevel(logging.WARNING)
formatter = logging.Formatter("%(name)s %(levelname)s %(message)s")
files.setFormatter(formatter)
log2.addHandler(files)

for i in range(1, 6):
log1.log(i*10, logging.getLevelName(i*10).lower())
log2.log(i*10, logging.getLevelName(i*10).lower())

logging.critical("AHHH! I'm the root logger but you forget me!") # 默认使用logging时logger name为"root"
logging.getLogger("root").info("Of course not!")

5. 参考文档

  • Logging HOWTO
  • python 的日志logging模块学习
  • 6. 后记

    又填完一个坑,最近要填的坑好多…