Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically find the log4j2 (beta9) log filename

Tags:

java

log4j2

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.

like image 238
Dennis Avatar asked Feb 07 '14 17:02

Dennis


People also ask

Where is the Log4j2 configuration file?

By default, we'll leave the Log4j2 configuration file (log4j2. xml/log4j2-spring. xml) in the project classpath or resources folder.

How do you give a dynamic file name in the Appender in Log4j properties?

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).

How do you set log file path in Log4j properties dynamically?

System. setProperty("{my. log", "C:/logfile. log");

What is status in Log4j2 properties file?

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.


3 Answers

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();
}
like image 129
Paul Hicks Avatar answered Oct 14 '22 06:10

Paul Hicks


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!

like image 23
Dennis Avatar answered Oct 14 '22 06:10

Dennis


Alternative method with less class casting:

        RollingFileAppender appender = (RollingFileAppender) LoggerContext.getContext().getConfiguration()
        .getAppenders().get("myRfAppenderName");
    String logFile = appender.getFileName();
like image 30
Dimitar II Avatar answered Oct 14 '22 08:10

Dimitar II