Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch from log4j to logback

I have this code with log4j, I don't use any kind of configuration files

static Logger logger = Logger.getLogger(Application.class);

...

Appender ap = new NTEventLogAppender();

SimpleLayout layout = new SimpleLayout();
Appender fp = null;
try {
    fp = new FileAppender(layout, "output.txt");
} catch (IOException e) {           
    e.printStackTrace();
}

logger.addAppender(ap);
logger.addAppender(fp);

logger.info("info");

can anybody show me how can I do the same thing with logback

like image 492
Omu Avatar asked Jan 06 '10 09:01

Omu


1 Answers

Why is it that you don't use configuration files? Is is because you change the logging configuration at runtime?

Unless you have a very specific reason to do so, configuring your logging framework using configuration files seems more reasonable to me.

If you use configuration files, your configuration might be something along those lines:

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>output.txt</file>
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%level - %msg%n</Pattern>
    </layout>
  </appender>

  <root level="debug">
    <appender-ref ref="FILE" />
  </root>
</configuration>

For the NTEventLogAppender, to my knowledge it doesn't exist for logback. But porting an appender from log4j to logback is a pretty easy task, so you should be able to create your own appender.

If you need to configure the appender programmatically, check the logback documentation and examples: you might find some ideas over there.

Hope this helps...

like image 61
Sébastien Avatar answered Sep 24 '22 06:09

Sébastien