简体   繁体   English

从log4j切换到logback

[英]switch from log4j to logback

I have this code with log4j, I don't use any kind of configuration files 我在log4j中有此代码,我不使用任何类型的配置文件

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 有人可以告诉我如何使用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. 对于NTEventLogAppender,据我所知,它不存在用于注销的方法。 But porting an appender from log4j to logback is a pretty easy task, so you should be able to create your own appender. 但是将一个appter从log4j移植到logback是一个非常简单的任务,因此您应该能够创建自己的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... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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