为库和cli工具提供简单的日志管理。
magiclog的Python项目详细描述
magiclog简化了应用程序和库日志记录,允许您使用 使用无需复杂设置或 记录器发现。
frommagiclogimportlogdefmain():log.configure()log.info('Hello, INFO.')if__name__=='__main__':main()
magiclog以比 logging模块的基本配置,而不要求 图书馆作者:
- from magiclog import log使用 一个小小的元编程,不需要考虑是否应该 使用__package__、__name__或sys.argv[0]。
- 当您在后台运行任务时,magiclog会识别 这将记录到syslog;否则将记录到stderr。
- magiclog为stderr提供简单的单行格式,并带有时间 和一条消息,但也可以以扩展格式登录,使用日志和 水平,如有要求。并且magiclog在 登录到syslog。
magiclog仅在调用log.configure()时才安排日志。 如果要自定义级别,可以显式地将它们传递给 log.configure()和level=...、syslog=...和stderr=... 命名参数。否则,您的记录器具有一个空处理程序,如中所述 Configuring Logging for a Library
log.configure()# Same as the above.log.configure(level='info')# Use Syslog or stderr based on whether or not we're in the foreground,# but at debug level instead of info level.log.configure(level='debug')# Enables both channels, whether foregrounded or backgrounded.log.configure(syslog='info',stderr='info')# Enables the more verbose console logging format, with module and function# name information as well as time.log.configure(level='info',extended=True)
配置其他模块的记录器
magiclog中的logger函数提供记录器发现、遍历 堆栈并使用模块或包名称,或运行的名称 可执行,视情况而定。您可以使用logger检索和 配置其他模块的记录器,遵循与 应用程序记录器。
importmagiclogfrommagiclogimportlogdefmain():# You can pass a module object or a string to ``logger``. Here, we# configure magiclog's own (usually not very useful) logger.magiclog.logger(magiclog).configure()# Obtain a reference to Boto3's logger and auto-configure it. The# logger for ``magiclog`` will print out a few things as it sets this# logger up.magiclog.logger('boto3').configure()log.configure()log.info('Hello, INFO.')if__name__=='__main__':main()