0

In log4j, is it possible to filter not only on a package / class level, but also on a method level? If yes, how?

Thanks for any hint :-)

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

1 Answers1

1

In log4j you do not "filter" (I assume you refer to the config where you define the log levels per logger) per package or class, you filter per logger name.

It just happens that most of the time one uses the name of a class to instantiate a logger, so the name of the logger ends up being the canonical name of the class. You can just give any name to your logger and then use this name to configure it.

vptheron
  • 7,426
  • 25
  • 34
  • hmm I get what you mean, so in the end one filters per logger level (so actually not really by logger name). sad, would be nice to be able to focus on only one method ): – stefan.at.kotlin Apr 10 '13 at 19:35
  • Say your method M is in your class A, you can create a logger with the name "A.M" and then only use this logger in the method M. That way you can configure it the way you want in your log4j config. – vptheron Apr 10 '13 at 19:36
  • thanks, that's an idea, but would involce changing existing code. There are some kind of filters like here: http://stackoverflow.com/questions/3730343/log4j-stringtomatch-in-property-file wondering if the filters are applied after formatting the message (I included the method name in the messages by an appropriate layout.ConversionPattern) – stefan.at.kotlin Apr 10 '13 at 19:42