1515import java .util .logging .LogRecord ;
1616import java .util .logging .MemoryHandler ;
1717
18+ import oracle .weblogic .deploy .util .WLSDeployContext ;
19+ import oracle .weblogic .deploy .util .WLSDeployExit ;
20+
1821
1922/**
2023 * This class save the log records logged by the tool at Info level or greater. The WLSDeployExit exit method will
2427 * log manager logger properties and adds the handler to the root WLSDEPLOY Logger. See the class for information
2528 * on how to inject this handler into the wlsdeploy root logger.
2629 *
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.
2831 * A final total of the records logged by the tool for the Level categories indicated above is displayed to the console.
2932 *
3033 * @see WLSDeployCustomizeLoggingConfig
3134 * @see oracle.weblogic.deploy.util.WLSDeployExit
3235 */
33- public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHandler {
36+ public class SummaryHandler extends Handler implements WLSDeployLogEndHandler {
3437 private static final String CLASS = SummaryHandler .class .getName ();
3538 private static final String LEVEL_PROPERTY = "level" ;
3639 private static final String TARGET_PROPERTY = "target" ;
3740 private static final String FORMATTER_PROPERTY = "formatter" ;
3841 private static final String SIZE_PROPERTY = "size" ;
3942 private static final int DEFAULT_SIZE = 3000 ;
40- private static final String LINE_SEPARATION = System .lineSeparator ();
4143
4244 private PlatformLogger LOGGER = WLSDeployLogFactory .getLogger ("wlsdeploy.exit" );
43- private boolean online = false ;
44- private String toolName ;
4545 private int bufferSize ;
46+ private WLSDeployContext context ;
4647
47- private Handler target ;
48+ private Handler topTarget ;
4849 private List <LevelHandler > handlers = new ArrayList <>();
4950 private boolean closed = false ;
5051
@@ -54,10 +55,8 @@ public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHand
5455 public SummaryHandler () {
5556 super ();
5657 configure ();
57- target = getConsoleHandler ();
58- target .setFormatter (new TotalFormatter ());
58+
5959 LOGGER .setLevel (Level .INFO );
60- addLevelHandler (Level .INFO );
6160 addLevelHandler (Level .WARNING );
6261 addLevelHandler (Level .SEVERE );
6362 }
@@ -79,52 +78,35 @@ public synchronized void publish(LogRecord record) {
7978 }
8079 }
8180
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-
10781 @ Override
10882 public void flush () {
109- super .flush ();
83+ topTarget .flush ();
11084 }
11185
11286 @ Override
11387 public void close () throws SecurityException {
114- super .close ();
88+ topTarget .close ();
11589 }
11690
11791 /**
11892 * This method is called by the tool to complete the SummaryHandler, and display the recap and total information
11993 * to the console. The log records are only displayed to the console if the tool was run in online mode.
12094 * This compensates for wlst writing spurious blank lines to the console during online mode.
12195 *
122- * @param onlineMode if true, a recap of the log records will be displayed
96+ * @param modelContext contextual information about the tool
12397 */
12498 @ 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 );
128110 }
129111
130112 /**
@@ -144,29 +126,48 @@ public static Properties getHandlerProperties() {
144126 private void addLevelHandler (Level level ) {
145127 LevelHandler handler ;
146128 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 );
153131 handler .setLevel (level );
154132 handler .setFilter (getFilter ());
155133 handlers .add (handler );
156134 }
157135
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+
158152 private class TotalFormatter extends Formatter {
153+
159154 @ Override
160155 public synchronized String format (LogRecord record ) {
161- return formatMessage (record ) + System .lineSeparator ();
156+ return System . lineSeparator () + formatMessage (record ) + System .lineSeparator ();
162157 }
158+
163159 }
164160
165161 private class SummaryFormatter extends Formatter {
166162
167163 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 ();
169165 private int sequence = 0 ;
166+ private Level level ;
167+
168+ public SummaryFormatter (Level level ) {
169+ this .level = level ;
170+ }
170171
171172 @ Override
172173 public synchronized String format (LogRecord record ) {
@@ -178,19 +179,14 @@ public synchronized String format(LogRecord record) {
178179 String formatted = formatMessage (record );
179180 if (msgId != null && !msgId .equals (formatted )) {
180181 // 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 );
186183 }
187184 return message ;
188185 }
189186
190187 @ Override
191188 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 ())));
194190 }
195191 }
196192
@@ -211,84 +207,17 @@ public synchronized void publish(LogRecord record) {
211207 }
212208 }
213209
214- public synchronized int pushSection () {
215- super .push ();
216- int result = totalRecords ;
217- totalRecords = 0 ;
218- return result ;
219- }
220-
221210 int getTotalRecords () {
222211 return totalRecords ;
223212 }
224213
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- }
285214 }
286215
287216 private void configure () {
288217 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 ));
292221 }
293222
294223 private int getSize (String propSize ) {
@@ -313,6 +242,7 @@ private ConsoleHandler getConsoleHandler() {
313242 return handler ;
314243 }
315244
245+
316246 private LogRecord getLogRecord (String msg , Object ... params ) {
317247 LogRecord record = new LogRecord (Level .INFO , msg );
318248 record .setLoggerName (LOGGER .getName ());
@@ -324,4 +254,5 @@ private LogRecord getLogRecord(String msg, Object... params) {
324254 record .setResourceBundle (LOGGER .getUnderlyingLogger ().getResourceBundle ());
325255 return record ;
326256 }
257+
327258}
0 commit comments