15
15
import java .util .logging .LogRecord ;
16
16
import java .util .logging .MemoryHandler ;
17
17
18
+ import oracle .weblogic .deploy .util .WLSDeployContext ;
19
+ import oracle .weblogic .deploy .util .WLSDeployExit ;
20
+
18
21
19
22
/**
20
23
* This class save the log records logged by the tool at Info level or greater. The WLSDeployExit exit method will
24
27
* log manager logger properties and adds the handler to the root WLSDEPLOY Logger. See the class for information
25
28
* on how to inject this handler into the wlsdeploy root logger.
26
29
*
27
- * Before the tool exit, if specified by the caller, a recap of the saved logs is displayed to the console.
30
+ * Before the tool exit, if specified by the caller, an activity summary of the saved logs is displayed to the console.
28
31
* A final total of the records logged by the tool for the Level categories indicated above is displayed to the console.
29
32
*
30
33
* @see WLSDeployCustomizeLoggingConfig
31
34
* @see oracle.weblogic.deploy.util.WLSDeployExit
32
35
*/
33
- public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHandler {
36
+ public class SummaryHandler extends Handler implements WLSDeployLogEndHandler {
34
37
private static final String CLASS = SummaryHandler .class .getName ();
35
38
private static final String LEVEL_PROPERTY = "level" ;
36
39
private static final String TARGET_PROPERTY = "target" ;
37
40
private static final String FORMATTER_PROPERTY = "formatter" ;
38
41
private static final String SIZE_PROPERTY = "size" ;
39
42
private static final int DEFAULT_SIZE = 3000 ;
40
- private static final String LINE_SEPARATION = System .lineSeparator ();
41
43
42
44
private PlatformLogger LOGGER = WLSDeployLogFactory .getLogger ("wlsdeploy.exit" );
43
- private boolean online = false ;
44
- private String toolName ;
45
45
private int bufferSize ;
46
+ private WLSDeployContext context ;
46
47
47
- private Handler target ;
48
+ private Handler topTarget ;
48
49
private List <LevelHandler > handlers = new ArrayList <>();
49
50
private boolean closed = false ;
50
51
@@ -54,10 +55,8 @@ public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHand
54
55
public SummaryHandler () {
55
56
super ();
56
57
configure ();
57
- target = getConsoleHandler ();
58
- target .setFormatter (new TotalFormatter ());
58
+
59
59
LOGGER .setLevel (Level .INFO );
60
- addLevelHandler (Level .INFO );
61
60
addLevelHandler (Level .WARNING );
62
61
addLevelHandler (Level .SEVERE );
63
62
}
@@ -79,52 +78,35 @@ public synchronized void publish(LogRecord record) {
79
78
}
80
79
}
81
80
82
- /**
83
- * The Summary Handler will publish the recaps and total. The log records are discarded and the total reset.
84
- */
85
- @ Override
86
- public synchronized void push () {
87
- String METHOD = "push" ;
88
- LOGGER .entering (CLASS , METHOD );
89
- closed = true ;
90
- setPushLevel (getLevel ());
91
- StringBuffer buffer = new StringBuffer ();
92
- System .out .println (LINE_SEPARATION );
93
- target .publish (getLogRecord ("WLSDPLY-21003" , toolName ));
94
- java .util .Formatter fmt = new java .util .Formatter (buffer );
95
- for (LevelHandler handler : handlers ) {
96
- int count = handler .pushSection ();
97
- super .push ();
98
- if (count >= 0 ) {
99
- fmt .format (" %1$s : %2$,5d" , handler .getLevel ().getName (), count );
100
- }
101
- }
102
-
103
- System .out .println (LINE_SEPARATION );
104
- target .publish (getLogRecord ("WLSDPLY-21002" , buffer ));
105
- }
106
-
107
81
@ Override
108
82
public void flush () {
109
- super .flush ();
83
+ topTarget .flush ();
110
84
}
111
85
112
86
@ Override
113
87
public void close () throws SecurityException {
114
- super .close ();
88
+ topTarget .close ();
115
89
}
116
90
117
91
/**
118
92
* This method is called by the tool to complete the SummaryHandler, and display the recap and total information
119
93
* to the console. The log records are only displayed to the console if the tool was run in online mode.
120
94
* This compensates for wlst writing spurious blank lines to the console during online mode.
121
95
*
122
- * @param onlineMode if true, a recap of the log records will be displayed
96
+ * @param modelContext contextual information about the tool
123
97
*/
124
98
@ Override
125
- public void logEnd (String toolName , boolean onlineMode ) {
126
- this .toolName = toolName ;
127
- push ();
99
+ public synchronized void logEnd (WLSDeployContext modelContext ) {
100
+ closed = true ;
101
+ String METHOD = "push" ;
102
+ LOGGER .entering (modelContext , CLASS , METHOD );
103
+ this .context = modelContext ;
104
+ summaryHead (topTarget );
105
+ for (LevelHandler handler : handlers ) {
106
+ handler .push ();
107
+ }
108
+ summaryTail (topTarget );
109
+ LOGGER .exiting (CLASS , METHOD );
128
110
}
129
111
130
112
/**
@@ -144,29 +126,48 @@ public static Properties getHandlerProperties() {
144
126
private void addLevelHandler (Level level ) {
145
127
LevelHandler handler ;
146
128
Handler levelTarget = getConsoleHandler ();
147
- levelTarget .setFormatter (new SummaryFormatter ());
148
- if (getLevel ().intValue () <= level .intValue ()) {
149
- handler = new LevelHandler (levelTarget , bufferSize , level );
150
- } else {
151
- handler = new NoActionHandler (levelTarget , bufferSize , level );
152
- }
129
+ levelTarget .setFormatter (new SummaryFormatter (level ));
130
+ handler = new LevelHandler (levelTarget , bufferSize , level );
153
131
handler .setLevel (level );
154
132
handler .setFilter (getFilter ());
155
133
handlers .add (handler );
156
134
}
157
135
136
+ void summaryHead (Handler handler ) {
137
+ handler .publish (getLogRecord ("WLSDPLY-21003" , context .getProgramName (),
138
+ context .getVersion (), context .getWlstMode ()));
139
+ }
140
+
141
+ void summaryTail (Handler handler ) {
142
+ StringBuffer buffer = new StringBuffer ();
143
+ java .util .Formatter fmt = new java .util .Formatter (buffer );
144
+ for (LevelHandler levelHandler : handlers ) {
145
+ if (levelHandler .getTotalRecords () >= 0 ) {
146
+ fmt .format (" %1$s : %2$,5d" , levelHandler .getLevel ().getName (), levelHandler .getTotalRecords ());
147
+ }
148
+ }
149
+ handler .publish (getLogRecord ("WLSDPLY-21002" , buffer ));
150
+ }
151
+
158
152
private class TotalFormatter extends Formatter {
153
+
159
154
@ Override
160
155
public synchronized String format (LogRecord record ) {
161
- return formatMessage (record ) + System .lineSeparator ();
156
+ return System . lineSeparator () + formatMessage (record ) + System .lineSeparator ();
162
157
}
158
+
163
159
}
164
160
165
161
private class SummaryFormatter extends Formatter {
166
162
167
163
private final String MSG_FORMAT = " %1$5d. %2$s: %3$s" + System .lineSeparator ();
168
- private final String INTERNAL = "%s" + System .lineSeparator ();
164
+ private final String INTERNAL = System . lineSeparator () + "%s" + System . lineSeparator () + System .lineSeparator ();
169
165
private int sequence = 0 ;
166
+ private Level level ;
167
+
168
+ public SummaryFormatter (Level level ) {
169
+ this .level = level ;
170
+ }
170
171
171
172
@ Override
172
173
public synchronized String format (LogRecord record ) {
@@ -178,19 +179,14 @@ public synchronized String format(LogRecord record) {
178
179
String formatted = formatMessage (record );
179
180
if (msgId != null && !msgId .equals (formatted )) {
180
181
// this has a msg id. don't post any that don't have msg id.
181
- if (msgId .startsWith ("WLSDPLY-21" )) {
182
- message = String .format (INTERNAL , formatted );
183
- } else {
184
- message = String .format (MSG_FORMAT , ++sequence , msgId , formatted );
185
- }
182
+ message = String .format (MSG_FORMAT , ++sequence , msgId , formatted );
186
183
}
187
184
return message ;
188
185
}
189
186
190
187
@ Override
191
188
public String getHead (Handler handler ) {
192
- return formatMessage (getLogRecord ("WLSDPLY-21000" , handler .getLevel ().getLocalizedName ()))
193
- + System .lineSeparator ();
189
+ return String .format (INTERNAL , formatMessage (getLogRecord ("WLSDPLY-21000" , level .getLocalizedName ())));
194
190
}
195
191
}
196
192
@@ -211,84 +207,17 @@ public synchronized void publish(LogRecord record) {
211
207
}
212
208
}
213
209
214
- public synchronized int pushSection () {
215
- super .push ();
216
- int result = totalRecords ;
217
- totalRecords = 0 ;
218
- return result ;
219
- }
220
-
221
210
int getTotalRecords () {
222
211
return totalRecords ;
223
212
}
224
213
225
- void logStart () {
226
- if (getTotalRecords () > 0 ) {
227
- System .out .println (LINE_SEPARATION );
228
- target .publish (getLogRecord ("WLSDPLY-21000" , getLevel ().getName ()));
229
- System .out .println (LINE_SEPARATION );
230
- }
231
- }
232
-
233
- void logEnd () {
234
- System .out .println (LINE_SEPARATION );
235
- target .publish (getLogRecord ("WLSDPLY-21001" , getLevel ().getName (), getTotalRecords ()));
236
- }
237
- }
238
-
239
- private class NoActionHandler extends LevelHandler {
240
-
241
- NoActionHandler (Handler handler , int size , Level level ) {
242
- super (handler , size , level );
243
- }
244
-
245
-
246
- @ Override
247
- public void publish (LogRecord record ) {
248
-
249
- }
250
-
251
- @ Override
252
- public int pushSection () {
253
- return getTotalRecords ();
254
- }
255
-
256
- @ Override
257
- public void push () {
258
-
259
- }
260
-
261
- @ Override
262
- public void flush () {
263
-
264
- }
265
-
266
- @ Override
267
- public void close () throws SecurityException {
268
-
269
- }
270
-
271
- @ Override
272
- public int getTotalRecords () {
273
- return -1 ;
274
- }
275
-
276
- @ Override
277
- void logStart () {
278
-
279
- }
280
-
281
- @ Override
282
- void logEnd () {
283
-
284
- }
285
214
}
286
215
287
216
private void configure () {
288
217
LogManager manager = LogManager .getLogManager ();
289
- String cname = getClass (). getName ();
290
-
291
- bufferSize = getSize (manager .getProperty (cname + "." + SIZE_PROPERTY ));
218
+ topTarget = getConsoleHandler ();
219
+ topTarget . setFormatter ( new TotalFormatter ());
220
+ bufferSize = getSize (manager .getProperty (getClass (). getName () + "." + SIZE_PROPERTY ));
292
221
}
293
222
294
223
private int getSize (String propSize ) {
@@ -313,6 +242,7 @@ private ConsoleHandler getConsoleHandler() {
313
242
return handler ;
314
243
}
315
244
245
+
316
246
private LogRecord getLogRecord (String msg , Object ... params ) {
317
247
LogRecord record = new LogRecord (Level .INFO , msg );
318
248
record .setLoggerName (LOGGER .getName ());
@@ -324,4 +254,5 @@ private LogRecord getLogRecord(String msg, Object... params) {
324
254
record .setResourceBundle (LOGGER .getUnderlyingLogger ().getResourceBundle ());
325
255
return record ;
326
256
}
257
+
327
258
}
0 commit comments