How to add custom log handler to Google App Engine?

こ雲淡風輕ζ 提交于 2019-12-10 14:48:36

问题


I am trying to add a custom log handler to my java application. I have implemented a InnerLogger class that extends java.util.Logging.Handler class. And in my logging.properties declared as a handler:

handlers:com.mycompany.util.InnerLogger

But when I launch the development sever, I got the following error:

Can't load log handler "com.mycompany.util.InnerLogger"
java.lang.ClassNotFoundException: com.mycompany.util.InnerLogger

I can add my custom handler to loggers one by one ,but I just wondering is there a way to add it to all loggers.

Thanks


回答1:


I was able to add a Handler to the root Logger when my application initializes. You can put this code in a warmup task or servlet filter.

private static Logger LOG;

...

LOG = Logger.getLogger("");
LOG.addHandler(myCustomHandler);

Its not as graceful as using logging.properties, but it is an adequate workaround when running on GAE.




回答2:


It might not the solution you were looking for, but have you considered using Simple Logging Facade for Java (SLF4J) in your application instead of using directly JUL?




回答3:


    public static void addStreamHandler(){

    // TODO check if appCode has X length
    CustomHandler handler = new CustomHandler();
    LogManager manager = LogManager.getLogManager();
    Enumeration<String> e = manager.getLoggerNames();
    while (e.hasMoreElements()){

        Logger logger = manager.getLogger(e.nextElement());
        if ( logger.getName().startsWith("com.google"))continue;
        logger.addHandler(handler);
        System.out.println(logger.getName());
    }
    System.out.println("***finished*****");
}

public static void removeStreamHandlers(){

    LogManager manager = LogManager.getLogManager();
    Enumeration<String> e = manager.getLoggerNames();
    while (e.hasMoreElements()){

        Logger logger = manager.getLogger(e.nextElement());
        Handler[] handlers = logger.getHandlers();
        for ( int i = 0; i < handlers.length; i++){
            Handler h = handlers[i];
            if (h instanceof CustomHandler){
                logger.removeHandler(h);
                break;
            }
        }
    }
}


来源:https://stackoverflow.com/questions/9841172/how-to-add-custom-log-handler-to-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!