Can anyone explain to me why, in the presence of a static initialization block, does the logger duplicate a message in the console?
Java Code:
public class LoggerProxy { private final static Logger log = Logger.getLogger(Example.class.getName()); /*static */{ // <- ΠΏΡΠΈ Π½Π°Π»ΠΈΡΠΈΠΈ static, ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Π΄ΡΠ±Π»ΠΈΡΡΠ΅ΡΡΡ log.setLevel(Level.ALL); log.addHandler(new ConsoleHandler()); } public static void info(String msg){ log.info(messageFormat(new Exception(msg))); } public static void info(Exception ex){ log.info(messageFormat(ex)); } private static String messageFormat(Exception ex){ StackTraceElement[] stackTraceElement = ex.getStackTrace(); int index = 1; return String.format( "[%s/%s.%s()]: %s", stackTraceElement[index].hashCode(), stackTraceElement[index].getClassName(), stackTraceElement[index].getMethodName(), ex.getMessage() // OR -> ex.getLocalizedMessage() ); } public static void config(String msg){...} public static void config(Exception ex){...} public static void fine(String msg){...} public static void fine(Exception ex){...} public static void finer(String msg){...} public static void finer(Exception ex){...} public static void finest(String msg){...} public static void finest(Exception ex){...} public static void severe(String msg){...} public static void severe(Exception ex){...} public static void warning(String msg){...} public static void warning(Exception ex){...} public static void addHandler(Handler handler){...} public static void removeHandler(Handler handler){...} } Console:
// ΠΏΡΠΈ Π½Π°Π»ΠΈΡΠΈΠΈ static Π² Π±Π»ΠΎΠΊΠ΅ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ ΠΌΠ°Ρ 02, 2018 7:17:25 ΠΠ LoggerProxy info INFO: ... ΠΌΠ°Ρ 02, 2018 7:17:25 ΠΠ LoggerProxy info INFO: ... // ΠΏΡΠΈ ΠΎΡΡΡΡΡΡΠ²ΠΈΠΈ static Π² Π±Π»ΠΎΠΊΠ΅ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ ΠΌΠ°Ρ 02, 2018 7:17:25 ΠΠ LoggerProxy info INFO: ...
log.addHandler(new ConsoleHandler());out the linelog.addHandler(new ConsoleHandler());- not a Programmer