在 webapp 中以编程方式获取 tomcat 日志文件

发布于 2024-11-06 02:27:28 字数 297 浏览 0 评论 0原文

我正在寻找一种在 Web 应用程序中检索 Tomcat 日志的方法。过去我曾在其他 Web 应用程序中看到过此功能,通常将日志转储到 Servlet 中。

我正在使用 slf4j(带有 log4j)和 Tomcat 6。我在 Tomcat 文档中没有找到任何相关内容,尽管 JMX API 看起来可能提供一些有用的东西?我不太关心输出是否仅代表 Web 应用程序日志记录或整个 Tomcat 日志,两者都足够。

理想情况下,我希望有一个不涉及从文件系统中抓取日志的解决方案,尽管如果这是唯一的方法,如果可以在运行时计算日志目录那就太好了......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-11-13 02:27:28

从文件系统中抓取日志可能是最简单的方法。您可以使用 System.getProperty("catalina.base") + "/logs" 以编程方式直接获取日志。

否则,您可以在 log4j 配置中设置一个额外的附加程序来记录到 JDBC、JMS、Writer 等。无论对您的应用程序有意义什么。

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.

不即不离 2024-11-13 02:27:28

此函数将获取与给定前缀匹配的最新日志文件。您不需要知道日志写入哪个目录。

    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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文