日志模块logging(只需要CV)
学习日志模块只需要会CV即可
什么是日志:
日志就是在你编码的过程中记录一些代码的变化
比如:记录用户什么时间登录的 用户什么时间注册的等
目的: 后续可以查看错误原因、对账等
import logging
"""日志的级别:后续我们写日志的时候,可以按照日志的级别选择性的记录"""
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
日志级别等级 CRITICAL > ERROR > WARNING > INFO > DEBUG
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s'
filename:用指定的文件名创建FiledHandler,这样日志会被存储在指定的文件中。
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
format:指定handler使用的日志显示格式。
datefmt:指定日期时间格式。
level:设置rootlogger(后边会讲解具体概念)的日志级别
stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件(f=open(‘test.log’,’w’)),默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。
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用户输出的消息
import logging
file_handler = logging.FileHandler(filename='x1.log', mode='a', encoding='utf-8',)
"""指定日志存储的格式:"""
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s - %(lineno)d行 - %(created)f : %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
handlers=[file_handler,],
level=logging.DEBUG
)
logging.debug('你好a aaa')
日志的详细使用
logging库提供了多个组件:Logger、Handler、Filter、Formatter
import logging
logger = logging.getLogger('转账记录')
hd1 = logging.FileHandler('a1.log',encoding='utf8')
hd2 = logging.FileHandler('a2.log',encoding='utf8')
hd3 = logging.StreamHandler()
fm1 = logging.Formatter(
fmt='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
)
fm2 = logging.Formatter(
fmt='%(asctime)s - %(name)s %(message)s',
datefmt='%Y-%m-%d',
)
logger.addHandler(hd1)
logger.addHandler(hd2)
logger.addHandler(hd3)
hd1.setFormatter(fm1)
hd2.setFormatter(fm2)
hd3.setFormatter(fm1)
logger.setLevel(30)
logger.warning('写了半天 好累啊 好热啊')

日志配置成字典格式使用(重要、用的多)
import logging
import logging.config
standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
'[%(levelname)s][%(message)s]'
simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
test_format = '%(asctime)s] %(message)s'
other_format = '[%(levelname)s][%(asctime)s]] %(message)s'
import os
base_dir = os.path.dirname(os.path.abspath(__file__))
log_path = os.path.join(base_dir, 'log')
if not os.path.exists(log_path):
os.mkdir(log_path)
logfile_path = os.path.join(log_path, 'a3.log')
LOGGING_DIC = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': standard_format
},
'simple': {
'format': simple_format
},
'other': {
'format': other_format
},
},
'filters': {},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'default': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': logfile_path,
'maxBytes': 1024 * 1024 * 100,
'backupCount': 5,
'encoding': 'utf-8',
},
'other': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'other'
},
},
'loggers': {
'': {
'handlers': ['default', 'console'],
'level': 'DEBUG',
'propagate': True,
},
'default': {
'handlers': ['default', ],
'level': 'DEBUG',
'propagate': False,
},
'console': {
'handlers': ['console', 'default'],
'level': 'DEBUG',
'propagate': False,
},
'other': {
'handlers': ['other'],
'level': 'DEBUG',
'propagate': False,
},
},
}
def common(xxx):
logging.config.dictConfig(LOGGING_DIC)
logger1 = logging.getLogger(xxx)
return logger1
logger=common('default')
logger.debug('xxx')
logger.info('xxx')
logger.warning('xxx')
第三方模块的下载与安装
之前学习的模块都是内置的模块,不需要安装的,直接拿来使用
但是,内置模块不能够满足我们日常的开发需求,有时候呢需要借助于一些第三方的模块(别人开发的,下载使用)
如何下载与使用第三方模块: pip 命令是有Python解释器提供的
pip install 模块名
pip3.8 install django==版本名
为了加快下载的速度,我们把默认的官网源修改成我们国内的源
"""换源"""
国内的源:
豆瓣:http://pypi.douban.com/simple/
阿里云:http://mirrors.aliyun.com/pypi/simple/
华为云:https://repo.huaweicloud.com/repository/pypi/simple
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
中科大:https://pypi.mirrors.ustc.edu.cn/simple/
换源的方法:
pip install django
pip3.8 install numpy -i http://mirrors.aliyun.com/pypi/simple/
永久换源:
-1 在文件地址栏输入:%APPDATA% 回车,快速进入 C:\Users\电脑用户\AppData\Roaming 文件夹中
-2 新建 pip 文件夹并
-3 在文件夹中新建 pip.ini 配置文件
-4 配置文件写入:
如果想换源就直接把源的路径换了就可以了
'''
[global]
index-url = https://mirrors.aliyun.com/pypi/simple
[install]
use-mirrors =true
mirrors =https://mirrors.aliyun.com/pypi/simple
trusted-host =mirrors.aliyun.com
'''
pip3.8 install django==3.2.12