更快地替换标准日志模块。

fastlogging的Python项目详细描述


fastlogging模块可以更快地用主要兼容的api替换标准日志模块。

它具有以下功能:

  • (colored, if colorama is installed) logging to console
  • logging to file (maximum file size with rotating/history feature can be configured)
  • old log files can be compressed (the compression algorithm can be configured)
  • count same successive messages within a 30s time frame and log only once the message with the counted value.
  • log domains
  • log to different files
  • writing to log files is done in (per file) background threads, if configured
  • configure callback function for custom detection of same successive log messages
  • configure callback function for custom message formatter
  • configure callback function for custom log writer

api描述为here

安装

只需运行

pythonsetup.pyinstall--user

或者创建一个轮子并安装它。

pythonsetup.pybdist_wheel

如果安装了cython包,则将安装fastlogging的优化版本。 如果需要纯python版本的fastlogging模块,请添加选项nocython

用法

fromfastloggingimportLogInitlogger=LogInit(pathName="/tmp/example1.log",console=True,colors=True)logger.debug("This is a debug message.")logger.info("This is an info message.")logger.warning("This is a warning message.")logger.rotate()logger.fatal("This is a fatal message.")logger.shutdown()

上面的示例将所有消息写入文件和控制台。在控制台上打印消息 有颜色。通过旋转调用,日志文件被重命名为example1.log.1,并创建一个新的日志文件。

第二个示例在localhost上创建一个服务器套接字,并将所有消息写入日志文件15秒。

importosimporttimefromfastloggingimportLogInitaddr="127.0.0.1"port=12345pathName="C:/temp/server.log"ifos.name=='nt'else"/tmp/server.log"logger=LogInit(pathName=pathName,server=(addr,port))logger.info("Logging started.")logger.debug("This is a debug message.")logger.info("This is an info message.")logger.warning("This is a warning message.")time.sleep(15)logger.info("Shutdown logging.")logger.shutdown()

现在,第三个示例连接到日志服务器并发送300000条消息。

importosimporttimefromfastloggingimportLogInitaddr="127.0.0.1"port=12345logger=LogInit(connect=(addr,port,"HELLO%d"%os.getpid()))foriinrange(100000):logger.debug("This is a DBG message %d."%i)logger.info("This is an INF message %d."%i)logger.warning("This is a WRN message %d."%i)time.sleep(10.0)logger.shutdown()

这些信息被分块发送以提高速度。

优化速度

如下图所示,fastlogging比默认的日志模块快得多 与Python(红色条)。

您还可以看到,使用线程可能比直接将日志写入 文件,因为额外的开销。所以只有当你有一个很慢的磁盘和很多 要记录的消息。

还有3个酒吧表现得更好。要了解优化,请深入了解 必须做一条记录线。

让我们分析执行以下代码行时发生的情况:

logger.debug("This is a debug message.")

python解释器首先为定位参数创建一个元组,并为 论据。然后调用方法info。在方法info中,根据严重性检查日志级别。 只有当严重性足够高时,才会记录消息。

如果我们在上面的行之前设置一个if呢?

iflogger.level<=DEBUG:logger.debug("This is a debug message.")

运行基准测试将告诉我们,如果日志级别高于debug,代码现在运行得更快。 通常我们只需要在开发或修复的情况下调试消息。所以优化是有意义的 这样的台词。但是手工操作会很麻烦,会使代码膨胀。

为了简化这个任务,fastlogging模块附带了一个为您工作的AST optimizer

基准

以下基准测试是在Ubuntu18.10上用Ryzen7CPU和一个固态硬盘测量的。

可以看到,禁用旋转时,fastlogging的速度约为~5x快,日志旋转时,>;13x快。

doc/benchmarks/log.png

使用单个日志文件对结果进行基准测试

doc/benchmarks/rotate.png

使用旋转日志文件对结果进行基准测试

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java中的模板类   java如何创建跨服务域模型映射?   java如何最好地指定与Netty一起使用的Protobuf(最好使用内置Protobuf支持)   java Android Studio计数器文本视图字段未更新   操作属性>添加更改侦听器上的java按钮   Android中JSON参数的java解析   java DynamoDB和全局二级索引和ObjectMapper   JVM的Xmx限制的java外部控制?   java如何在触摸事件中停止图像滑块的自动滚动   java如何在AEM中从/content/dam读取xml文件?   在创建JavaNIO文件系统时,“env”选项(及其用途)是什么?   java如何在jdk 7或更早版本中使用lambda表达式   使用Java清理CSS