从log4j切换到logback [英] switch from log4j to logback

查看:245
本文介绍了从log4j切换到logback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在log4j中有此代码,我不使用任何类型的配置文件

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");

有人可以告诉我如何使用logback做同样的事情

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>

对于NTEventLogAppender,据我所知,它不存在用于注销的方法.但是将一个appter从log4j移植到logback是一个非常简单的任务,因此您应该能够创建自己的Appender.

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.

如果您需要以编程方式配置附加程序,请查看 logback文档示例:您可能会在那找到一些想法.

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

希望这对您有帮助...

Hope this helps...

这篇关于从log4j切换到logback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆