@@ -4,72 +4,176 @@ public final class ExtractorLogger {
44
55 private ExtractorLogger () { }
66
7- private static Logger logger = new EmptyLogger ();
7+ private static final Logger EMPTY_LOGGER = new EmptyLogger ();
8+ private static volatile Logger logger = EMPTY_LOGGER ;
89
910 public static void setLogger (final Logger customLogger ) {
10- logger = customLogger ;
11+ logger = customLogger != null ? customLogger : EMPTY_LOGGER ;
1112 }
1213
14+ public enum Level { DEBUG , WARN , ERROR }
15+
16+ @ SuppressWarnings ("checkstyle:NeedBraces" )
17+ private static void log (final Level level ,
18+ final String tag ,
19+ final String message ,
20+ final Throwable t ) {
21+ if (logger == EMPTY_LOGGER ) return ;
22+ switch (level ) {
23+ case DEBUG :
24+ if (t == null ) {
25+ logger .debug (tag , message );
26+ } else {
27+ logger .debug (tag , message , t );
28+ }
29+ break ;
30+ case WARN :
31+ if (t == null ) {
32+ logger .warn (tag , message );
33+ } else {
34+ logger .warn (tag , message , t );
35+ }
36+ break ;
37+ case ERROR :
38+ if (t == null ) {
39+ logger .error (tag , message );
40+ } else {
41+ logger .error (tag , message , t );
42+ }
43+ break ;
44+ }
45+ }
46+
47+ @ SuppressWarnings ("checkstyle:NeedBraces" )
48+ private static void logFormat (final Level level ,
49+ final String tag ,
50+ final Throwable t ,
51+ final String template ,
52+ final Object ... args ) {
53+ if (logger == EMPTY_LOGGER ) return ;
54+ log (level , tag , format (template , args ), t );
55+ }
56+
57+ // DEBUG
1358 public static void d (final String tag , final String msg ) {
14- logger . debug ( tag , msg );
59+ log ( Level . DEBUG , tag , msg , null );
1560 }
1661
1762 public static void d (final String tag , final String msg , final Throwable t ) {
18- logger .debug (tag , msg , t );
63+ log (Level .DEBUG , tag , msg , t );
64+ }
65+
66+ public static void d (final String tag , final String template , final Object ... args ) {
67+ logFormat (Level .DEBUG , tag , null , template , args );
1968 }
2069
70+ public static void d (final String tag ,
71+ final Throwable t ,
72+ final String template ,
73+ final Object ... args ) {
74+ logFormat (Level .DEBUG , tag , t , template , args );
75+ }
76+
77+ // WARN
2178 public static void w (final String tag , final String msg ) {
22- logger . warn ( tag , msg );
79+ log ( Level . WARN , tag , msg , null );
2380 }
2481
2582 public static void w (final String tag , final String msg , final Throwable t ) {
26- logger .warn (tag , msg , t );
83+ log (Level .WARN , tag , msg , t );
84+ }
85+
86+ public static void w (final String tag , final String template , final Object ... args ) {
87+ logFormat (Level .WARN , tag , null , template , args );
2788 }
2889
90+ public static void w (final String tag ,
91+ final Throwable t ,
92+ final String template ,
93+ final Object ... args ) {
94+ logFormat (Level .WARN , tag , t , template , args );
95+ }
96+
97+ // ERROR
2998 public static void e (final String tag , final String msg ) {
30- logger . error ( tag , msg );
99+ log ( Level . ERROR , tag , msg , null );
31100 }
32101
33102 public static void e (final String tag , final String msg , final Throwable t ) {
34- logger .error (tag , msg , t );
103+ log (Level .ERROR , tag , msg , t );
104+ }
105+
106+ public static void e (final String tag , final String template , final Object ... args ) {
107+ logFormat (Level .ERROR , tag , null , template , args );
35108 }
36109
110+ public static void e (final String tag ,
111+ final Throwable t ,
112+ final String template ,
113+ final Object ... args ) {
114+ logFormat (Level .ERROR , tag , t , template , args );
115+ }
116+
117+ /**
118+ * Simple string format method for easier logger in the form of
119+ * {@code ExtractorLogger.d("Hello my name {Name} {}", name, surname)}
120+ * @param template The template string to format
121+ * @param args Arguments to replace identifiers with in {@code template}
122+ * @return Formatted string with arguments replaced
123+ */
124+ private static String format (final String template , final Object ... args ) {
125+ if (template == null || args == null || args .length == 0 ) {
126+ return template ;
127+ }
128+ final var out = new StringBuilder (template .length () + Math .min (32 , 16 * args .length ));
129+ int cursorIndex = 0 ;
130+ int argIndex = 0 ;
131+ final int n = template .length ();
132+ while (cursorIndex < n ) {
133+ // Find first/next open brace
134+ final int openBraceIndex = template .indexOf ('{' , cursorIndex );
135+ if (openBraceIndex < 0 ) {
136+ // If none found then there's no more arguments to replace
137+ out .append (template , cursorIndex , n ); break ;
138+ }
139+
140+ // Find matching closing brace
141+ final int close = template .indexOf ('}' , openBraceIndex + 1 );
142+ if (close < 0 ) {
143+ // If none found then there's no more arguments to replace
144+ out .append (template , cursorIndex , n ); break ;
145+ }
146+ out .append (template , cursorIndex , openBraceIndex );
147+ out .append (argIndex < args .length
148+ ? String .valueOf (args [argIndex ++])
149+ : template .substring (openBraceIndex , close + 1 ));
150+ cursorIndex = close + 1 ;
151+ }
152+ return out .toString ();
153+ }
37154
38155 private static final class EmptyLogger implements Logger {
39156 public void debug (final String tag , final String msg ) { }
40-
41- @ Override
42157 public void debug (final String tag , final String msg , final Throwable throwable ) { }
43-
44158 public void warn (final String tag , final String msg ) { }
45-
46- @ Override
47159 public void warn (final String tag , final String msg , final Throwable t ) { }
48-
49160 public void error (final String tag , final String msg ) { }
50-
51161 public void error (final String tag , final String msg , final Throwable t ) { }
52162 }
53163
54- /**
55- * Logger that prints to stdout
56- */
57164 public static final class ConsoleLogger implements Logger {
58165 public void debug (final String tag , final String msg ) {
59- System .out .println ("[DEBUG][" + tag + "] " + msg );
166+ System .out .println ("[DEBUG][" + tag + "] " + msg );
60167 }
61168
62- @ Override
63169 public void debug (final String tag , final String msg , final Throwable throwable ) {
64170 debug (tag , msg );
65171 throwable .printStackTrace (System .err );
66172 }
67-
68173 public void warn (final String tag , final String msg ) {
69174 System .out .println ("[WARN ][" + tag + "] " + msg );
70175 }
71176
72- @ Override
73177 public void warn (final String tag , final String msg , final Throwable t ) {
74178 warn (tag , msg );
75179 t .printStackTrace (System .err );
0 commit comments