问题描述
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">示例:您可能会在那儿找到一些想法.
希望这会有所帮助...
问题描述
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...