Skip to content

Redirect log to file

Anthony Raymond edited this page Mar 17, 2017 · 1 revision

This project use log4j2, you'll be able to tweek logging easily.

Create the file <MY_CONFIG_FOLDER_PATH>/log4j2.xml and add this content to it:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration shutdownHook="disable">
    <Appenders>
        <File name="FileAppender" fileName="${sys:logFilename}" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
        <Async name="Async">
            <AppenderRef ref="FileAppender"/>
        </Async>
    </Appenders>
    <Loggers>
        <Logger name="org.apache.log4j.xml" level="all"/>
        <Root level="INFO">
            <AppenderRef ref="Async"/>
        </Root>
    </Loggers>
</Configuration>

This file is your logger configuration. (notice the append="false", if false, the logfile will be overwrited each time you restart joal). Also shutdownHook="disable" is required to ensure that log still works until gracefull shutdown.

And then, start your java application with :

java -Dlog4j.configurationFile=file:<MY_CONFIG_FOLDER_PATH>/log4j2.xml -DlogFilename=<LOGFILE_PATH> -jar jack-of-all-trades.jar <MY_CONFIG_FOLDER_PATH>

Be sure to :

  • Set <LOGFILE_PATH> with the absolute path of the logfile (like so: /var/log/joal.log or whatever path your want).
  • Have <MY_CONFIG_FOLDER_PATH> being an absolute path.
Clone this wiki locally