java slf4j 登录控制台而不是文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14780886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:22:08  来源:igfitidea点击:

slf4j logging in console instead of file

javaloggingwebdavslf4jmilton

提问by paulochf

I'm testing Milton WebDAV API and I need to log when some document is opened. I can have it logging on Eclipse's console, but can't make it put the message on a external file.

我正在测试 Milton WebDAV API,我需要在打开某个文档时进行记录。我可以让它登录 Eclipse 的控制台,但不能让它把消息放在一个外部文件上。

Found several links here at SO and Google, but none worked. I've spent about 4h in this already. Any guesses?

在 SO 和 Google 上找到了几个链接,但都没有。我已经在这上面花了大约 4 小时。有什么猜测吗?

Here's the situation (tried to format as best as I could):

这是情况(尝试尽可能地格式化):

log4j.properties

log4j.properties

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=/home/paulo/workspace/MiltonTutorial/logs/log.txt
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=INFO, file

DocumentResource.java

文档资源.java

public class DocumentResource implements GetableResource,
    PropFindableResource, DeletableResource, MoveableResource,
    CopyableResource, ReplaceableResource, PropPatchableResource, MultiNamespaceCustomPropertyResource {

    private final static Logger log = LoggerFactory.getLogger(DocumentResource.class);
    Document doc;

    (...)

    @Override
    public void sendContent(OutputStream out, Range arg1,
            Map<String, String> arg2, String arg3) throws IOException,
            NotAuthorizedException, BadRequestException {
        log.info(">>> File {} opened", doc.getFileName());

        out.write(this.doc.getContent());
    }

Eclipse's console when execute 'get testfile' on a WebDAV client

在 WebDAV 客户端上执行“get testfile”时 Eclipse 的控制台

08/02/2013 18:03:15 com.ettrema.tutorial.milton.DocumentResource sendContent INFO: >>> File testfile opened

08/02/2013 18:03:15 com.ettrema.tutorial.milton.DocumentResource sendContent INFO:>>> 文件 testfile 打开

log.txt big content here

log.txt 大内容在这里

Thanks!

谢谢!

回答by Brad at Kademi

Just edit your log4j file to be something like this:

只需将您的 log4j 文件编辑为如下所示:

log4j.rootLogger=DEBUG, A2

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p %c %x - %m%n

log4j.appender.A2=org.apache.log4j.FileAppender  
log4j.appender.A2.File=/a.log  
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-5p %c %x - %m%n

log4j.logger.io.milton=TRACE

If the logger settings arent being applied (ie you're not seeing logs appear in your file) then you probably have another log4j.properties file somewhere in your classpath which is being used instead of this one. Sometimes it can be included in jar files (eek!)

如果没有应用记录器设置(即您没有看到日志出现在您的文件中),那么您的类路径中可能有另一个 log4j.properties 文件正在被使用,而不是这个文件。有时它可以包含在 jar 文件中(哎呀!)

回答by Alex Vayda

slf4jis just a wrapper for other loggers, one of which is log4jthat is used to log the stuff to the file (the one is configured by log4j.properties).

slf4j只是其他记录器的包装器,其中之一log4j用于将内容记录到文件中(一个由 配置log4j.properties)。

Most likely the rest of the system is using Log4J's API while your SLF4 is configure to log to the console.

很可能系统的其余部分正在使用 Log4J 的 API,而您的 SLF4 配置为登录到控制台。

Make sure you have slf4j-log4j12.jarin your classpath, not slf4j-simpleone

确保你slf4j-log4j12.jar的类路径中有,而不是slf4j-simple一个