3

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.

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

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

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

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

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

log.txt big content here

Thanks!

paulochf
  • 690
  • 2
  • 11
  • 21
  • What you're saying is that while most of the log messages end up in log.txt the output from `DocumentResource` (and only this) ends up on the console? Might you have more than one `log4j.properties` somewhere on the classpath? – Marcel Stör Feb 08 '13 at 20:43
  • Actually I'm not interested in the contents that log.txt has now, only those what I would like to log using `log.info("...")`, which isn't logging on the file, just on the console. – paulochf Feb 12 '13 at 22:05

2 Answers2

1

Just edit your log4j file to be something like this:

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!)

paulochf
  • 690
  • 2
  • 11
  • 21
Brad at Kademi
  • 1,300
  • 8
  • 7
  • Hey Brad! I'm sure that isn't another .properties file anywhere, but I'm going to try your suggestion. =) – paulochf Feb 12 '13 at 22:01
  • It keeps logging just messages from `org.hibernate...`, `io.milton` is not on the file. – paulochf Feb 14 '13 at 16:46
  • 1
    I realized now that your .properties last line references `io.milton`. Shouldn't it reference the DocumentResource.java package? – paulochf Feb 14 '13 at 16:53
0

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

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

Make sure you have slf4j-log4j12.jar in your classpath, not slf4j-simple one

Alex Vayda
  • 6,154
  • 5
  • 34
  • 50