gpt4 book ai didi

java - 跨多个类使用 java.util.logging

转载 作者:行者123 更新时间:2023-11-30 02:22:23 24 4
gpt4 key购买 nike

我在网上找到了这个使用java.util.logging的例子here 。我不明白如何修改它以跨多个类使用示例
我是否只需在每个类(class)中声明这一点?

Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);  

也在类里面,他们提供了一个语法错误:

// suppress the logging output to the console
Logger rootLogger = Logger.*getLogger*("");

访问根记录器的正确方法是什么?

package com.vogella.logger;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MyLogger {
static private FileHandler fileTxt;
static private SimpleFormatter formatterTxt;

static private FileHandler fileHTML;
static private Formatter formatterHTML;

static public void setup() throws IOException {

// get the global logger to configure it
Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

// suppress the logging output to the console
Logger rootLogger = Logger.*getLogger*("");
Handler[] handlers = rootLogger.getHandlers();
if (handlers[0] instanceof ConsoleHandler) {
rootLogger.removeHandler(handlers[0]);
}

logger.setLevel(Level.INFO);
fileTxt = new FileHandler("Logging.txt");
fileHTML = new FileHandler("Logging.html");

// create a TXT formatter
formatterTxt = new SimpleFormatter();
fileTxt.setFormatter(formatterTxt);
logger.addHandler(fileTxt);

// create an HTML formatter
formatterHTML = new MyHtmlFormatter();
fileHTML.setFormatter(formatterHTML);
logger.addHandler(fileHTML);
}
}

最佳答案

。使用GLOBAL_LOGGER_NAME如链接的 Javadoc 中所述,还可以使用每个类的全局记录器进行日志记录。虽然用处不大,因为它是提供的默认 java 记录器。

GLOBAL_LOGGER_NAME is a name for the global logger. This name is provided as a convenience to developers who are making casual use of the Logging package....

The preferred way to get the global logger object is via the call Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).

它也是 Logger.getGlobal() 的替代品来自较旧的 jdk 版本。

<小时/>

要配置根记录器,请使用:-

Logger logger = Logger.getLogger("");

关于java - 跨多个类使用 java.util.logging,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46430342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com