对Google App Engine(GAE)使用logback访问 [英] Using logback-access with Google App Engine (GAE)

查看:135
本文介绍了对Google App Engine(GAE)使用logback访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个使用GAE作为移动应用后端的项目。我们希望在项目中实现非常好的日志记录。我花了很多时间阅读log4j,logback-classic,logback-access,java.util.logging(JUL)和slf4j。



我的结论是我想要使用logback-access,因为它在记录http相关的东西时具有一些很好的功能(例如记录错误发生时的完整请求数据等)。

从GAE只支持JUL的日志级别,而logback-access不支持slf4j,我认为我应该安装logback-access并确保它通过JUL将所有日志写入GAE。



这可能吗?有没有人做过这件事,并且可以指导我进行logback-access和JUL的配置文件?如果我不必添加自定义Appender(我正在考虑可以根据文档添加到配置中的ch.qos.logback.access.jetty.RequestLogImpl的连接),可以直接通过JUL进行logback-access通信?或者,Jetty的logback-access配置不适用于GAE?我得到了什么错误?



欢迎提供任何提示! >我们做到了这一点,并在logback中待了两年。简言之,您将最终将JUL和logback设置混合在一起,因为您的应用程序将使用logback,Google类将直接使用JUL(并且您无法将JUL重定向到GAE中的logback)。



两年后,我们切换到slf4j + JUL配置,这更简单,只需一个配置点。然而,这并不容易,但(警告:大量的代码遵循):

logging.properties:

  .level = INFO 
handlers = com.acme.log.InfoHandler,com.acme.log.ErrorHandler

#这些应该可以工作,吨。请参阅
#http://code.google.com/p/googleappengine/issues/detail?id=7467
com.acme.log.InfoHandler.level = ALL
com.acme。 log.ErrorHandler.level =警告

#为单个类设置日志级别示例
#变通办法http://code.google.com/p/google-guice/issues/detail ?id = 488
com.google.inject.internal.util.level =警告

InfoHandler.java:

  / ** 
*记录{@link Level#INFO}至{@link System#出来}。
*这个类的灵感来自{@link ConsoleHandler}
* /
public class InfoHandler extends StreamHandler {

public InfoHandler(){
setOutputStream的System.out);
setFilter(new MaxLevelFilter(Level.WARNING));
}

@Override
public void publish(LogRecord record){
super.publish(record);
flush();


@Override
public void close(){
flush();
}

}

ErrorHandler.java:

  public class ErrorHandler extends StreamHandler {

public ErrorHandler(){
setOutputStream(System.err) ;
}

@Override
public void publish(LogRecord record){
super.publish(record);
flush();


@Override
public void close(){
flush();
}

}

MaxLevel.java:

  public class MaxLevelFilter implements Filter {

private final Level maxLevel;

public MaxLevelFilter(级别){
this.maxLevel = level;
}

@Override
public boolean isLoggable(LogRecord record){
return maxLevel.intValue()> 。record.getLevel()的intValue();
}

}

您还应该应用解决方法此处在服务器启动时的某个应用程序侦听器中。


I am working on a project that uses GAE as the backend for a mobile app. We want to have really good logging implemented in the project. I have spent a lot of time reading about log4j, logback-classic, logback-access, java.util.logging (JUL) and slf4j.

My conclusion is that I want to use logback-access since it has some nice features when it comes to logging http related stuff (e.g. logging full-request-data upon errors etc.).

Since GAE only supports the log levels for JUL and logback-access does not support slf4j, I thought that I should just install logback-access and make sure it writes all logs through JUL to GAE.

Is this possible? Has anyone done this and can guide me when it comes to the configuration files for logback-access and JUL? Can logback-access communicate directly through JUL, whithout me having to add a custom Appender (I am thinking about the connection to ch.qos.logback.access.jetty.RequestLogImpl that can be added to the configuration according to the docs)? Or does the logback-access configuration for Jetty not apply for GAE? Have I gotten something wrong?

Any tips are welcome!

解决方案

We did that and stayed with logback for two years. The short story is that you will end up mixing JUL and logback settings, since your application will be using logback and Google classes will be using JUL directly (and you cannot redirect JUL into logback in GAE).

After two years we switched to a slf4j + JUL configuration, it's easier and a single point of configuration. It is not easy, though (warning: tons of code follow):

logging.properties:

.level = INFO
handlers = com.acme.log.InfoHandler,com.acme.log.ErrorHandler

# these should work, but they don't. See 
# http://code.google.com/p/googleappengine/issues/detail?id=7467
com.acme.log.InfoHandler.level=ALL
com.acme.log.ErrorHandler.level=WARNING

# Example of log level setup for a single class
# workaround http://code.google.com/p/google-guice/issues/detail?id=488
com.google.inject.internal.util.level = WARNING

InfoHandler.java:

/**
 * Logs {@link Level#INFO} to {@link System#out}.
 * This class is inspired by {@link ConsoleHandler}
 */
public class InfoHandler extends StreamHandler {

    public InfoHandler() {
        setOutputStream(System.out);
        setFilter(new MaxLevelFilter(Level.WARNING));
    }

    @Override
    public void publish(LogRecord record) {
        super.publish(record);  
        flush();
    }

    @Override
    public void close() {
        flush();
    }

}

ErrorHandler.java:

public class ErrorHandler extends StreamHandler {

    public ErrorHandler() {
        setOutputStream(System.err);
    }

    @Override
    public void publish(LogRecord record) {
        super.publish(record);  
        flush();
    }

    @Override
    public void close() {
        flush();
    }

}

MaxLevel.java:

public class MaxLevelFilter implements Filter {

    private final Level maxLevel;

    public MaxLevelFilter(Level level) {
        this.maxLevel = level;
    }

    @Override
    public boolean isLoggable(LogRecord record) {
        return maxLevel.intValue() > record.getLevel().intValue();
    }

}

You should also apply the workaround explained here at some application listener when the server starts.

这篇关于对Google App Engine(GAE)使用logback访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆