Play Framework - change the location of the log file

I am using external logback uing file "Dlogger.file" as follows,

.....    -Dconfig.file="C:\temp\application.conf" **-Dlogger.file="c:\temp\logback.xml"** -Dpidfile.path=NULL -Dhttps.port=443 -Dhttp.por ..............

      

and my logback.xml

file looks like this:

<configuration>    
  <conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
 <file>${application.home}/application.log</file>
 <encoder>enter code here
   <pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
 </encoder>
</appender>

      

Instead ${application.home}

(in a file logback.xml

) I want to replace it with a key defined in application.conf like

application.logpath="c:/temp"

      

or in other words, I want to determine the location of the log file (path) in application.conf.

+3


source to share


2 answers


Add under <configuration>

:

<property resource="application.conf" />

      



then use ${application.logpath}

:

<file>${application.logpath}/application.log</file>

      

+6


source


The easiest way to achieve this is:

...  -Dapplication.home="C:/temp/somedir" 

      

But unfortunately it adds another parameter to your launch command.


The reason your current solution is not working is simply because the Logback is configured at class load time, but this is the point where your config.file has not been loaded yet.



So, to get it working, you can use one little hack: re-initialize the log after startup:

import play.*;

public class Global extends GlobalSettings {
    @Override
    public void onStart(Application app) {
        reloadLoggerContext();

        Logger.info("Application has started");
    }

    @Override
    public void onStop(Application app) {
        // ...
    }

    private void reloadLoggerContext() throws JoranException {
        // Hack to use system properties inside logback.xml
        LoggerContext ctx = new LoggerContext();
        ctx.reset();
        new ContextInitializer(ctx).autoConfig();
    }
}

      

It will work if Play! Framework will export application.home

as a system property or environment variable. If it doesn't (I don't know Play! Well) then you can do it yourself with sth like this:

System.setProperty("application.home", app.configuration().getString("application.home"));

+2


source







All Articles