8

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.
Pokechu22
  • 4,984
  • 9
  • 37
  • 62
BombSite_A
  • 310
  • 2
  • 5
  • 13

3 Answers3

8

please add this into your log4j.properties file

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
Naresh kumar
  • 1,069
  • 1
  • 7
  • 21
4

Example taken from this link Using Log4j for debugging in Java. You might be missing BasicConfigurator.configure();

public class Log4jTest {
  // Initialize logger for instance Log4jTest
  static Logger log  = Logger.getLogger(Log4jTest.class);

  public static void main (String[] args) {
     // Basic configurator to log debug messages to the console
     BasicConfigurator.configure();         
      
     // Add some log messages
     log.debug("This is a debug message");
     log.trace("This is a trace message");         
 }
}
MAD
  • 165
  • 4
  • Thank you. BasicConfigurator.configure() worked for me. Something to note, you need to make sure BasicConfiugrater.configure() comes AFTER you call System.setOut(), otherwise it won't work. – BombSite_A Nov 10 '14 at 22:27
  • Fixed the broken link above. – MAD Mar 11 '22 at 09:06
1

The file log4j.properties or log4j.xml must be in the default package. e.g.:

Default package

Of if you are using Maven, in "src/main/resources":

resources

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148