Tuỳ chỉnh tính năng ghi nhật ký trên iOS
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Việc ghi nhật ký iOS do IOSLogHandler.java xử lý,
ghi đè java.util.logging.Handler
. Bạn có thể đặt chế độ này làm mặc định bằng cách thêm tài nguyên logging.properties
vào
ứng dụng của bạn, như thể hiện trong LogManager.
Thay đổi trình xử lý ghi nhật ký theo phương thức lập trình
Để thêm trình xử lý ghi nhật ký theo cách lập trình, hãy dùng chính mã mà bạn sẽ dùng để thay đổi trình xử lý đó trong Java:
LogManager.getLogger("").addHandler(myHandler);
Nếu bạn không muốn(các) trình xử lý hiện có cũng chạy, trước tiên hãy xoá chúng bằng cách sử dụng:
Logger logger = LogManager.getLogger("");
for (Handler h : logger.getHandlers()) {
logger.removeHandler(h);
}
Thay đổi Trình xử lý nhật ký bằng một tệp thuộc tính
Để thay đổi trình xử lý ghi nhật ký mặc định bằng tệp register.properties, bạn phải chỉ định trình xử lý đó
như thế này (tương tự như với các ứng dụng Java):
handlers=mycompany.mylogger.MyIOSLogHandler
java.util.logging.ConsoleHandler.level=ALL
Tệp này có thể có bất kỳ tên nào, miễn là tên đó được sử dụng trong quá trình tải.
Tiếp theo, hãy thêm tệp log.properties dưới dạng tài nguyên iOS vào dự án của bạn.
Không giống như các ứng dụng Java, ứng dụng J2ObjC cần tải tệp thuộc tính một cách rõ ràng:
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);
}
}
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2024-08-28 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2024-08-28 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"]]