简体   繁体   English

如何为 Node.JS 项目实现通用/与包无关的日志记录?

[英]How do I implement generic / package-agnostic logging for a Node.JS Project?

I want to add logging to my Node.JS project, so that I can view debug logs while the application runs.我想将日志记录添加到我的 Node.JS 项目中,以便在应用程序运行时查看调试日志。 There are a lot of different loggers out there for JavaScript apparently, and I'm not sure which one to use.显然,有很多不同的 JavaScript 记录器,我不确定使用哪一个。 Ideally, I would like to write my code so that the logger is not part of the codebase, but instead the logging is generic and I can switch between different generic logging libraries as I see fit.理想情况下,我想编写我的代码,以便记录器不是代码库的一部分,而是日志是通用的,我可以在我认为合适的情况下在不同的通用日志库之间切换。 How can I do this?我怎样才能做到这一点?

Do not use Pino , debug , or any other external loggers in your project if you intend for people to take a dependency on your package!如果您打算让人们依赖您的包,请不要在项目中使用Pinodebug或任何其他外部记录器!

abstract-logging: a no-op logging interface for JS modules. abstract-logging:JS 模块的无操作日志接口。

You can use the NPM package abstract-logging for these purposes.您可以将 NPM 包abstract-logging用于这些目的。 It implements a standard log4j logging interface of no-ops.它实现了无操作的标准 log4j 日志接口。 From the readme:从自述文件:

This module provides an interface for modules to include so that they can support logging via an external logger that conforms to the standard Log4j interface.该模块为要包含的模块提供了一个接口,以便它们可以通过符合标准 Log4j 接口的外部记录器支持日志记录。 One such logger is Pino.一个这样的记录器是皮诺。 This module is intended for modules that are meant to be used by other modules.该模块适用于其他模块使用的模块。

This way, when you release your package, you only have to include the small, no dependency library as a direct dependency.这样,当你发布你的包时,你只需要包含一个小的、没有依赖的库作为直接依赖。 Libraries like Pino , debug , etc. can be included as dev-dependencies while working on the package, and when someone takes a dependency on your package they don't have to worry about having their logging library clashing with your logging library, causing all sorts of debug troubles.在处理包时,可以将Pinodebug等库作为开发依赖项包含在内,当有人依赖您的包时,他们不必担心他们的日志库与您的日志库发生冲突,从而导致所有各种调试麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM