如何从Log4j Logger / Appender中排除单个类? [英] How to exclude a single Class from a Log4j Logger / Appender?

查看:119
本文介绍了如何从Log4j Logger / Appender中排除单个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包com.example。这个包有五个类。我想将其中的四个类记录到一个文件中,但排除第五个类。

I have a package "com.example". This package has five classes. I want to log four of these classes to a file, but exclude the fifth class.

我可以编写四个记录器,例如logger name =com.example.Class1,并将相同的appender添加到所有四个记录器。有没有更简单的方法(让我们认为我有100个而不是5个类)?

I could write four loggers, e.g. logger name="com.example.Class1", and add the same appender to all four loggers. Is there no easier way (let us think that I have 100 instead of 5 classes)?

还有其他一些像这样的问题。但其他人只想排除一个类来记录这个类。这可以使用addivity标志来解决。但我认为可加性标志在这里不起作用,因为我不想记录第五类,而是所有其他类?!

There are some other questions like this one. But the other guys just wanted to exclude a class to log this class. This can be solved using the addivity flag. But I think the additivity flag does not work here, becasue I do not want to log the fifth class, but all other ones?!

希望有人可以帮助我吗?

Hope someone can help me out?

推荐答案

只需将第五课配置为使用日志级别OFF:

Simply configure your fifth class to use the log-level OFF:

log4j.logger.com.example=INFO, MyAppender
log4j.logger.com.example.FifthClass=OFF

实际上我建议你不要将它设置为 OFF ,而是致命 ERROR 甚至 WARN 代替。

Actually I suggest you don't set it to OFF, but to FATAL. ERROR or even WARN instead.

主要原因想要忽略某些类的日志记录通常是因为它记录的很多(超过有用并且很难读取日志)。但是,大多数时候你仍然想知道某些事情是否真的真的错误。通过将其设置为 ERROR ,您仍然可以看到真正有问题的案例,但不会被大量的 INFO 日志语句烦恼。

The main reason to want to ignore logging from some class is usually if it logs to much (more than is useful and makes it hard to read the log). However, most of the time you'd still want to know if something goes really wrong. By setting it to ERROR you can still see the real problematic cases, but not be annoyed by tons of INFO log statements.

这篇关于如何从Log4j Logger / Appender中排除单个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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