7
7
import java .io .IOException ;
8
8
import java .text .DateFormat ;
9
9
import java .util .Date ;
10
+ import java .util .logging .ConsoleHandler ;
10
11
import java .util .logging .FileHandler ;
11
12
import java .util .logging .Formatter ;
13
+ import java .util .logging .Handler ;
12
14
import java .util .logging .Level ;
13
15
import java .util .logging .LogRecord ;
14
16
import java .util .logging .Logger ;
22
24
*/
23
25
public class Debug {
24
26
private static Debug instance = null ;
25
- private static long startup = 0L ;
26
- private long timeLog = 0L ;
27
27
28
28
private Logger log ;
29
29
private String logPrefix ;
30
30
private TextFileHandler logFile = null ;
31
31
private boolean debug = false ;
32
+ private Level oldLogLevel = null ;
33
+
34
+ // timer
35
+ private static boolean isStartup = true ;
36
+ private static long startup = 0L ;
37
+ private long timeLog = 0L ;
32
38
33
39
/**
34
40
* 初期化
@@ -90,6 +96,42 @@ public void debug(Object... args){
90
96
}
91
97
}
92
98
99
+ /**
100
+ * コンソール出力するログレベルを変更
101
+ * @param level
102
+ */
103
+ private void setConsoleLevel (Level level ){
104
+ if (level == null ) return ;
105
+
106
+ Handler handler = getConsoleHandler (log );
107
+ if (handler != null ){
108
+ handler .setLevel (level );
109
+ }
110
+ }
111
+
112
+ /**
113
+ * コンソールハンドラを返す
114
+ * @param log
115
+ * @return
116
+ */
117
+ private Handler getConsoleHandler (Logger log ){
118
+ // コンソールハンドラを返す
119
+ Handler [] handlers = log .getHandlers ();
120
+ for (Handler h : handlers ){
121
+ if (h instanceof ConsoleHandler ){
122
+ return h ;
123
+ }
124
+ }
125
+
126
+ // 親ロガーのコンソールハンドラを返す
127
+ Logger parent = log .getParent ();
128
+ if (parent != null ){
129
+ return getConsoleHandler (parent );
130
+ }else {
131
+ return null ;
132
+ }
133
+ }
134
+
93
135
/* getter / setter */
94
136
95
137
/**
@@ -107,8 +149,16 @@ public void setDebug(boolean isDebug){
107
149
this .debug = isDebug ;
108
150
109
151
if (isDebug ){
152
+ oldLogLevel = log .getLevel ();
153
+ log .setLevel (Level .FINE );
154
+ setConsoleLevel (Level .FINE );
155
+
110
156
log .info (logPrefix + "DEBUG MODE ENABLED!" );
111
157
}else {
158
+ if (oldLogLevel != null ){
159
+ log .setLevel (oldLogLevel );
160
+ setConsoleLevel (oldLogLevel );
161
+ }
112
162
log .info (logPrefix + "DEBUG MODE DISABLED!" );
113
163
}
114
164
}
@@ -160,40 +210,35 @@ public static void setStartupBeginTime(){
160
210
if (startup <= 0L ){
161
211
startup = System .currentTimeMillis ();
162
212
}
213
+ isStartup = true ;
214
+ }
215
+ /**
216
+ * プラグイン起動完了時に経過時間を出力する
217
+ */
218
+ public void finishStartup (){
219
+ isStartup = false ;
220
+ debug ("[Timer] Total initialization time: " + (System .currentTimeMillis ()-startup ) + "ms" );
163
221
}
164
222
165
223
/**
166
224
* デバッグ時刻計測開始
167
225
* @param actionName
168
226
* @param useStartup
169
227
*/
170
- public void debugStartTimer (String actionName , boolean useStartup ) {
228
+ public void startTimer (String actionName ) {
171
229
timeLog = System .currentTimeMillis ();
172
230
173
- if (debug ){
174
- if (useStartup ){
175
- debug ("[Startup Timer] starting " + actionName + " (t+" + (System .currentTimeMillis ()-startup ) + ")" );
176
- }else {
177
- debug ("[Startup Timer] starting " + actionName );
178
- }
179
- }
231
+ if (isStartup ){
232
+ debug ("[Timer] starting " + actionName + " (t+" + (System .currentTimeMillis ()-startup ) + ")" );
233
+ }else {
234
+ debug ("[Timer] starting " + actionName );
235
+ }
180
236
}
181
-
182
- /**
183
- * デバッグ時刻計測開始
184
- * @param actionName
185
- */
186
- public void debugStartTimer (String actionName ){
187
- this .debugStartTimer (actionName , false );
188
- }
189
-
190
237
/**
191
238
* デバッグ時刻計測終了
192
239
* @param actionName
193
240
*/
194
- public void debugEndTimer (String actionName ){
195
- if (debug ){
196
- debug ("[Startup Timer] " + actionName + " finished in " + (System .currentTimeMillis ()-timeLog ) + "ms" );
197
- }
241
+ public void endTimer (String actionName ){
242
+ debug ("[Timer] " + actionName + " finished in " + (System .currentTimeMillis ()-timeLog ) + "ms" );
198
243
}
199
244
}
0 commit comments