<configuration
debug="false" scan="false" scanPeriod="60 seconds">
<!-- snip... -->
<!-- variable used to hold desired log file name
I comment out because I don't seem to be able to override
if present
-->
<!--<property name="system.logfile.name" value="deploy" />-->
<!-- snip... -->
<timestamp key="bySecond" datePattern="yyyyMMdd'_'HH.mm.ss"/>
<!-- snip... -->
<!--
this is the appender I want to "pause" -->
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<!-- snip... -->
</appender>
<appender name="ROLLINGFILE_SYSTEM"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- snip... -->
<File>${system.logfile.name}-${bySecond}.log</File>
<!-- snip... -->
</appender>
<!-- snip... -->
</configuration>
Approaches tested for dynamic filename: 1) use SLF4J Logger and Jason Configurator, 2) use Logback Logger.
1) In the case below, the logback.xml file is processed, and a file is created with the name "system.logfile.name_IS_UNDEFINED..." as expected. I then execute the code below to change the name (I also delete the file originally created). The result is a file with the desired name is created - but no logging occurs, either to the console or the log file! If I *don't* execute the code to change the file name, logging occurs just fine. I've wasted a ton of time trying to work with this. But even if this code worked, I'd be out of luck, because use case #2 requires pasusing an appender, and for that, I need to use the Logback logger directly. So on two #2 - change the log file name and access appenders.
import
org.slf4j.LoggerFactory;
import org.slf4j.Logger;
public class Foo {
private static Logger logger = LoggerFactory.getLogger(ExecWrapper.class);
public static void main(String[] args) {
// -- attempt to change log file anme
LoggerContext lc = (LoggerContext)
LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new
JoranConfigurator();
String logFileName = "foo.bar";
lc.reset();
lc.putProperty("system.logfile.name",
logFileName);
configurator.setContext(lc);
configurator.doConfigure("logback.xml");
logger.info("test message.");
...etc.
2) In this code, I'd expect to be able to access the appenders - and not even have to use Joran. But the appender is null, and even the iterator for appenders has no elements! I'm sorry, but it's so unbelievably frustrating - I think life would be easier with plain old Java logging than with working with this. If anyone has any thoughts...
import org.slf4j.LoggerFactory;
import
ch.qos.logback.classic.Logger;
public class Foo {
private static Logger logger = (Logger) LoggerFactory.getLogger(ExecWrapper.class);
public static void main(String[] args) {
LoggerContext
lc = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender<ILoggingEvent
> fileAppender =
(RollingFileAppender<ILoggingEvent
>)logger.getAppender("ROLLINGFILE_SYSTEM");
if(fileAppender
!= null)
{
String logFileName = "foo.bar";
fileAppender.stop();
fileAppender.setFile(logFileName
+ ".log");
fileAppender.setContext(lc);
fileAppender.start();
}
Regards,
Gordon