简体   繁体   中英

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM