1

I have an intellij java project.

I have added this 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?

How can I set the output file to be:

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

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • You shouldn't include src/main/resources, because this will first be only available in your IDE/project source and secondly end up as your classpath in a container like a jar or a war. – Stefan May 24 '16 at 11:32

3 Answers3

5

You can use .\your file name.

.\ is used to refer your project location.

e.g. .\abc.log, in case of eclipse this file is present in your project folder in workspace.

In the case of jar this file will be written in a folder in which your .jar file is present.

Peter David Carter
  • 2,548
  • 8
  • 25
  • 44
  • does it changes when I run test or prod code? test code is logged if the log4j.properties is in `src/main/resources`? – Elad Benda May 25 '16 at 12:49
  • ./ just represent relative path to the project. – Ankur Sharma May 26 '16 at 04:30
  • i'm still missing something in the properties file. can you have a look here http://stackoverflow.com/questions/37449034/why-is-log4-appender-member-is-null-cannot-init-log4j?noredirect=1#comment62400062_37449034 ? – Elad Benda May 26 '16 at 05:36
  • If you're in windows and you are using the backslash to designate directories, then you need a double backslash: log4j.appender.file.File=.\\target\\logfile.txt Otherwise the backslash escapes the first character of your path (in my case, the "t" in target. It gives the error: log4j:ERROR setFile(null,true) call failed. java.io.FileNotFoundException: . arget\logfile.txt (The filename, directory name, or volume label syntax is incorrect) – Tihamer Jan 27 '22 at 20:15
0

I couldn't find straight-forward solution via setting the right property in 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. 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.

One of the options is to set an environment variable, where you would like to have your logs. Log4j handles environment variables. Just set 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). 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

jackal
  • 1,143
  • 17
  • 32
0

The way to output a log file to a directory relative to a jar.

Set a jar directory to ThreadContext in a java file,

    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?
http://logging.apache.org/log4j/2.x/manual/thread-context.html
http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

kena0ki
  • 479
  • 6
  • 9