用户主目录下的log4j日志文件[英] log4j log file in user home directory

本文是小编为大家收集整理的关于用户主目录下的log4j日志文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在处理将在OSX和Windows上运行的应用程序.我希望将日志写给用户主目录.对于OSX,它将在/用户//library/application支持/myapp/log Directory下方,并取决于Windows的不同版本,以下版本//用户//appdata/myapp/log Directory.

我能做到的最好的方法是什么?我已经寻找解决方案的解决方案,但是没有什么有用的或我舒适使用的解决方案.

期待您的输入.

编辑: 由于日志文件的位置取决于操作系统,我希望找到一个运行的时间解决方案,可能是下面的

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

谢谢

推荐答案

将ConsoleAppender更改为FileAppender.据我所知,写作请求将被重定向到Windows OS上的AppData.不确定macos.

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

和编辑log4j config,例如此

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

或用于用户家庭:

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

请注意,log4j 2需要sys:前缀 - 感谢@sgrubsmyon

其他推荐答案

谢谢大家的输入.基于Alex提出的提示,我采用以下方法,

在log4j.properties中我有以下配置

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

,在应用程序的开始时,我已经完成了.

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

getUserAppDirectory()方法定义为

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

其他推荐答案

可能是最清洁的方法是写下您的log4j配置,假设特定系统属性(例如myapp.data.dir)

log4j.appender.logfile.fileName=${myapp.data.dir}/logs/myapp.log

并以适当的方式在每个平台的发射器中以适当的方式设置该属性.例如,如果您在Mac OS X上使用.app捆绑包,则可以在Info.plist

中设置系统属性
<plist version="1.0">
<dict>
    <!-- ... -->
    <key>Java</key>
    <dict>
        <!-- ... -->
        <key>Properties</key>
        <dict>
            <key>apple.laf.useScreenMenuBar</key>
            <string>true</string>
            <key>myapp.data.dir</key>
            <string>$USER_HOME/Library/Application Support/MyApp</string>

或将其相对于Windows上的AppData环境变量设置(该变量将处理XP和7之间的差异).有了 lunage4j .exe您可以将-Dmyapp.data.dir="%APPDATA%\MyApp"放在 .l4j.ini file .

您需要明确的代码来初始化log4j,然后再尝试访问任何记录器

if(System.getProperty("myapp.data.dir") == null) {
  // fallback to ~/.myapp (sensible Linux default) if run without a launcher
  System.setProperty("myapp.data.dir", new File(
        System.getProperty("user.home"), ".myapp").getAbsolutePath());
}
// ensure log directory exists
new File(new File(System.getProperty("myapp.data.dir")), "logs").mkdirs();
// now it's safe to configure log4j
PropertyConfigurator.configure(this.getClass().getResource("/log4j.properties"));

本文地址:https://www.itbaoku.cn/post/1574871.html

问题描述

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

推荐答案

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

其他推荐答案

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";
}

其他推荐答案

Possibly the cleanest approach would be to write your log4j configuration assuming a particular system property (say myapp.data.dir)

log4j.appender.logfile.fileName=${myapp.data.dir}/logs/myapp.log

and set that property in the appropriate way in the launcher for each platform. For example if you're using an .app bundle on Mac OS X then you can set system properties in Info.plist

<plist version="1.0">
<dict>
    <!-- ... -->
    <key>Java</key>
    <dict>
        <!-- ... -->
        <key>Properties</key>
        <dict>
            <key>apple.laf.useScreenMenuBar</key>
            <string>true</string>
            <key>myapp.data.dir</key>
            <string>$USER_HOME/Library/Application Support/MyApp</string>

or set it relative to the APPDATA environment variable on Windows (which would handle the difference between XP and 7). With a Launch4J .exe you could put -Dmyapp.data.dir="%APPDATA%\MyApp" in the .l4j.ini file.

You'd need explicit code to initialize log4j before you try and access any loggers

if(System.getProperty("myapp.data.dir") == null) {
  // fallback to ~/.myapp (sensible Linux default) if run without a launcher
  System.setProperty("myapp.data.dir", new File(
        System.getProperty("user.home"), ".myapp").getAbsolutePath());
}
// ensure log directory exists
new File(new File(System.getProperty("myapp.data.dir")), "logs").mkdirs();
// now it's safe to configure log4j
PropertyConfigurator.configure(this.getClass().getResource("/log4j.properties"));