How do I get Log4j to use stdout?

I am currently using log4j in some code that I am working on for debugging purposes. I am running my code with java -jar test.jar | tee file.txt

to enter a file, but now I want to be able to access the file I am entering while it is still running, which is what tee

it cannot do. I'm doing it now

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 doesn't fit my file, it does fit my console, but it println()

works fine. Does anyone know why this is happening and how to fix it?

Thank.

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 am getting the following error when starting 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.

      

+3


source to share


3 answers


Example taken from this link Using Log4j for Debugging in Java . You may 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");         
 }
}

      

0


source


Add this to 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

      

+2


source


The file log4j.properties

or log4j.xml

should be in the default package. eg:.

Default package

If you are using Maven, under "src / main / resources":

resources

0


source







All Articles