python logging的简单应用

背景

在一些简单场景下,可以使用单python文件实现。这是也可以通过logging模块来记录执行日志。

一般情况通过shell直接运行脚本,建议配置console输出和文件输出log。其中文件输出建议使用按时间分割或者按大小分割,以免日志文件过大,导致后续排查问题困难。

实现

简单的场景下,可以直接使用basicConfig来配置:

1
2
3
4
5
6
7
8
9
timedRotatingFileHandler = handlers.TimedRotatingFileHandler(
filename='log.log',
when='midnight'
)
streamHandler = logging.StreamHandler()
logging.basicConfig(level=logging.INFO,#控制台打印的日志级别
format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s', #日志格式
handlers=[timedRotatingFileHandler, streamHandler]
)

其中:

format:配置日志格式。

StreamHandler:即console输出,如使用命令行执行,就会直接在命令行输出信息。

TimedRotatingFileHandler:为按时间分割的log文件。

以上就是一个简单日志配置,实现脚本运行时,日志同步输出到按时间分割的文件和console。

拓展

logging模块的basicConfig其实是自带文件记录的,可以先看下它的定义:

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
def basicConfig(**kwargs):
"""
Do basic configuration for the logging system.

This function does nothing if the root logger already has handlers
configured. It is a convenience method intended for use by simple scripts
to do one-shot configuration of the logging package.

The default behaviour is to create a StreamHandler which writes to
sys.stderr, set a formatter using the BASIC_FORMAT format string, and
add the handler to the root logger.

A number of optional keyword arguments may be specified, which can alter
the default behaviour.

filename Specifies that a FileHandler be created, using the specified
filename, rather than a StreamHandler.
filemode Specifies the mode to open the file, if filename is specified
(if filemode is unspecified, it defaults to 'a').
format Use the specified format string for the handler.
datefmt Use the specified date/time format.
style If a format string is specified, use this to specify the
type of format string (possible values '%', '{', '$', for
%-formatting, :meth:`str.format` and :class:`string.Template`
- defaults to '%').
level Set the root logger level to the specified level.
stream Use the specified stream to initialize the StreamHandler. Note
that this argument is incompatible with 'filename' - if both
are present, 'stream' is ignored.
handlers If specified, this should be an iterable of already created
handlers, which will be added to the root handler. Any handler
in the list which does not have a formatter assigned will be
assigned the formatter created in this function.

Note that you could specify a stream created using open(filename, mode)
rather than passing the filename and mode in. However, it should be
remembered that StreamHandler does not close its stream (since it may be
using sys.stdout or sys.stderr), whereas FileHandler closes its stream
when the handler is closed.

.. versionchanged:: 3.2
Added the ``style`` parameter.

.. versionchanged:: 3.3
Added the ``handlers`` parameter. A ``ValueError`` is now thrown for
incompatible arguments (e.g. ``handlers`` specified together with
``filename``/``filemode``, or ``filename``/``filemode`` specified
together with ``stream``, or ``handlers`` specified together with
``stream``.
"""
# Add thread safety in case someone mistakenly calls
# basicConfig() from multiple threads

可以看出可以通过在basicConfig()中有如下三个参数:

  • filename:记录日志的文件名
  • filemode:记录日志的方式(a为追加,w为覆盖)
  • stream:console输出

可以通过配置filename,filemode参数来配置文件输出。但仅是单文件输出而已。如果这两个参数留空,那么默认stream输出,这两种无法直接使用同时配置。

若需要同时有console输出和文件记录那么需要使用另一个参数:

  • handlers:通过先定义handler,并添加到该参数中,即可。

另外,需要注意的是,在自行定义handler时, 基础的三个handler:StreamHandler, FileHandlerNullHandler是直接定义在logging模块中的,而其他handler,如按TimedRotatingFileHandlerSocketHandlerSysLogHandler 等则需要通过logging.handlers模块引入。

1
2
3
4
5
6
import logging
streamHandler = logging.StreamHandler()
timedRotatingFileHandler = logging.handlers.TimedRotatingFileHandler(
filename='log.log',
when='midnight'
)