Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get tomcat log file programmatically within webapp

I'm looking for a way to retrieve the Tomcat log within a webapp. In the past I've seen this feature provided in other webapps, usually dumping the log in a Servlet.

I'm using slf4j (with log4j) and Tomcat 6. I haven't found anything relevant in the Tomcat docs, although the JMX API looks like it might provide something useful? I'm not too concerned whether the output represents just the webapp logging or the entire Tomcat log, either will suffice.

Ideally, I'm hoping for a solution that does not involve scraping the log from the filesystem, although if that is the only way, it would be great if the log directory could be calculated at runtime...

like image 222
seanhodges Avatar asked May 09 '11 11:05

seanhodges


2 Answers

Scraping the log from the filesystem is probably the easiest way to go. You can get the log directly programatically using System.getProperty("catalina.base") + "/logs".

Otherwise you could set up an additional appender in your log4j configuration to log to something like JDBC, JMS, a Writer, etc. Whatever makes sense for your app.

like image 122
sourcedelica Avatar answered Nov 01 '22 17:11

sourcedelica


This function will get the most recent log file matching a given prefix. You do not need to know what directory the logs are written to.

    public static File locateLogFile( final String prefixToMatch ) {
    File result = null;
    Handler[] handlers = LogManager.getLogManager().getLogger( "" ).getHandlers();
    try {
        for( Handler handler : handlers ) {

            Field directoryField;
            Field prefixField;
            try {
                //These are private fields in the juli FileHandler class
                directoryField = handler.getClass().getDeclaredField( "directory" );
                prefixField = handler.getClass().getDeclaredField( "prefix" );
                directoryField.setAccessible( true );
                prefixField.setAccessible( true );
            } catch( NoSuchFieldException e ) {
                continue;
            }

            String directory = (String)directoryField.get( handler );
            if( prefixToMatch.equals( prefixField.get( handler ) ) ) {
                File logDirectory = new File(  directory );
                File[] logFiles = logDirectory.listFiles( new FileFilter() {
                    public boolean accept( File pathname ) {
                        return pathname.getName().startsWith( prefixToMatch );
                    }
                } );
                if( logFiles.length == 0 ) continue;
                Arrays.sort( logFiles );
                result = logFiles[ logFiles.length - 1 ];
                break;
            }
        }
    } catch( IllegalAccessException e ) {
        log.log( Level.WARNING, "Couldn't get log file", e );
    }
    return result;
}
like image 36
Jesse Barnum Avatar answered Nov 01 '22 16:11

Jesse Barnum