- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python logging模块的使用总结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
日志级别 。
logging.basicConfig()函数中的具体参数含义 。
format参数用到的格式化信息 。
。
参数 | 描述 |
---|---|
%(name)s | Logger的名字 |
%(levelno)s | 数字形式的日志级别 |
%(levelname)s | 文本形式的日志级别 |
%(pathname)s | 调用日志输出函数的模块的完整路径名,可能没有 |
%(filename)s | 调用日志输出函数的模块的文件名 |
%(module)s | 调用日志输出函数的模块名 |
%(funcName)s | 调用日志输出函数的函数名 |
%(lineno)d | 调用日志输出函数的语句所在的代码行 |
%(created)f | 当前时间,用UNIX标准的表示时间的浮 点数表示 |
%(relativeCreated)d | 输出日志信息时的,自Logger创建以 来的毫秒数 |
%(asctime)s | 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒 |
%(thread)d | 线程ID。可能没有 |
%(threadName)s | 线程名。可能没有 |
%(process)d | 进程ID。可能没有 |
%(message)s | 用户输出的消息 |
。
使用logging打印日志到标准输出 。
1
2
3
4
|
import
logging
logging.debug(
'debug message'
)
logging.info(
'info message'
)
logging.warning(
'warning message'
)
|
使用logging.baseConfig()将日志输入到文件 。
1
2
3
4
5
6
7
8
9
10
11
|
import
os
logging.basicConfig(
filename
=
os.path.join(os.getcwd(),
'all.log'
),
level
=
logging.DEBUG,
format
=
'%(asctime)s %(filename)s : %(levelname)s %(message)s'
,
# 定义输出log的格式
filemode
=
'a'
,
datefmt
=
'%Y-%m-%d %A %H:%M:%S'
,
)
logging.debug(
'this is a message'
)
|
自定义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
29
30
31
32
33
34
35
36
|
import
logging
from
logging
import
handlers
class
Logger(
object
):
level_relations
=
{
'debug'
: logging.DEBUG,
'info'
: logging.INFO,
'warning'
: logging.WARNING,
'error'
: logging.ERROR,
'crit'
: logging.CRITICAL
}
def
__init__(
self
, filename, level
=
'info'
, when
=
'D'
, backCount
=
3
,
fmt
=
'%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
):
self
.logger
=
logging.getLogger(filename)
format_str
=
logging.Formatter(fmt)
# 设置日志格式
self
.logger.setLevel(
self
.level_relations.get(level))
# 设置日志级别
# 向控制台输出日志
stream_handler
=
logging.StreamHandler()
stream_handler.setFormatter(format_str)
self
.logger.addHandler(stream_handler)
# 日志按文件大小写入文件
# 1MB = 1024 * 1024 bytes
# 这里设置文件的大小为500MB
rotating_file_handler
=
handlers.RotatingFileHandler(
filename
=
filename, mode
=
'a'
, maxBytes
=
1024
*
1024
*
500
, backupCount
=
5
, encoding
=
'utf-8'
)
rotating_file_handler.setFormatter(format_str)
self
.logger.addHandler(rotating_file_handler)
log
=
Logger(
'all.log'
, level
=
'info'
)
log.logger.info(
'[测试log] hello, world'
)
|
按照间隔日期自动生成日志文件 。
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
import
logging
from
logging
import
handlers
class
Logger(
object
):
level_relations
=
{
'debug'
: logging.DEBUG,
'info'
: logging.INFO,
'warning'
: logging.WARNING,
'error'
: logging.ERROR,
'crit'
: logging.CRITICAL
}
def
__init__(
self
, filename, level
=
'info'
, when
=
'D'
, backCount
=
3
,
fmt
=
'%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
):
self
.logger
=
logging.getLogger(filename)
format_str
=
logging.Formatter(fmt)
# 设置日志格式
self
.logger.setLevel(
self
.level_relations.get(level))
# 设置日志级别
# 往文件里写入
# 指定间隔时间自动生成文件的处理器
timed_rotating_file_handler
=
handlers.TimedRotatingFileHandler(
filename
=
filename, when
=
when, backupCount
=
backCount, encoding
=
'utf-8'
)
# 实例化TimedRotatingFileHandler
# interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种:
# S 秒
# M 分
# H 小时、
# D 天、
# W 每星期(interval==0时代表星期一)
# midnight 每天凌晨
timed_rotating_file_handler.setFormatter(format_str)
# 设置文件里写入的格式
self
.logger.addHandler(timed_rotating_file_handler)
# 往屏幕上输出
stream_handler
=
logging.StreamHandler()
stream_handler.setFormatter(format_str)
self
.logger.addHandler(stream_handler)
log
=
Logger(
'all.log'
, level
=
'info'
)
log.logger.info(
'[测试log] hello, world'
)
|
logging 模块在Flask中的使用 我在使用Flask的过程中看了很多Flask关于logging的文档,但使用起来不是很顺手,于是自己就根据Flask的官方文档写了如下的log模块,以便集成到Flask中使用.
restful api 项目目录:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
.
├── apps_api
│ ├── common
│ ├── models
│ └── resources
├── logs
├── migrations
│ └── versions
├── static
├── templates
├── test
└── utils
└── app.py
└── config.py
└── exts.py
└── log.py
└── manage.py
└── run.py
└── README.md
└── requirements.txt
|
log.py 文件 。
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# -*- coding: utf-8 -*-
import
logging
from
flask.logging
import
default_handler
import
os
from
logging.handlers
import
RotatingFileHandler
from
logging
import
StreamHandler
BASE_DIR
=
os.path.dirname(os.path.abspath(__file__))
LOG_PATH
=
os.path.join(BASE_DIR,
'logs'
)
LOG_PATH_ERROR
=
os.path.join(LOG_PATH,
'error.log'
)
LOG_PATH_INFO
=
os.path.join(LOG_PATH,
'info.log'
)
LOG_PATH_ALL
=
os.path.join(LOG_PATH,
'all.log'
)
# 日志文件最大 100MB
LOG_FILE_MAX_BYTES
=
100
*
1024
*
1024
# 轮转数量是 10 个
LOG_FILE_BACKUP_COUNT
=
10
class
Logger(
object
):
def
init_app(
self
, app):
# 移除默认的handler
app.logger.removeHandler(default_handler)
formatter
=
logging.Formatter(
'%(asctime)s [%(thread)d:%(threadName)s] [%(filename)s:%(module)s:%(funcName)s] '
'[%(levelname)s]: %(message)s'
)
# 将日志输出到文件
# 1 MB = 1024 * 1024 bytes
# 此处设置日志文件大小为500MB,超过500MB自动开始写入新的日志文件,历史文件归档
file_handler
=
RotatingFileHandler(
filename
=
LOG_PATH_ALL,
mode
=
'a'
,
maxBytes
=
LOG_FILE_MAX_BYTES,
backupCount
=
LOG_FILE_BACKUP_COUNT,
encoding
=
'utf-8'
)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)
stream_handler
=
StreamHandler()
stream_handler.setFormatter(formatter)
stream_handler.setLevel(logging.INFO)
for
logger
in
(
# 这里自己还可以添加更多的日志模块,具体请参阅Flask官方文档
app.logger,
logging.getLogger(
'sqlalchemy'
),
logging.getLogger(
'werkzeug'
)
):
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
|
在exts.py扩展文件中添加log模块 。
1
2
3
4
|
# encoding: utf-8
from
log
import
Logger
logger
=
Logger()
|
在app.py 文件中引入logger模块,这个文件是create_app的工厂模块.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# encoding: utf-8
from
flask
import
Flask
from
config
import
CONFIG
from
exts
import
logger
def
create_app():
app
=
Flask(__name__)
# 加载配置
app.config.from_object(CONFIG)
# 初始化logger
logger.init_app(app)
return
app
|
运行run.py 。
1
2
3
4
5
6
7
8
|
# -*- coding: utf-8 -*-
from
app
import
create_app
app
=
create_app()
if
__name__
=
=
'__main__'
:
app.run()
|
1
2
3
4
5
6
7
8
9
10
|
$ python run.py
*
Serving Flask app
"app"
(lazy loading)
*
Environment: production
WARNING: This
is
a development server. Do
not
use it
in
a production deployment.
Use a production WSGI server instead.
*
Debug mode: on
2019
-
07
-
08
08
:
15
:
50
,
396
[
140735687508864
:MainThread] [_internal.py:_internal:_log] [INFO]:
*
Running on http:
/
/
127.0
.
0.1
:
5000
/
(Press CTRL
+
C to quit)
2019
-
07
-
08
08
:
15
:
50
,
397
[
140735687508864
:MainThread] [_internal.py:_internal:_log] [INFO]:
*
Restarting with stat
2019
-
07
-
08
08
:
15
:
50
,
748
[
140735687508864
:MainThread] [_internal.py:_internal:_log] [WARNING]:
*
Debugger
is
active!
2019
-
07
-
08
08
:
15
:
50
,
755
[
140735687508864
:MainThread] [_internal.py:_internal:_log] [INFO]:
*
Debugger PIN:
234
-
828
-
739
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://www.cnblogs.com/DeaconOne/p/11153810.html 。
最后此篇关于python logging模块的使用总结的文章就讲到这里了,如果你想了解更多关于python logging模块的使用总结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
1、定义 设 \(u\) 和 \(v\) 为一张图上的任意两个节点。令 \(c(u, v)\) 为它们之间的边的容量, \(f(u, v)\) 为它们之间的流量,则需要满足以
1、前言 工作中涉及到文件系统,有时候需要判断文件和目录是否存在。我结合apue第四章文件和目录,总结一下如何正确判断文件和目录是否存在,方便以后查询。 2、stat系列函数 stat函数用来
并查集(Union-Find Set): 一种用于管理分组的数据结构。它具备两个操作:(1)查询元素a和元素b是否为同一组 (2) 将元素a和b合并为同一组。 注意:并查集不能将在同一组的元素拆
当下,注解非常流行,以前很长篇的代码,现在基本上一个注解就能搞定。 那,在Mybatis中又有哪些注解呢? Mybatis中的注解基本上都在org.apache.ibatis.annotat
指针操作数组,方法一是p+index,方法二是p[index],第二种方法跟数组访问方法是一样的。 数组引用返回的是数组的第一个元素的指针地址。 可以将指针指向数组的任意元素,然后从那里开始访问
通常部署完php环境后会进行一些安全设置,除了熟悉各种php漏洞外,还可以通过配置php.ini来加固PHP的运行环境,PHP官方也曾经多次修改php.ini的默认设置。 下面对php.ini中一
在JavaScript中,使用typeof可以检测基本数据类型,使用instanceof可以检测引用数据类型。在PHP中,也有检测数据类型的方法,具体如下: 1、输出变量的数据类型(gettype
把图片缓存到本地,在很多场景都会用到,如果只是存储文件信息,那建一个plist文件,或者数据库就能很方便的解决问题,但是如果存储图片到沙盒就没那么方便了。这里简单介绍两种保存图片到沙盒的方法。
(1)需要安装docker容器,在docker容器内安装jenkins,gogs,tomcat。 新建maven项目,添加findbugs plugin。 使用docker
今天主题是实现并发服务器,实现方法有多种版本,先从简单的单进程代码实现到多进程,多线程的实现,最终引入一些高级模块来实现并发TCP服务器。 说到TCP,想起吐槽大会有个段子提到三次握手,也只有程序
如下所示: Ctrl+1或F2快速修复 Ctrl+D快捷删除行 Shift+Enter 快速切换到下一行,在本行的任何位置都可 Ctrl+F11快速运行代码 Alt+上下键 快速移动行(可
JSP是Servlet技术的扩展,本质上是Servlet的简易方式,更强调应用的外表表达。 JSP编译后是”类servlet”。 Servlet和JSP最主要的不同点在于,Servlet的应用逻辑
Java中的Runable,Callable,Future,FutureTask,ExecutorService,Excetor,Excutors,ThreadPoolExcetor在这里对这些关键
读取Java文件到byte数组的三种方法(总结) ? 1
用java实现的数组创建二叉树以及递归先序遍历,递归中序遍历,递归后序遍历,非递归前序遍历,非递归中序遍历,非递归后序遍历,深度优先遍历,广度优先遍历8种遍历方式:
1、简明总结 ASCII(char) 返回字符的ASCII码值 BIT_LENGTH(str) 返回字符串的比特长度 CONCAT(s1,s2…,sn)
java应用服务器(web server),是指运行java程序的web应用服务器软件,不包括nginx、Apache等通用web服务器软件。 一、Tomcat Tomcat是Apache 软件基
事务作为抽象层,允许应用忽略DB 内部一些复杂并发问题和某些硬件、软件故障,简化应用层的处理逻辑:事务中止(transaction abort),而应用仅需重试。对复杂访问模式,事务可大大减少需要考虑
我们在本教程学习了如何描述 XML 文档的结构 我们学习到了如何使用 DTD 来定义一个 XML 文档的合法元素,以及如何在我们的 XML 内部或者作为一个外部引用来声明 DTD 我们学习了如何为
在这个XPath 基础教程中我们讲解了如何在 XML 文档中查找信息 我们可以使用 XPath 的元素和属性在 XML 文档中进行导航 我们也学习了如何使用 XPath 中内建的某些标准函数 如
我是一名优秀的程序员,十分优秀!