Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j log file in user home directory

Tags:

java

log4j

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/Application Support/MyApp/log directory, and under windows depending on the version under /Users//AppData/MyApp/log directory.

What is the best way I can do this? I have looked around for solutions for this, but nothing helpful or a solution I am comfortable using has come up.

Look forward to your inputs.

edit: Since the location of the log file depends on the OS, I am hoping to find a run time solution, possibly something like below

if (System.getProperty("os.name").contains("mac"))
    logFileLocation = System.getProperty("user.home") + "/Library/Application Support/MyApp/logs"
else
    logFileLocation = System.getenv("APPDATA") + "/MyApp/logs"

Thanks

like image 589
Poorav Avatar asked Jul 26 '12 11:07

Poorav


People also ask

Where is log4j log file location?

The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging.

Where should log4j properties be stored?

During Content Engine installation, two log4j sample files are placed on the system in the ContentEngine\config\samples\ folder: log4j. properties. client: A Java format file that contains client configuration settings.

What are Appenders and loggers in log4j?

Apache log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as consoles, files, sockets, NT event logs, etc. Each Appender object has different properties associated with it, and these properties indicate the behavior of that object.


2 Answers

Change the ConsoleAppender to a FileAppender. As far as I know the write request will be redirected to appdata on windows OS. Not sure about MacOs.

URL mySource = MyAppMainClass.class.getProtectionDomain().getCodeSource().getLocation();
File rootFolder = new File(mySource.getPath());
System.setProperty("app.root", rootFolder.getAbsolutePath());

and edit log4j config like this

log4j.appender.NotConsole=org.apache.log4j.RollingFileAppender
log4j.appender.NotConsole.fileName=${app.root}/fileName.log

or for user home:

// log4j 1.*
log4j.appender.NotConsole.fileName=${user.home}/fileName.log
// log4j 2.*
log4j.appender.NotConsole.fileName=${sys:user.home}/fileName.log

Note that log4j 2 requires sys: prefix - thanks to @sgrubsmyon

like image 77
Observer Avatar answered Sep 20 '22 03:09

Observer


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";
}
like image 35
Poorav Avatar answered Sep 23 '22 03:09

Poorav