Lukas
Lukas

Reputation: 434

How to customize logging for Google App Engine Java?

Google App Engine Java uses java.util.logging to create logging messages. I want to modify the log messages, that are displayed in Developers Console - Monitoring - Logs. The idea is to add some additional output like username without putting it in each log message manually:

log.info("user action");

should result in an logging output like

user "testuser": user action

Therefore I created an own Formatter:

public class TestFormatter extends Formatter {
    @Override
    public String format(LogRecord record) {
        // find out username..
        return "user " + username + ": " + record.getMessage();
    }
}

Setting this as formatter for the ConsoleHandler in the logging.properties has not effekt: java.util.logging.ConsoleHandler.formatter = com.example.guestbook.TestFormatter

When deploying it in on the local machine, and trying to add it programmatically like this:

Logger rootLogger = Logger.getLogger("");
Handler[] handlers = rootLogger.getHandlers();
log.info("Handler[] size: " + handlers.length);
for(Handler h : handlers) {
    log.info(h.toString());
    h.setFormatter(new TestFormatter());
}

I get 2 handler, one ConsoleHandler and one DevLogHandler. But setting the formatter results in the fact that no further logs are displayed. On GAE instead I get 0 handler.

When trying to acces Logger.getGlobal() instead of Logger.getLogger(""), I get 0 Handler on the local instance and a SecurityException: No permission to modify global on GAE. This exception already arises when trying to get the list of Handlers.

Now my question: Is there a way to modify the logs of developer console in such a way? If yes, how?

Upvotes: 3

Views: 510

Answers (1)

Deviling Master
Deviling Master

Reputation: 3113

As I reply I got in the past from a Google ticket I opened for a similar question

I would discourage tampering with the Loggers/Handlers used internally by GAE.

Besides that, the Global Logger cannot be customized, you can try to it with a Logger with a custom name

Upvotes: 1

Related Questions