简体   繁体   English

log4j参数化日志记录

[英]log4j parameterized logging

I want to develop a simple logger which sends logs to a server where all the logs are stored and displayed in graphs, etc. 我想开发一个简单的记录器,它将日志发送到服务器,其中所有日志都存储并以图形等形式显示。

The logger has to be parameterized since there is a structure given by the developed database for analyzing and displaying the stored logs. 记录器必须参数化,因为开发的数据库给出了用于分析和显示存储的日志的结构。

The parameters for the logs are implemented with overloaded methods, for example: 日志的参数使用重载方法实现,例如:

mylog.error(String jobname, String whatever);
mylog.error(String jobname, String whatever, List<String> whatever);

This is how the logs are implemented by a developer when needed. 这是开发人员在需要时实现日志的方式。

I wonder if I could use log4j for something like this? 我想知道我是否可以使用log4j这样的东西?

Anyway, my own simple logging works fine, sending the logs in json to my server, where they are stored, etc. 无论如何,我自己的简单日志工作正常,将json中的日志发送到我的服务器,存储它们等等。

This is how SLF4J works. 这就是SLF4J的工作原理。 Decouple logging from your application with SLF4J and you can use the Log4J connector to use it as the underlying implementation. 使用SLF4J从应用程序中解除日志记录,您可以使用Log4J连接器将其用作底层实现。 That being said, the creator of SLF4J wrote Logback to be an ideal implementation of the SLF4J interface. 话虽这么说,SLF4J的创建者将Logback写成了SLF4J接口的理想实现。 If you aren't tied organizationally to Log4J, I suggest you use it. 如果你没有在组织上与Log4J联系,我建议你使用它。

There are a number of ways to do this. 有很多方法可以做到这一点。

I tend to prefer a Mapped Diagnostic Context (MDC) (and comparison with NDC ) although I wrote my own formatter extension for displaying it as I needed ordered rendering. 我倾向于选择映射诊断上下文(MDC) (并与NDC进行比较 ),尽管我编写了自己的格式化程序扩展来显示它,因为我需要有序渲染。

Other frameworks have similar capabilities, but a mapped approach allows for some nice headings, which is handy for log parsing tools, or for DB-oriented logging. 其他框架具有类似的功能,但是映射方法允许一些很好的标题,这对于日志解析工具或面向数据库的日志记录来说很方便。

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

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