hello,小伙伴们,你们好,今天给你们介绍的开源项目是:loguru
,这个开源项目是一个Python
简易日志库,这个开源项目的宗旨是经过添加一系列有用的功能来解决标准记录器的注意事项,从而减小 Python
日志记录的痛苦。git
咱们使用logging的配置方法基本上是这样的:github
import logging
logging.basicConfig(
filename='test.log',
level=logging.DEBUG,
format='[line:%(lineno)d] - %(funcName)s: %(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
)
复制代码
使用官方自带的logging
模块,每次建立项目都要本身去配置一下才能够,常常码代码使用起来有点吗发,可是使用这个loguru
模块,只须要两行代码便可完成以上配置,简单易用。bash
pip install loguru
复制代码
最简单的使用方法,只须要倒入模块,而后debug输出,看控制台效果便可。app
from loguru import logger
logger.debug("That's it, beautiful and simple logging!")
复制代码
打印在控制台中是彩色的! 性能
若是你的终端兼容,loguru会自动为日志添加颜色。你能够经过使用接收器格式的标记标签来定义本身喜欢的样式。学习
logger.add(sys.stdout, colorize=True, format="<green>{time}</green> <level>{message}</level>")
复制代码
只须要添加一行代码便可测试
from loguru import logger
# 添加到名为demo.log文件中
logger.add("demo.log")
logger.debug("That's it, beautiful and simple logging!")
复制代码
输出到文件中 ui
以时间为文件名分割日志,能够这样作:spa
from loguru import logger
# 以时间分割日志
logger.add('file_{time}.log')
logger.debug("That's it, beautiful and simple logging!")
复制代码
效果展现(看文件名称) debug
logger.add("rotation.log", rotation="500 MB")
复制代码
以上配置能够实现每 500MB 存储一个文件,每一个 log 文件过大就会新建立一个 log 文件。咱们在配置 log 名字时加上了一个 time 占位符,这样在生成时能够自动将时间替换进去,生成一个文件名包含时间的 log 文件。
咱们也可使用rotation
参数实现定时建立 log 文件,例如:
logger.add('runtime_{time}.log', rotation='12:00')
复制代码
这样就是实现天天 12点新建立一个 log 文件输出了。
另外咱们也能够配置 log 文件的循环时间,好比每隔两周建立一个 log 文件,写法以下:
logger.add('runtime_{time}.log', rotation='2 week')
复制代码
这样咱们就能够实现两周建立一个 log 文件了。
不知道你们有没有遇到过这样的状况,不少状况下,一些很是久远的 log 对咱们来讲并无什么用处了,它白白占据了一些存储空间,不清除掉就会很是浪费。retention 这个参数能够配置日志的最长保留时间。
好比咱们想要设置日志文件最长保留 7 天,能够这么来配置:
logger.add('runtime.log', retention='7 days')
复制代码
这样 log 文件里面就会保留最新 7 天的 log,老师不再用担忧 log 沉积的问题。
loguru 还能够配置文件的压缩格式,好比使用 zip 文件格式保存,以下:
logger.add('runtime.log', compression='zip')
复制代码
这样能够更加节省存储空间。
logger.info('If you are using Python {}, prefer {feature} of course!', 3.6, feature='f-strings')
复制代码
这样在添加参数就很是方便了。
在不少状况下,若是遇到运行错误,而咱们在打印输出 log 的时候万一不当心没有配置好 Traceback 的输出,颇有可能咱们就无法追踪错误所在了。
但用了 loguru 以后,咱们用它提供的装饰器就能够直接进行 Traceback 的记录,相似这样的配置便可:
@logger.catch
def my_function(x, y, z):
# An error? It's caught anyway!
return 1 / (x + y + z)
复制代码
咱们作个测试,咱们在调用时三个参数都传入 0,直接引起除以 0 的错误,看看会出现什么状况:
my_function(0, 0, 0)
复制代码
运行完毕以后,能够发现 log 里面就出现了 Traceback 信息,并且给咱们输出了当时的变量值,真的是不能再赞了!结果以下:
> File "run.py", line 15, in <module>
my_function(0, 0, 0)
└ <function my_function at 0x1171dd510>
File "/private/var/py/logurutest/demo5.py", line 13, in my_function
return 1 / (x + y + z)
│ │ └ 0
│ └ 0
└ 0
ZeroDivisionError: division by zero
复制代码
但愿对日志进行序列化以便于解析或传递日志?使用该serialize
参数,每条日志消息在发送到已配置的接收器以前将转换为JSON字符串。
logger.add('file_{time}.log', serialize=True)
# {"text": "2020-07-15 21:47:33.793 | DEBUG | __main__:<module>:14 - That's it, beautiful and simple logging!\n", "record": {"elapsed": {"repr": "0:00:00.017317", "seconds": 0.017317}, "exception": null, "extra": {}, "file": {"name": "loguru_demo.py", "path": "/Users/notes/modules/loguru_demo.py"}, "function": "<module>", "level": {"icon": "\ud83d\udc1e", "name": "DEBUG", "no": 10}, "line": 14, "message": "That's it, beautiful and simple logging!", "module": "loguru_demo", "name": "__main__", "process": {"id": 7280, "name": "MainProcess"}, "thread": {"id": 4458839488, "name": "MainThread"}, "time": {"repr": "2020-07-15 21:47:33.793271+08:00", "timestamp": 1594820853.793271}}}
复制代码
Loguru能够轻松地与功能强大的notifiers
库结合使用(必须单独安装),以在程序意外失败时接收电子邮件或发送其余多种通知。
import notifiers
params = {
"username": "you@gmail.com",
"password": "abc123",
"to": "dest@gmail.com"
}
# Send a single notification
notifier = notifiers.get_notifier("gmail")
notifier.notify(message="The application is running!", **params)
# Be alerted on each error message
from notifiers.logging import NotificationHandler
handler = NotificationHandler("gmail", defaults=params)
logger.add(handler, level="ERROR")
复制代码
或者配合ES
使用
尽管在大多数状况下,日志记录对性能的影响能够忽略不计,但零成本的日志记录器将容许在任何地方使用它而无需过多担忧。在即将发布的版本中,Loguru的关键功能将以C语言实现,以实现最大速度。
以上就是loguru
的基本用法了,感兴趣的小伙伴能够研究一下这个的用法,特别简单易用,赶快去配置到你的项目中吧!
今天的推荐不知道你们喜欢吗?若是大家喜欢话,请在文章底部留言或点赞,以表示对个人支持,大家的留言,点赞,转发关注是我持续更新的动力哦!
关注公众号回复:"1024
",免费领取一大波学习资源,先到先得哦!