Finding Log4J’s Log Files

Posted on 2008-12-08 in java

On a few occasions lately, I’ve logged into remote machines to look at Java log files, and then spent two minutes figuring out where to find them. I wrote this bit of code to wrangle the log file locations from the Log4J API, which I use write out the file paths to a known location on startup. If you have a somewhat complex multi-environment configuration, or generate multiple log files per application, then this code snippet may prove helpful.

Since the Log4J API is still Enumeration-based, there are some ugly old-school loops in there. All types are from the java.util or org.apache.log4j packages.

private static Map<String,String> getLogLocations() {

  Collection<Logger> allLoggers = new ArrayList<Logger>();

  Logger rootLogger = Logger.getRootLogger();

  for (Enumeration<Logger> loggers =
          rootLogger.getLoggerRepository().getCurrentLoggers() ;
          loggers.hasMoreElements() ; ) {


  Set<FileAppender> fileAppenders =
        new LinkedHashSet<FileAppender>();

  for (Logger logger : allLoggers) {
    for (Enumeration<Appender> appenders =
            logger.getAllAppenders() ;
            appenders.hasMoreElements() ; ) {

      Appender appender = appenders.nextElement();
      if (appender instanceof FileAppender) {

        fileAppenders.add((FileAppender) appender);

  Map<String, String> locations =
        new LinkedHashMap<String,String>();

  for (FileAppender appender : fileAppenders) {
    locations.put(appender.getName(), appender.getFile());

  return locations;

The code just grabs the root logger, finds all the other loggers via its LoggerRepository, and then finds the set of all FileAppenders attached to them. It returns a Map of appender name to logfile location (both Strings), in the order they are declared in your configuration.

Not really the world’s most elegant code. Despite the clunky Log4J API, I can’t help thinking that would take no more than two or three lines in Python or Ruby.

Written by phil on December 8th, 2008

Tagged with ,


2 Responses to 'Finding Log4J’s Log Files'

Subscribe to comments with RSS or TrackBack to 'Finding Log4J’s Log Files'.

  1. Excellent! Was googling to see if anyone had this code for the exact same reason. Thanks so much for sharing!

    David Blevins

    16 Mar 11 at 10:23 pm

  2. Thanks! This was wonderful!


    18 Dec 12 at 3:12 pm

Leave a Reply