Slash90ITA
Slash90ITA

Reputation: 97

Log4j2: file logging not working with jar

I have a java application with log4j2 configured in order to log to console and to file. When i start my application from ecplise, all the log are ok (console and file).

When i start the jar of the application from CMD or Powershell (as admin), only the console log works. If i start the jar by only double click, the file log works (but no console is displayed).

This my log4j2.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">

<Appenders>

<File name="file_all" fileName="logs/ALL.log">
  <PatternLayout>
    <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n</Pattern>
  </PatternLayout>
</File>

<File name="file_error" fileName="logs/ERROR.log">
  <PatternLayout>
    <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n</Pattern>
  </PatternLayout>
</File>

<Console name="STDOUT" target="SYSTEM_OUT">
  <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>

</Appenders>

<Loggers>

<Root level="trace">
  <AppenderRef ref="file_all" level="INFO"/>
  <AppenderRef ref="file_error" level="ERROR"/>
  <AppenderRef ref="STDOUT" level="INFO"/>
</Root>
</Loggers>

</Configuration>

Upvotes: 1

Views: 4577

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

Your log4j2.xml config file starts with <configuration status="off" ... (or perhaps WARN or ERROR, you haven't shown the full config).

To investigate, switch on log4j's internal status logging by changing this to <configuration status="trace" ...

This will show output on the console. (You can redirect this to a file with java -jar myjar.jar > out.log.)

I hope this will give us more info on what is going on. Please paste that output into your question.


EDIT: the status output looks good; no errors or anything. Could it be that double-clicking a jar file is associated with javaw (not java), so there is no console? You mention that when starting the application from a CMD prompt or Powershell, you cannot find the log file. The line "Starting FileManager logs/ALL.log" in the status log tells me that log4j2 successfully created an output stream. However, this is a relative path, and the status log does not mention the absolute path. I think it is relative to the current directory, so what is the current directory of the CMD prompt or Powershell?

Upvotes: 1

Related Questions