刚刚开始掌握这个日志模块。
当我在 Eclipse 中运行它时,控制台输出将到达我期望的位置:控制台处理程序到控制台,文件处理程序到文件。
当我从命令提示符运行时,我不知道控制台输出或文件输出的去向。
这是我的配置方式:
Handler consoleHandler = null;
Handler fileHandler = null;
try {
logger.setUseParentHandlers(false);
// Creating consoleHandler and fileHandler
consoleHandler = new DualConsoleHandler();
// 1000 bytes max, 5 files max, append mode = true
fileHandler = new FileHandler("./output/logging/log%g.xml", 10000, 5, true);
// Assigning handlers to logger object
logger.addHandler(consoleHandler);
logger.addHandler(fileHandler);
// Setting levels to handlers and logger
consoleHandler.setLevel(Level.ALL);
fileHandler.setLevel(Level.ALL);
logger.setLevel(Level.ALL);
} catch (IOException exception) {
logger.log(Level.SEVERE, "Error occur in FileHandler.", exception);
}
DualConsoleHandler
看起来像这样(在另一个 SO 问题中找到),其想法是将 INFO 或更低的值打印到 stdout,但将 WARNING 或更高的值打印到 stderr:
public class DualConsoleHandler extends StreamHandler {
private final ConsoleHandler stderrHandler = new ConsoleHandler();
public DualConsoleHandler() {
super(System.out, new SimpleFormatter());
}
@Override
public void publish(LogRecord record) {
if (record.getLevel().intValue() <= Level.INFO.intValue()) {
super.publish(record);
super.flush();
} else {
stderrHandler.publish(record);
stderrHandler.flush();
}
}
}
最佳答案
When I run from the command prompt I have no idea where the console output or the file output is going.
控制台输出进入控制台。
文件输出将转到您指定的路径。即 ./output/logging/log%g.xml
在运行它的目录中查找 output/logging
目录。
关于java.util.logging:从 Windows 命令提示符运行时,处理程序输出去了哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36667210/