Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback - using appenders per method, rather then class

I have a class with several methods. I would like each method to output to a different log file. With a logback.xml file like this, it logs ALL logging calls from the class.

<logger name="com.mincom.ellipse.conversion.BatchConverter" level="INFO">
   <appender-ref ref="FILE" />
</logger>

How do I get per method logging calls. I'm sure it's very simple, but I cannot seem to see the answer in the doco.

like image 707
Dominic Bou-Samra Avatar asked Jul 05 '11 23:07

Dominic Bou-Samra


People also ask

What are Appenders in Logback?

What is an Appender? Logback delegates the task of writing a logging event to components called appenders. Appenders must implement the ch.qos.logback.core.Appender interface.

Does Logback use SLF4J?

Logback natively implements the SLF4J API.

Is Logback better than Log4j?

Log4J comes out the clear winner here, being able to write almost 270% more lines than JUL, 12.5% more than logback, 52% more than SLF4J SL.


1 Answers

Haven't used logback, but in log4j and others you can setup loggers with any name you like. Using the classes package and name is just a convention. So I'd setup Multiple loggers in your class, something like this:

Logger logA = LogFactory.getLogger("LogA");
Logger logB = LogFactory.getLogger("LogB");

public void methodA() {
    logA.debug(...);
}
public void methodB() {
    logB.debug(...);
}

And then in your logback setup ...

<logger name="LogA" level="INFO">
   <appender-ref ref="FILE-A" />
</logger>

<logger name="LogB" level="INFO">
   <appender-ref ref="FILE-B" />
</logger>

Should work. Probably needs some tweaking :-)

like image 164
drekka Avatar answered Sep 21 '22 22:09

drekka