Customize iOS Logging
Stay organized with collections
Save and categorize content based on your preferences.
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
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.
Next, add the logging.properties file as an iOS resource to your project.
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);
}
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["iOS logging in J2ObjC applications uses a custom `IOSLogHandler` that overrides the standard Java logging handler."],["You can modify the logging behavior either programmatically by adding or removing handlers or by using a `logging.properties` file."],["To use a `logging.properties` file, you need to add it as an iOS resource and explicitly load it within your application code."],["When loading a custom `logging.properties` file, make sure to handle potential errors, such as missing files or incorrect configurations."]]],["iOS logging uses `IOSLogHandler.java`, which can be set as the default via a `logging.properties` resource. Programmatically, handlers are added using `LogManager.getLogger(\"\").addHandler(myHandler)`. Existing handlers can be removed with `logger.removeHandler(h)`. With a property file, specify the handler (e.g., `handlers=mycompany.mylogger.MyIOSLogHandler`) and add it as an iOS resource. The property file must be explicitly loaded in a J2ObjC app using `LogManager.getLogManager().readConfiguration(loggingProperties)`.\n"]]