自定义日志级别 [英] Custom Log Level

查看:164
本文介绍了自定义日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我现有的应用程序 org.apache.log4j 中,API已在java代码中使用。

In my existing application "org.apache.log4j" API's have been used in java code.

要求:

我必须在日志文件中记录一些语句(比如xyz),并且不应该依赖于日志级别。例如:如果我的日志级别是错误然后xyz应该打印,如果我的日志级别是调试然后xyz应该打印。

I have to log some statement(say xyz) in log file in any case and should not dependent of log levels.For example : if my log level is error then also xyz should print, if my log level is debug then also xyz should print.

我不能使xyz的日志语句是调试因为如果我这样做,其他除了xyz之外的日志语句也将开始打印。

I cannot make log statement of xyz is debug because if i do this, other log statements apart from xyz will also start printing.

为此,我相信,我必须添加一些自定义日志级别。请帮助如何做以及如何设置它的级别排序,以便在任何情况下都应该打印。

For this, I believe, I have to add some custom log level.Please help how to do it and how to set its level ordering so that in any case it should print.

提前致谢。
最好的问候

Thanks in advance. Best Regards

推荐答案

你能做的是创建一个不同的 Logger 用于那些语句(定义记录器时不限制使用类名)

What you could do is create a different Logger for those statements (you are not restricted to use classes names when defining a logger)

// Standard logger
private static Logger log = Logger.getLogger(MyClass.class)

// XYZ logger
private static Logger logXYZ = Logger.getLogger("logs.xyz");

您可以从多个类访问相同的记录器,只需传递相同的标签。

You can access the same logger from several class, you just have to pass the same label.

然后,在配置文件中,您可以为该类别定义不同的日志级别,甚至可以在不同的appender中输出这些日志(不同的文件,处理等)

Then, in the configuration file, you can define a different log level for that category, and even output these logs in a different appender (different file, processing, etc.)

这篇关于自定义日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆