I'm currently using log4j in some code I'm working on for debugging purposes. I've been running my code with java -jar test.jar | tee file.txt
to log to a file but now I want to be able to switch the file I'm logging to while it's still running, something that tee
can't do. Right now I'm doing this
private static final Logger log = LoggerFactory.getLogger(Test.class);
public void main() {
File file = new File(/path/to/file);
System.setOut(new PrintStream(file));
System.out.println("hello world"); //This works.
log.info("hello world"); //this doesn't.
For some reason the Logger output isn't going to my file, it's going to my console, but println()
works fine. Does anyone have any idea why this is happening and how to fix this?
Thanks.
Edit: Here's my log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!--<param name="ConversionPattern" value="%d %-5p [%c] %m%n" />-->
<param name="ConversionPattern" value="%d %-5p [%c:%L] %m%n" />
</layout>
</appender>
<logger name="com.company">
<level value="trace" />
</logger>
<logger name="org.springframework">
<level value="debug" />
</logger>
<logger name="com.company.Selenium">
<level value="debug" />
</logger>
<logger name="org.springframework">
<level value="warn" />
</logger>
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Also I'm getting the following error when I run my project
log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.