java.util.logging.SimpileFormatter系统属性配置
我最近在学习Java日志记录,在javadoc中,它说可以使用属性“Java.util.logging.SimpleFormatter.format”配置SimpleFormatter 在下面的代码中,我尝试使用System.setProperty()两次“设置格式”,但在第二次尝试中似乎不起作用,下面代码中的“formatterB”仍将使用“formatterA”中定义的格式 这是什么原因,谢谢java.util.logging.SimpileFormatter系统属性配置,java,logging,formatter,Java,Logging,Formatter,我最近在学习Java日志记录,在javadoc中,它说可以使用属性“Java.util.logging.SimpleFormatter.format”配置SimpleFormatter 在下面的代码中,我尝试使用System.setProperty()两次“设置格式”,但在第二次尝试中似乎不起作用,下面代码中的“formatterB”仍将使用“formatterA”中定义的格式 这是什么原因,谢谢 public class Test { public static void main(S
public class Test {
public static void main(String[] args) {
try {
Logger loggerA = Logger.getLogger("A");
System.setProperty("java.util.logging.SimpleFormatter.format", "A: %1$tc %2$s%n%4$s: %5$s%6$s%n"); // first attempt
Handler handlerA = new FileHandler("A.log", 0, 1, true);
SimpleFormatter formatterA = new SimpleFormatter();
handlerA.setFormatter(formatterA);
loggerA.addHandler(handlerA);
loggerA.info("Logger A info message");
Logger loggerB = Logger.getLogger("B");
System.setProperty("java.util.logging.SimpleFormatter.format", "B: %1$tc %2$s%n%4$s: %5$s%6$s%n"); // second attempt
Handler handlerB = new FileHandler("B.log", 0, 1, true);
SimpleFormatter formatterB = new SimpleFormatter();
handlerB.setFormatter(formatterB);
loggerB.addHandler(handlerB);
loggerB.info("Logger B info message");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
在类加载时存储一次的SimpleFormat。该格式用于SimpleFormat类的所有实例
您必须滚动自己的格式化程序类以支持多种格式 澄清了困惑,真的很有帮助:)