简体   繁体   English

slf4j登录控制台而不是文件

[英]slf4j logging in console instead of file

I'm testing Milton WebDAV API and I need to log when some document is opened. 我正在测试Milton WebDAV API,我需要在打开某个文档时进行记录。 I can have it logging on Eclipse's console, but can't make it put the message on a external file. 我可以让它登录Eclipse的控制台,但不能让它将消息放在外部文件上。

Found several links here at SO and Google, but none worked. 在SO和谷歌找到了几个链接,但都没有。 I've spent about 4h in this already. 我已经花了大约4小时。 Any guesses? 任何猜测?

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 DocumentResource.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! 谢谢!

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. 如果没有应用记录器设置(即您没有看到日志出现在您的文件中),那么您可能在类路径中的某处使用了另一个log4j.properties文件,而不是使用此文件。 Sometimes it can be included in jar files (eek!) 有时它可以包含在jar文件中(eek!)

slf4j is just a wrapper for other loggers, one of which is log4j that 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.jar in your classpath, not slf4j-simple one 确保你的类路径中有slf4j-log4j12.jar ,而不是slf4j-simple

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

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