I am using Java 1.7 and Log4j2 (beta9) and I have the following log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{dd/MMM/yyyy HH:mm:ss.SSS}]> %-5p - %m%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/foo.log" filePattern="logs/foo-%d{dd-MMM-yyyy}-%i.log">
<Policies>
<OnStartupTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy fileIndex="max" max="10"/>
<PatternLayout pattern="[%d{dd/MMM/yyyy HH:mm:ss.SSS}]> %-5p - %m%n"/>
</RollingFile>
<Async name="ASYNC">
<AppenderRef ref="RollingFile"/>
<AppenderRef ref="CONSOLE"/>
</Async>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="ASYNC"/>
</Root>
</Loggers>
</Configuration>
And as far as the logging itself goes, it does what I need.
As part of the error/exception sequence, I need to send an email with the log file as an attachment.
How do I programmatically get the 'fileName="logs/foo.log"' bit? I would really hate to have to hard code this.
By default, we'll leave the Log4j2 configuration file (log4j2. xml/log4j2-spring. xml) in the project classpath or resources folder.
System. setProperty("logfilename", "a_cool_logname"); Once that is set you can go ahead and get your loggers as normal and they will log to the dynamic file (be careful of those static Loggers that create loggers before your main method executes).
System. setProperty("{my. log", "C:/logfile. log");
Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.
From the Logger
, iterate through all getAllAppenders
looking for the one that implements RollingFileAppender
, and call getFile
on it. Or you could use getAppender("RollingFile")
instead, if you don't mind hard-coding the appender's name.
For Log4j 1:
public static final Logger LOG = Logger.getLogger(YourClass.class);
public File getLoggerFile() {
Appender appender = LOG.getAppender("RollingFile");
return appender.getFile();
}
For Log4j 2: (note that this requires the non-interface logger)
public static final Logger LOG = LogManager.getLogger(YourClass.class);
public String getLoggerFileName() {
org.apache.logging.log4j.core.Logger loggerImpl = (org.apache.logging.log4j.core.Logger) LOG;
Appender appender = loggerImpl.getAppenders().get("RollingFile");
// Unfortunately, File is no longer an option to return, here.
return ((RollingFileAppender) appender).getFileName();
}
Paul's answer was correct with a minor modification:
public static String getLoggerFile( Logger log ) {
org.apache.logging.log4j.core.Logger loggerImpl = (org.apache.logging.log4j.core.Logger) log;
Appender appender = loggerImpl.getAppenders().get("RollingFile");
return ((RollingFileAppender) appender).getFileName();
}
All credit for the answer goes to Paul!
Alternative method with less class casting:
RollingFileAppender appender = (RollingFileAppender) LoggerContext.getContext().getConfiguration()
.getAppenders().get("myRfAppenderName");
String logFile = appender.getFileName();
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