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 public JULLogger(String name) {
27 this.julLogger = java.util.logging.Logger.getLogger(name);
28 }
29
30 public void debug(String msg) {
31 this.julLogger.fine(msg);
32 }
33
34 public void debug(String msg, Object... args) {
35 if (isDebugEnabled()) {
36 this.julLogger.fine(format(msg, args));
37 }
38 }
39
40 public void debug(String msg, Throwable thrown)
41 {
42 logException(Level.FINE, msg, thrown);
43 }
44
45 public void info(String msg) {
46 this.julLogger.info(msg);
47 }
48
49 public void info(String msg, Object... args) {
50 if (isInfoEnabled()) {
51 this.julLogger.info(format(msg, args));
52 }
53 }
54
55 public void info(String msg, Throwable thrown)
56 {
57 logException(Level.INFO, msg, thrown);
58 }
59
60 public void warn(String msg)
61 {
62 this.julLogger.warning(msg);
63 }
64
65 public void warn(String msg, Object... args) {
66 this.julLogger.warning(format(msg, args));
67 }
68
69 public void warn(String msg, Throwable thrown)
70 {
71 logException(Level.WARNING, msg, thrown);
72 }
73
74 public void error(String msg)
75 {
76 this.julLogger.severe(msg);
77 }
78
79 public void error(String msg, Object... args) {
80 this.julLogger.severe(format(msg, args));
81 }
82
83 public void error(String msg, Throwable thrown) {
84 logException(Level.SEVERE, msg, thrown);
85 }
86
87 public boolean isDebugEnabled() {
88 return this.julLogger.isLoggable(Level.FINE);
89 }
90
91 public boolean isInfoEnabled() {
92 return this.julLogger.isLoggable(Level.INFO);
93 }
94
95 public boolean isWarnEnabled()
96 {
97 return this.julLogger.isLoggable(Level.WARNING);
98 }
99
100 public boolean isErrorEnabled()
101 {
102 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 int length = format.length();
121 StringBuilder tgt = new StringBuilder(length);
122 int prevPos = 0;
123 int argIndex = 0;
124 while (prevPos < length) {
125 int pos = format.indexOf("{}", prevPos);
126 if (pos >= 0) {
127 Object arg = args[argIndex++];
128 tgt.append(format.substring(prevPos, pos));
129 tgt.append(arg == null ? "" : arg.toString());
130 prevPos = pos + 2;
131 } else {
132 tgt.append(format.substring(prevPos));
133 prevPos = length;
134 }
135 }
136 return tgt.toString();
137 }
138
139 private void logException(Level level, String msg, Throwable thrown) {
140 this.julLogger.log(level, msg, thrown);
141 }
142 }