自定义 iOS 日志记录
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
iOS 日志记录由 IOSLogHandler.java 处理,
这会替换 java.util.logging.Handler
。您可以将 logging.properties
资源添加到
如 LogManager 中所示。
以编程方式更改日志记录处理程序
如需以编程方式添加日志记录处理程序,请使用您在 Java 中更改它时所用的相同代码:
LogManager.getLogger("").addHandler(myHandler);
如果您不希望同时运行现有处理程序,请先使用以下命令将其移除:
Logger logger = LogManager.getLogger("");
for (Handler h : logger.getHandlers()) {
logger.removeHandler(h);
}
使用属性文件更改日志记录处理程序
要使用 logging.properties 文件更改默认日志记录处理程序,必须指定该处理程序。
如下所示(与 Java 应用相同):
handlers=mycompany.mylogger.MyIOSLogHandler
java.util.logging.ConsoleHandler.level=ALL
此文件可以使用任何名称,只要在加载过程中使用该名称即可。
接下来,将 logging.properties 文件作为 iOS 资源添加到项目中。
与 Java 应用程序不同,J2ObjC 应用程序需要显式加载属性文件:
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);
}
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-25。"],[[["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"]]