5

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

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
aimless
  • 605
  • 6
  • 13
  • Related: [Issue 11499: Allow custom loggers for Java AppEngine](https://code.google.com/p/googleappengine/issues/detail?id=11499) – jmehrens Sep 06 '16 at 16:57

3 Answers3

3

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.

Brad
  • 5,428
  • 1
  • 33
  • 56
  • This worked for me, although it ended up creating duplicate logging statements because I merely called LOG.addHandler(myCustomHandler). To fix that, you could remove the existing handlers returned from LOG.getHandlers(), and then add your custom handler. – sappenin Oct 16 '12 at 02:07
  • 1
    +1 Is the work-around still necessary as of GAE 1.9.1 and do we know the technical reason why the setting in logging.properties is ignored? – Drux Apr 30 '14 at 10:26
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?

Le Hibou
  • 1,541
  • 1
  • 10
  • 12
1
    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;
            }
        }
    }
}
aimless
  • 605
  • 6
  • 13