By default, Log4j
logs to standard output and that means you should be able to see log messages on your Eclipse's console view. To log to a file you need to use a FileAppender
explicitly by defining it in a log4j.properties
file in your classpath.
Create the following log4j.properties
file in your classpath. This allows you to log your message to both a file as well as your console.
log4j.rootLogger=debug, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=example.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%p %t %c - %m%n
Note: The above creates an example.log in your current working directory (i.e. Eclipse's project directory) so that the same log4j.properties could work with different projects without overwriting each other's logs.
References:
Apache log4j 1.2 - Short introduction to log4j