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

本文是小编为大家收集整理的关于从log4j切换到logback的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我使用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");

任何人都可以向我展示如何使用logback

做同样的事情

推荐答案

为什么不使用配置文件?是因为您在运行时更改日志记录配置吗?

除非您有非常具体的理由,否则使用配置文件配置您的记录框架似乎更合理.

如果您使用配置文件,则您的配置可能与这些行:

<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,据我所知,登录不存在.但是,将appender从log4j到logback是一项非常容易的任务,因此您应该能够创建自己的appender.

如果您需要以编程方式配置Appender,请检查 logback document =" http://github.com/ceki/logback/tree/master/logback-examples/" rel =" nofollow noreferrer">示例:您可能会在那儿找到一些想法.

希望这会有所帮助...

本文地址:https://www.itbaoku.cn/post/1574928.html

问题描述

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

相关标签/搜索