用户主目录中的log4j日志文件 [英] log4j log file in user home directory
问题描述
我正在开发将在OSX和Windows上运行的应用程序.我希望将日志写入用户的主目录.对于OSX,它将位于/Users//Library/Application Support/MyApp/log目录下,并且位于Windows下,具体取决于/Users//AppData/MyApp/log目录下的版本.
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.
期待您的投入.
由于日志文件的位置取决于操作系统,因此我希望找到一个运行时解决方案,可能类似于以下内容
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"
谢谢
推荐答案
将ConsoleAppender
更改为FileAppender
.据我所知,写入请求将重定向到Windows OS上的appdata.不确定MacO.
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());
并像这样编辑log4j配置
and edit log4j config like this
log4j.appender.NotConsole=org.apache.log4j.RollingFileAppender
log4j.appender.NotConsole.fileName=${app.root}/fileName.log
或对于用户家庭:
log4j.appender.NotConsole.fileName=${user.home}/fileName.log
这篇关于用户主目录中的log4j日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!