Enjoy the good life everyday!
关闭
欢迎来PyGo个人空间 ^_^
Python模块之logger日志 | PyGo²

Python模块之logger日志

Python Python模块系列
Python模块系列

Python项目常用的模块代码。

版本

名称 版本
Python 3

描述

日志模块。

使用

1
2
3
4
5
6
7
from app.utils.logger import logger

logger.debug('message')
logger.info('message')
logger.warning('message')
logger.error('message')
logger.critical('message')

源码

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-

"""
------------------------------------------------

describe:
日志

record system information
level: debug, info, warning, error, critical

CRITICAL 50
ERROR 40
WARNING 30
INFO 20
DEBUG 10

formatter:
%(levelno)s:打印日志级别的数值。

%(levelname)s:打印日志级别的名称。

%(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]。

%(filename)s:打印当前执行程序名。

%(funcName)s:打印日志的当前函数。

%(lineno)d:打印日志的当前行号。

%(asctime)s:打印日志的时间。

%(thread)d:打印线程ID。

%(threadName)s:打印线程名称。

%(process)d:打印进程ID。

%(processName)s:打印线程名称。

%(module)s:打印模块名称。

%(message)s:打印日志信息。

base_info:
__author__ = "PyGo"
__time__ = "2022/9/14"
__version__ = "v.1.0.0"
__mail__ = "gaoming971366@163.com"
__project__ = "quality-inspect"

usage:
from app.utils.logger import logger

logger.debug('message')
logger.info('message')
logger.warning('message')
logger.error('message')
logger.critical('message')

design:

reference urls:

python version:
python3


Enjoy the good time everyday!!!
Life is short, I use python.

------------------------------------------------
"""

# ------------------------------------------------------------
# usage: /usr/bin/python logger.py
# ------------------------------------------------------------
import datetime
import inspect
import logging
import os
import sys
from logging.handlers import RotatingFileHandler
from config import LOG_DIR, LOG_LEVEL, LOG_FORMATTER, LOG_FILENAME_PREFIX

LEVEL = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}


logger = logging.getLogger(__name__)


# get current folder, solve is or not frozen of the script
def _get_cur_folder():
if getattr(sys, "frozen", False):
return os.path.dirname(os.path.abspath(__file__))
else:
cur_folder = os.path.dirname(inspect.getfile(inspect.currentframe()))
return os.path.abspath(cur_folder)


def _get_now(format="%Y-%m-%d %H:%M:%S"):
return datetime.datetime.strftime(datetime.datetime.now(), format)


# default log dir
def __get_log_dir():
return os.path.join(_get_cur_folder(), 'log')


logdir = LOG_DIR
level = LOG_LEVEL
formatter = LOG_FORMATTER
filename_prefix = LOG_FILENAME_PREFIX


if not logdir:
logdir = __get_log_dir()
if not os.path.exists(logdir):
try:
os.makedirs(logdir)
logger.critical('====== log dir is not exist, create: %s ======' % logdir)
except:
logger.critical('====== log dir is not exist and create failure, exist: %s ======' % logdir)
sys.exit(1)
if not level:
level = 'debug'
# 格式
if not formatter:
formatter = '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s - %(message)s'
formatter = logging.Formatter(formatter,
datefmt='%Y/%m/%d %H:%M:%S')

log_level = LEVEL.get(level)
logger.setLevel(level=log_level)


# 定义一个RotatingFileHandler,最多备份10个日志文件,每个日志文件最大10M
log_name = filename_prefix + '_' + _get_now(format="%Y-%m-%d") \
if filename_prefix and filename_prefix != '-' else _get_now(format="%Y-%m-%d")
log_file = os.path.join(logdir, (log_name + '.log'))
file_handler = RotatingFileHandler(log_file,
mode='a',
maxBytes=10*1024*1024,
backupCount=10,
encoding='utf-8')
file_handler.setLevel(log_level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
# 控制台
stream_handler = logging.StreamHandler()
stream_handler.setLevel(log_level)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)

系列

Python模块之command系统命令
Python模块之excel模块
Python模块之logger日志
Python模块之utils公共方法
Python模块之watcher打点
Python模块之config配置解析
Python模块之dtalk钉钉消息
Python模块之企业微信

更多模块请参考文章TAG进行查看。

Python模块系列,持续更新中。。。。。。
  • 本文作者:mingliang.gao【一个爱老婆Python程序猿。。。。。。】
  • 本文链接: http://pygo2.top/articles/5145/
  • 版权声明: 本博客所有文章欢迎转载,转载请注明出处!
觉得有帮助 请偶坐个公交车
0%