[ 
https://issues.apache.org/jira/browse/LOG4J2-365?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13745672#comment-13745672
 ] 

Remko Popma commented on LOG4J2-365:
------------------------------------

Here is an example launcher that makes sure the log4j jars are in the classpath 
of the child process:

{code}
package a;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

class ChildProcess {
    public static void main(String[] args) throws Exception {
        Logger log = LogManager.getLogger(ChildProcess.class);
        log.error("Hello world!");
    }   
}

public class Launcher {

    public static void main(String[] args) throws Exception {
        ProcessBuilder pb = new ProcessBuilder( //
                "java", //
                "-Dlog4j.configurationFile=log4j2.xml", //
                
                // THIS IS IMPORTANT!
                // Log4j jars must be in classpath of CHILD process!
                //
                // One (easy) way to do this is to pass the classpath of
                // the parent process to child process...
                // Then start laucher with the log4j jars in the classpath.
                "-cp", System.getProperty("java.class.path"), //
                //
                // Another way would be to set the individual jars & 
directories...
                //"-cp", 
"lib/log4j-api-2.0-beta8.jar;lib/log4j-core-2.0-beta8.jar",
                
                // class name of the child process
                ChildProcess.class.getName()
                );

        pb.redirectErrorStream(true); // merge System.out and System.err
        final Process process = pb.start();

        final boolean[] stop = { false };
        printProcessOutput(process, stop);
        process.waitFor();
        stop[0] = true;
    }

    private static Thread printProcessOutput(final Process process,
            final boolean[] stop) {

        Thread t = new Thread("OutputWriter") {
            @Override
            public void run() {
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        process.getInputStream()));
                try {
                    String line = null;
                    while (!stop[0] && (line = in.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (Exception ignored) {
                }
            }
        };
        t.start();
        return t;
    }
}
{code}

This child process logs correctly to the console. Here is the command:
{code}java -cp bin;lib/log4j-api-2.0-beta8.jar;lib/log4j-core-2.0-beta8.jar 
a.Launcher{code}

I get the following output:

{code}
09:37:51.310 [main] ERROR a.ChildProcess - Hello world!
{code}
                
> ERROR StatusLogger Unable to locate a logging implementation, using 
> SimpleLogge
> -------------------------------------------------------------------------------
>
>                 Key: LOG4J2-365
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-365
>             Project: Log4j 2
>          Issue Type: Question
>          Components: log4j 1.2 emulation
>    Affects Versions: 2.0-beta7
>         Environment: windows7
>            Reporter: wei wang
>            Priority: Blocker
>              Labels: ERROR, SimpleLogge, StatusLogger, Unable, a, 
> implementation,, locate, logging, to, using
>             Fix For: 2.0-beta7
>
>   Original Estimate: 12h
>  Remaining Estimate: 12h
>
> I have problem to make the logging work with the log4j2.xml  when packing 
> into jar file.
> I have launcher.jar which calls oamcli.jar(has logging implementation and log 
> is working in eclipse)
> How ever when I try to run :
> java -Dlog4j.configurationFile=path\to\log4j2.xml  -cp  "launcher.jar; lib\*" 
> .....
> I got a "ERROR StatusLogger Unable to locate a logging implementation, using 
> SimpleLogger"
> Please give me some advice and where to look for the answer. 
> Thanks!

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-dev-h...@logging.apache.org

Reply via email to