1

I am trying to change the location of the file depending on the OS type using SL4J 2.5. Looking up at other Stack overflow questions here and here. I came up with the following:

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" name="JCal">
    <Appenders>
        <File name="MyFile" fileName="${sys:JCalEnv}">
            <PatternLayout>
                <Pattern>%d %p [%t] %c{2} - %m%n</Pattern>
            </PatternLayout>
            <!--<HTMLLayout>-->
                <!--<title>JStreamer-log</title>-->
            <!--</HTMLLayout>-->
        </File>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"/>
        </Console>
        <Async name="Async">
            <AppenderRef ref="MyFile"/>
        </Async>
    </Appenders>
    <Loggers>
        <Root level="error">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="Async"/>
        </Root>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="Async"/>
        </Root>
    </Loggers>
</Configuration>

Test.java

public class Test extends Application {

    static {System.setProperty("JCalEnv", getUserAppDirectory());}

    public static final Logger logger = LogManager.getLogger();
    public static double JAVA_VERSION = getVersion();


    public static void main(String[] args) {
        launch(args);
    }

    static String getUserAppDirectory() {
        General g = new General();
        if (g.isMacOS()) {
            return System.getProperty("user.home") + "/.JCal/logs/JCal-log.log";
        }
        else {
            return System.getProperty("user.home") + "/JCal/logs/JCal-log.log";
        }
    }

}

When ever I run the file, LOG4J creates ${sys/JCalEnv} file with logs in it. Could anyone tell me how could I give its location and the name depending on the OS type?

Note: There is . folder for OS X file.

Community
  • 1
  • 1
Akshay
  • 2,622
  • 1
  • 38
  • 71
  • IIRC, you can fully configure Log4J through its API at runtime. – JimmyB Dec 27 '15 at 01:46
  • @HannoBinder any idea on how to do that? – Akshay Dec 27 '15 at 01:49
  • 1
    Frankly, I have no regard for either SL4J (it's an abstraction layer over log4j: who wants it; who needs it?) or XML format (I prefer the original log4j.properties - it's shorter and simpler). To answer your question: yes, log4j can easily be configured programmatically, at runtime: look [here](http://robertmaldon.blogspot.com/2007/09/programmatically-configuring-log4j-and.html) or [here](https://logging.apache.org/log4j/2.x/manual/customconfig.html) – paulsm4 Dec 27 '15 at 07:56

1 Answers1

1

I created a test program that is substantially similar to yours and had no problem with it on my Mac.

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

/**
 *
 */
public class FileSubstitution {

    static {
        System.setProperty("JCalEnv", getUserAppDirectory());
        System.setProperty("log4j.configurationFile", "log4j-fileName.xml");
    }

    public static final Logger logger = LogManager.getLogger();


    public static void main(String[] args) {
        logger.info("Hello, World");
    }

    static String getUserAppDirectory() {
        String osName = System.getProperty("os.name");
        System.out.println("os = " + osName);
        if (osName.contains("Mac")) {
            return  "target/.JCal/logs/JCal-log.log";
        }
        else {
            return  "target/JCal/logs/JCal-log.log";
        }
    }
}

log4j-fileName.xml has the same contents as the configuration you posted above.

rgoers
  • 8,696
  • 1
  • 22
  • 24