Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Log4j to use stdout?

Tags:

java

log4j

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.
like image 880
BombSite_A Avatar asked Nov 06 '14 23:11

BombSite_A


3 Answers

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
like image 105
Naresh kumar Avatar answered Nov 28 '22 23:11

Naresh kumar


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");         
 }
}
like image 30
MAD Avatar answered Nov 28 '22 23:11

MAD


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

like image 21
Paul Vargas Avatar answered Nov 29 '22 00:11

Paul Vargas