log4j log file in user home directory

后端 未结 4 440

I am working on an application that will run on OSX and windows. I want the logs to be written to the users home directory. For OSX it will be under the /Users//Library/Applicat

4条回答
  •  我在风中等你
    2021-02-07 17:13

    Thank you all for the inputs. based on the hint that Alex proposed I went with the following approach,

    In log4j.properties I had the following configuration

    log4j.appender.FILE=org.apache.log4j.RollingFileAppender
    log4j.appender.FILE.File=${userApp.root}/logs/myapp.log
    

    and at the start of the application I have done this.

    System.setProperty("userApp.root", getUserAppDirectory());
    

    the getUserAppDirectory() method is defined as

    static String getUserAppDirectory() {
        if (isMacOS())
            return System.getProperty("user.home") + "/Library/Application Support/myapp";
        else
            return System.getenv("APPDATA") + "/myapp";
    }
    

提交回复
热议问题