log4j 额外的日志记录参数

发布于 2024-07-14 00:05:31 字数 506 浏览 3 评论 0原文

我正在开发一个(内部)库,我想强制使用该库的开发人员在记录错误或致命级别消息时包含文本 ID。 在不修改 log4j 的情况下,我们想要强制执行的内容类似于:

logger.error( "E1234:An error has goneed" );

在没有对 log4j 进行扩展或进行少量扩展的情况下,我们可以在夜间构建期间扫描源代码来验证是否包含文本 ID,或实现一个在运行时验证它的 log4j 附加程序。

然而,我们想要的是向错误方法添加一个额外的参数。 例如:

logger.error( "E1234", "发生错误" );

这可以通过为 log4j Logger 类实现一些外观类来处理。

还有其他人有类似的问题吗? 你的解决方案是什么? 现在,我们更喜欢第一个代码示例。 将来我们可能会实现代码分析器在夜间构建期间执行(或者有人知道一些现有的代码分析器可以配置为检测 error() 方法调用中丢失的文本 ID 吗?)

I'm developing a (internal) library where I want to enforce that developers using this lihrary include a text ID when logging error or fatal level messages. Without modifying log4j, what we want to enforce is similar to:

logger.error( "E1234:An error has occured" );

With none or minor extension to log4j we could either, during nightly builds, scan the source code to verify that the text ID is included, or implement a log4j appender that verifies it during runtime.

What we would like, however, is to add an extra argument to the error-method. E.g.:

logger.error( "E1234", "An error has occured" );

This could be handled by implementing some facade class for the log4j Logger class.

Have anyone else had similar issue? What was your solution? For now, we prefer the first code example. In the future we might implement the code analyzer to execute during nightly builds (or do anyone know about some existing code analyzer that can be configured to detect a missing text ID from an error() method call?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤君无依 2024-07-21 00:05:31

避免像这样的复合键:“E1234:发生错误”

我会使用

public enum Error {
 E1234("E1234", "An error has occured"),
 E1245("E1235", "Other error has occured"),
 private final String code;
 private final String description;
 Error(code, description) {
   this.code;
   this.description = description
 }
 @Override
 public String toString() {
   return this.code + ": " + this.description;
 }
}

然后,您可以使用接受错误的方法创建自己的接口:

public interface MyLogger {
 void error(Error);
 void info(Error);
 void warn(Error);
 void debug(Error);
}

然后实现它并委托给 log4j 实际日志记录作业。 这将帮助您的开发人员避免出现问题。

Avoid compound keys like this: "E1234:An error has occured"

I would use

public enum Error {
 E1234("E1234", "An error has occured"),
 E1245("E1235", "Other error has occured"),
 private final String code;
 private final String description;
 Error(code, description) {
   this.code;
   this.description = description
 }
 @Override
 public String toString() {
   return this.code + ": " + this.description;
 }
}

Then, you can create your own interface with methods which accept an Error:

public interface MyLogger {
 void error(Error);
 void info(Error);
 void warn(Error);
 void debug(Error);
}

Then implement it and delegate to log4j real logging job. This will help your developer avoiding problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文