Customize iOS Logging

iOS logging is handled by IOSLogHandler.java, which overrides java.util.logging.Handler. You can set it as the default by adding a logging.properties resource to your app, as shown in LogManager.

Changing the Logging Handler Programmatically

To programmatically add a logging handler, use the same code you would use to change it in Java:

LogManager.getLogger("").addHandler(myHandler);

If you don't want the existing handler(s) to also run, remove them first using:

Logger logger = LogManager.getLogger("");
for (Handler h : logger.getHandlers()) {
  logger.removeHandler(h);
}

Changing the Logging Handler with a Property File

  1. To change the default logging handler using a logging.properties file, you have to specify that handler like this (the same as with Java applications):

    handlers=mycompany.mylogger.MyIOSLogHandler
    java.util.logging.ConsoleHandler.level=ALL
    

    This file can have any name, as long as that name is used during loading.

  2. Next, add the logging.properties file as an iOS resource to your project.

  3. Unlike with Java applications, a J2ObjC application needs to explicitly load the property file:

    static {
      // Fetch a logger in case the following leaves logging in a bad state, such
      // as not adding the logging.properties resource or using a different name.
      Logger log = Logger.getLogger("configLogger");
      try {
        InputStream loggingProperties = SomeClass.class.getResourceAsStream("logging.properties");
        LogManager.getLogManager().readConfiguration(loggingProperties);
      } catch (IOException exception) {
        log.log(Level.SEVERE, "Error in loading configuration", exception);
      }
    }