只要文件夹下含有__init__.py文件就是一个包
回想一下,之前我们没有学习模块的时候将一个整体的功能写入到文件中,为了能够充分的将某个功能进行重用 我们使用了模块,但是慢慢的模块就会越来越多.我们想提高程序的结构性可维护性,就使用包将模块进行统一管理
包能够管理多个模块,我们想要使用包里的模块怎么办呢?
使用import 和from xx import xx
2种方法
2.绝对路径导入
from bake.api import www #推荐使用
www.fun()
3.相对路径导入
from .. api.www import fun #zbb中
fun() #必须在最外层的同级进行导入
#执行
from bake.api.zbb import fun
函数式简单配置(最垃圾)
import logging
logging.debug('调试')
logging.info('信息')
logging.warning('警告')
logging.error('错误')
logging.critical('危险')
默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志,这说明默认的日志级别设置为WARNING
(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG),
默认的日志格式为日志级别:Logger名称:用户输出消息。
灵活配置日志级别,日志格式,输出位置:(垃圾)
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='/tmp/test.log',
filemode='w')
basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有:
format参数中可能用到的格式化串:
logger对象配置(标准版)
import logging
logger = logging.getLogger()
# 创建一个logger
fh = logging.FileHandler('test.log',mode="a",encoding='utf-8') # 文件
ch = logging.StreamHandler() # 屏幕
formatter = logging.Formatter('%(asctime)s - %(name)s - %(filename)s - [line:%(lineno)d] - %(levelname)s - %(message)s')
# 将屏幕和文件都是用以上格式
logger.setLevel(logging.DEBUG)
# 设置记录级别
fh.setFormatter(formatter)
# 使用自定义的格式化内容
ch.setFormatter(formatter)
logger.addHandler(fh) #logger对象可以添加多个fh和ch对象
logger.addHandler(ch) #打印在屏幕上
logger.debug('logger debug message')
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')
import logging
dic = {"key":123}
logging.debug(dic)
num = 100
logging.info(f"用户当前余额:{num - 50}")
try:
num = int(input("请输入数字:"))
except Exception as e:
logging.warning("int将字符串转换报错了")
print("12334")
logging.error('我是错误')
logging.critical('我是危险')
旗舰版(牛逼)
import os
import logging.config
# 定义三种日志输出格式 开始
standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字
simple_format = '在 %(asctime)s %(message)s'
id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'
# log文件的全路径
logfile_path = 'all2.log'
# log配置字典
LOGGING_DIC = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': standard_format
},
'simple': {
'format': simple_format
},
},
'filters': {},
'handlers': {
#打印到终端的日志
'stream': {
'level': 'DEBUG',
'class': 'logging.StreamHandler', # 打印到屏幕
'formatter': 'simple'
},
#打印到文件的日志,收集info及以上的日志
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler', # 保存到文件
'formatter': 'standard',
'filename': None, # 日志文件
'maxBytes': 1024*1024*1024, # 日志大小 5M
'backupCount': 5,
'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了
},
},
'loggers': {
#logging.getLogger(__name__)拿到的logger配置
'': {
'handlers': ['stream', 'file'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
'level': 'DEBUG',
'propagate': True, # 向上(更高level的logger)传递
},
},
}
def get_logger():
path = r'F:\s24\day21\liye.log'
LOGGING_DIC['handlers']['file']['filename'] = path
logging.config.dictConfig(LOGGING_DIC) # 导入上面定义的logging配置
logger = logging.getLogger(__name__) # 生成一个log实例
return logger
def save():
logger = get_logger()
logger.info(f'{} 存入300元') # 记录该文件的运行状态
save()
原文:https://www.cnblogs.com/zdqc/p/11290500.html