views:

34

answers:

2

I am working on a grails app where in development mode, I log some calculations to a log file through log4j.

I want to provide a service which will read the log file and make its contents available to a developer so they can see what is being logged in the log file.

Is there a way I can get the log4j appender file name at run time?

+1  A: 

This will find the filename of the first appender:

import org.apache.log4j.*

Logger.rootLogger.allAppenders.find{it instanceof FileAppender}?.fileName
ataylor
I'll try this out. Thanks ataylor.
The Fat Oracle
+1  A: 

If your logging configuration is set up like:

log4j = {
    appenders {
        file name: 'myFileAppender', file: '/tmp/myFile.log'
    }

    debug 'myFileAppender': 'myFileLogger'
}

You can access the myFileLogger Logger's file name like this:

import org.apache.log4j.Logger
import org.apache.log4j.FileAppender

def appender = (FileAppender)Logger.getLogger('myFileLogger').getAppender('myFileAppender')
def fileName = appender.file
Rob Hruska
Excellent. Thanks Rob.
The Fat Oracle