I use a external logback file uing "Dlogger.file" as follow,
..... -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 lile 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 of ${application.home}
(in the logback.xml
file) I want to replace it with a key defined in the application.conf like
application.logpath="c:/temp"
or in other words I want to define the log file location(path) in the application.conf.
Add underneath <configuration>
:
<property resource="application.conf" />
then use ${application.logpath}
:
<file>${application.logpath}/application.log</file>
The most simple way to achieve this would be:
... -Dapplication.home="C:/temp/somedir"
But unfortunately it appends one more parameter to your startup command.
The reason why your current solution doesn't work is simply because Logback configures itself during class loading, but it is the moment when your config.file is not loaded yet.
So to make it work you can use one little hack: re-initialize logback 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 system property or an environment variable. If it won't happen (I don't know Play! well), then you can do it by yourself with something like this:
System.setProperty("application.home", app.configuration().getString("application.home"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With