在我的项目中,我使用了一些不同的记录器,它们都附加了FileHandler和ConsoleHandler。我使用以下类创建这些记录器:
public class Logging {
static {
System.setProperty("java.util.logging.config.file", "properties/logging.properties");
try {
LogManager.getLogManager().readConfiguration();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Method to create a new logger using java.util.logging.
* @param name The name of the logger.
* @param logLevelFile The log level for writing into the log file.
* @param logLevelConsole The log level for writing on the console.
* @param logToFile True, if file logging shall be active.
* @param logToConsole True, if console logging shall be active.
* @return The new logger instance.
*/
public static Logger createLogger(String name, Level logLevelFile, Level logLevelConsole, boolean logToFile, boolean logToConsole) {
Logger logger = Logger.getLogger(name);
logger.setLevel(Level.ALL);
logger.setUseParentHandlers(false);
if (logToFile) {
final Handler fileHandler;
try {
fileHandler = new FileHandler("log/" + name + ".log");
fileHandler.setLevel(logLevelFile);
logger.addHandler(fileHandler);
// close file handler, so that *.lck files will be deleted by
// JVM
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
fileHandler.close();
}
}));
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (logToConsole) {
// remove existing console handlers
Handler[] handlers = logger.getHandlers();
for (Handler handler : handlers) {
if (handler.getClass() == ConsoleHandler.class)
logger.removeHandler(handler);
}
Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(logLevelConsole);
logger.addHandler(consoleHandler);
}
return logger;
}
}
正如您所看到的,每个记录器都会获得一个布尔值,无论它是否应该登录到文件并进入控制台。取决于可以设置,哪个级别应记录到控制台或文件中。
我的logging.properties看起来像这样:
handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format = [%1$td.%1$tm.%1$tY-%1$tH:%1$tM:%1$tS] [%2$s] %4$s: %5$s %n
# approximate amount of bytes to write to a log file, before rotating to a new file
java.util.logging.FileHandler.limit = 500000
# number of log files to use in the log file rotation
java.util.logging.FileHandler.count = 0
# whether or not the FileHandler should append to an existing file or not (true or false)
java.util.logging.FileHandler.append = false
我按如下方式初始化我的记录器:
Logging.createLogger(Properties.getGeneralLoggerName(), Level.INFO, Level.INFO, true, true);
Logging.createLogger(Properties.getBluetoothLoggerName(), Level.INFO, Level.INFO, true, true);
Logging.createLogger(Properties.getStrategicProcessesLoggerName(), Level.INFO, Level.WARNING, true, true);
Logging.createLogger(Properties.getNetworkLoggerName(), Level.INFO, Level.WARNING, true, true);
Logging.createLogger(Properties.getGPIOLoggerName(), Level.INFO, Level.WARNING, true, true);
Logging.createLogger(Properties.getFatigueDetectionLoggerName(), Level.INFO, Level.WARNING, true, true);
Logging.createLogger(Properties.getGPSACCLoggerName(), Level.INFO, Level.WARNING, true, true);
问题是,例如,最后一个记录器不会仅将警告记录到控制台中,还会记录信息日志。在我的Mac书上,这一切都运行正常,但是当我在我的嵌入式系统上使用这个代码(带有Ubuntu 14.04的Odroid U3)时它失败了,控制台输出很大。它们不是将输出记录到文件中,而是登录到控制台。
有人可以帮忙吗?
谢谢!
答案 0 :(得分:0)
在示例代码中,您要添加一个关闭钩子来关闭FileHandler以删除“lck”文件。您必须这样做的原因是因为您是not holding a strong reference to your created logger。您必须对记录器otherwise your added handlers will not be closed on shutdown by the LogManager保持强烈的敬意。
如果您允许logger get garbage collected,那么该记录器的下一个新实例将继承父记录器的级别设置,而不是“createLogger”方法中设置的级别。
不是将输出记录到文件中,而是将它们登录到控制台。
FileHandler可以抛出异常。修复异常的原因。 addShutdownHook方法也可以抛出未被代码捕获的异常。