1 | |
package com.aragost.javahg.log; |
2 | |
|
3 | |
import java.util.logging.Level; |
4 | |
|
5 | |
import com.google.common.annotations.VisibleForTesting; |
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
public class JULLogger implements Logger { |
23 | |
|
24 | |
private final java.util.logging.Logger julLogger; |
25 | |
|
26 | 8 | public JULLogger(String name) { |
27 | 8 | this.julLogger = java.util.logging.Logger.getLogger(name); |
28 | 8 | } |
29 | |
|
30 | |
public void debug(String msg) { |
31 | 0 | this.julLogger.fine(msg); |
32 | 0 | } |
33 | |
|
34 | |
public void debug(String msg, Object... args) { |
35 | 3789 | if (isDebugEnabled()) { |
36 | 0 | this.julLogger.fine(format(msg, args)); |
37 | |
} |
38 | 3789 | } |
39 | |
|
40 | |
public void debug(String msg, Throwable thrown) |
41 | |
{ |
42 | 0 | logException(Level.FINE, msg, thrown); |
43 | 0 | } |
44 | |
|
45 | |
public void info(String msg) { |
46 | 26 | this.julLogger.info(msg); |
47 | 26 | } |
48 | |
|
49 | |
public void info(String msg, Object... args) { |
50 | 401 | if (isInfoEnabled()) { |
51 | 0 | this.julLogger.info(format(msg, args)); |
52 | |
} |
53 | 401 | } |
54 | |
|
55 | |
public void info(String msg, Throwable thrown) |
56 | |
{ |
57 | 0 | logException(Level.INFO, msg, thrown); |
58 | 0 | } |
59 | |
|
60 | |
public void warn(String msg) |
61 | |
{ |
62 | 29 | this.julLogger.warning(msg); |
63 | 29 | } |
64 | |
|
65 | |
public void warn(String msg, Object... args) { |
66 | 12 | this.julLogger.warning(format(msg, args)); |
67 | 12 | } |
68 | |
|
69 | |
public void warn(String msg, Throwable thrown) |
70 | |
{ |
71 | 0 | logException(Level.WARNING, msg, thrown); |
72 | 0 | } |
73 | |
|
74 | |
public void error(String msg) |
75 | |
{ |
76 | 0 | this.julLogger.severe(msg); |
77 | 0 | } |
78 | |
|
79 | |
public void error(String msg, Object... args) { |
80 | 19 | this.julLogger.severe(format(msg, args)); |
81 | 19 | } |
82 | |
|
83 | |
public void error(String msg, Throwable thrown) { |
84 | 0 | logException(Level.SEVERE, msg, thrown); |
85 | 0 | } |
86 | |
|
87 | |
public boolean isDebugEnabled() { |
88 | 10299 | return this.julLogger.isLoggable(Level.FINE); |
89 | |
} |
90 | |
|
91 | |
public boolean isInfoEnabled() { |
92 | 7997 | return this.julLogger.isLoggable(Level.INFO); |
93 | |
} |
94 | |
|
95 | |
public boolean isWarnEnabled() |
96 | |
{ |
97 | 0 | return this.julLogger.isLoggable(Level.WARNING); |
98 | |
} |
99 | |
|
100 | |
public boolean isErrorEnabled() |
101 | |
{ |
102 | 0 | return this.julLogger.isLoggable(Level.SEVERE); |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
@VisibleForTesting |
119 | |
static String format(String format, Object[] args) { |
120 | 35 | int length = format.length(); |
121 | 35 | StringBuilder tgt = new StringBuilder(length); |
122 | 35 | int prevPos = 0; |
123 | 35 | int argIndex = 0; |
124 | 74 | while (prevPos < length) { |
125 | 39 | int pos = format.indexOf("{}", prevPos); |
126 | 39 | if (pos >= 0) { |
127 | 37 | Object arg = args[argIndex++]; |
128 | 37 | tgt.append(format.substring(prevPos, pos)); |
129 | 37 | tgt.append(arg == null ? "" : arg.toString()); |
130 | 37 | prevPos = pos + 2; |
131 | 37 | } else { |
132 | 2 | tgt.append(format.substring(prevPos)); |
133 | 2 | prevPos = length; |
134 | |
} |
135 | 39 | } |
136 | 35 | return tgt.toString(); |
137 | |
} |
138 | |
|
139 | |
private void logException(Level level, String msg, Throwable thrown) { |
140 | 0 | this.julLogger.log(level, msg, thrown); |
141 | 0 | } |
142 | |
} |