简体   繁体   English

如何将 log4j 输出文件设置为相对于我的 jar?

[英]how to set log4j output file to be relative to my jar?

I have an intellij java project.我有一个intellij java 项目。

I have added this log4j.properties我已经添加了这个log4j.properties

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log

How can I set the log4j.appender.file.File to be relative to my project location?如何将log4j.appender.file.File设置为相对于我的项目位置?

How can I set the output file to be:如何将输出文件设置为:

src/main/resources/logs/log.txt ? src/main/resources/logs/log.txt ?

You can use .\\your file name .您可以使用.\\your file name

.\\ is used to refer your project location. .\\用于引用您的项目位置。

eg .\\abc.log , in case of eclipse this file is present in your project folder in workspace.例如.\\abc.log ,在 eclipse 的情况下,此文件存在于工作区的项目文件夹中。

In the case of jar this file will be written in a folder in which your .jar file is present.在 jar 的情况下,此文件将写入您的 .jar 文件所在的文件夹中。

I couldn't find straight-forward solution via setting the right property in log4j.properties.我无法通过在 log4j.properties 中设置正确的属性来找到直接的解决方案。 However I can propose two solutions.但是我可以提出两种解决方案。 First, setting relative path in properties, makes that path relative not to the project (or jar), but the directory where you run your app from.首先,在属性中设置相对路径,使该路径不是相对于项目(或 jar),而是相对于您运行应用程序的目录。 So if you are creating for example a service which calls your jar, it will call it from /etc/init.d directory and your logs (most likely) will go nowhere.因此,例如,如果您正在创建一个调用 jar 的服务,它将从 /etc/init.d 目录中调用它,并且您的日志(很可能)将无处可去。

One of the options is to set an environment variable, where you would like to have your logs.选项之一是设置一个环境变量,您希望在其中放置日志。 Log4j handles environment variables. Log4j 处理环境变量。 Just set log4j.appender.file.File=${varname}只需设置log4j.appender.file.File=${varname}

Another option, if you are using shell script to start your service/app/jar, cd to its directory before running it (assuming you have your path set log4j.appender.file.File=./logs/myOutput.log ).另一种选择,如果你使用 shell 脚本来启动你的 service/app/jar,在运行它之前 cd 到它的目录(假设你的路径设置为log4j.appender.file.File=./logs/myOutput.log )。 Something like this:像这样的东西:

process="/path/to/your/jar/test.jar"
processName=`basename $process`
processDir=`dirname $process`

cd $processDir
java -jar "./$processName"

in this case your logs will be in /path/to/your/jar/logs/myOutput.log在这种情况下,您的日志将位于/path/to/your/jar/logs/myOutput.log

The way to output a log file to a directory relative to a jar.将日志文件输出到相对于 jar 的目录的方式。

Set a jar directory to ThreadContext in a java file,在java文件中将jar目录设置为ThreadContext,

    Path jarDir = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation()
    .toURI()).getPath().getParent(); // get a jar directory
    ThreadContext.put("jarDir", jarDir.toString()); // This needs to be done before `LogManager.getLogger()`
    Logger logger = LogManager.getLogger();

and use it in a configuration file.并在配置文件中使用它。

    <File name="File" fileName="${ctx:jarDir}/logfile.log">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %msg" charset="UTF-8" />
    </File>

References:参考资料:
How to get the path of a running JAR file? 如何获取正在运行的 JAR 文件的路径?
http://logging.apache.org/log4j/2.x/manual/thread-context.html http://logging.apache.org/log4j/2.x/manual/thread-context.html
http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM