1+ /*
2+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+ * The Universal Permissive License (UPL), Version 1.0
4+ */
15package oracle .weblogic .deploy .logging ;
26
3- import java .text .MessageFormat ;
4- import java .util .*;
5- import java .util .logging .*;
6- import java .util .logging .Formatter ;
7-
8-
7+ import java .util .ArrayList ;
8+ import java .util .List ;
9+ import java .util .Properties ;
10+ import java .util .logging .ConsoleHandler ;
11+ import java .util .logging .Handler ;
12+ import java .util .logging .Level ;
13+ import java .util .logging .LogManager ;
14+ import java .util .logging .LogRecord ;
15+ import java .util .logging .MemoryHandler ;
16+
17+
18+ /**
19+ * This class save the log records logged by the tool at Info level or greater. The WLSDeployExit exit method will
20+ * call this Handler to publish the messages, along with the total of the log records, by Level category.
21+ *
22+ * The WLSDeployCustomizeLoggingConfig adds the properties from this class' getHandlerProperties() to the
23+ * log manager logger properties and adds the handler to the root WLSDEPLOY Logger. See the class for information
24+ * on how to inject this handler into the wlsdeploy root logger.
25+ *
26+ * Before the tool exit, if specified by the caller, a recap of the saved logs is displayed to the console.
27+ * A final total of the records logged by the tool for the Level categories indicated above is displayed to the console.
28+ *
29+ * @see WLSDeployCustomizeLoggingConfig
30+ * @see oracle.weblogic.deploy.util.WLSDeployExit
31+ */
932public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHandler {
1033 private static final String CLASS = SummaryHandler .class .getName ();
1134 private static final String LEVEL_PROPERTY = "level" ;
@@ -16,13 +39,16 @@ public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHand
1639 private static final String LINE_SEPARATION = System .lineSeparator ();
1740
1841 private PlatformLogger LOGGER = WLSDeployLogFactory .getLogger ("wlsdeploy.exit" );
19- private boolean online = false ;
42+ private boolean recap = false ;
2043 private int bufferSize ;
2144
2245 private Handler target ;
2346 private List <LevelHandler > handlers = new ArrayList <>();
2447 private boolean closed = false ;
2548
49+ /**
50+ * This default constructor is populated with the handler properties loaded by the WLSDeployCustomizeLoggingConfig.
51+ */
2652 public SummaryHandler () {
2753 super ();
2854 configure ();
@@ -31,9 +57,14 @@ public SummaryHandler() {
3157 addLevelHandler (Level .INFO );
3258 addLevelHandler (Level .WARNING );
3359 addLevelHandler (Level .SEVERE );
34- System .out .println ("*** summary handler" );
3560 }
3661
62+ /**
63+ * Tally and save the log record if it matches one of the category Level handlers. Once the summary has completed,
64+ * all further log records will be ignored.
65+ *
66+ * @param record to tally and save in handler with matching Level category
67+ */
3768 @ Override
3869 public synchronized void publish (LogRecord record ) {
3970 // after close, take yourself out of the mix. The stored up log messages are going to go to the
@@ -45,9 +76,11 @@ public synchronized void publish(LogRecord record) {
4576 }
4677 }
4778
79+ /**
80+ * The Summary Handler will publish the recaps and total. The log records are discarded and the total reset.
81+ */
4882 @ Override
4983 public synchronized void push () {
50- System .out .println ("i am in push " );
5184 String METHOD = "push" ;
5285 LOGGER .entering (CLASS , METHOD );
5386 closed = true ;
@@ -58,7 +91,7 @@ public synchronized void push() {
5891 int count = handler .pushSection ();
5992 super .push ();
6093 if (count >= 0 ) {
61- fmt .format (" %1$s : %2$,5d" , handler .getLevel ().getName (), count );
94+ fmt .format (" %1$s : %2$,5d" , handler .getLevel ().getName (), count );
6295 }
6396 }
6497
@@ -76,17 +109,34 @@ public void close() throws SecurityException {
76109 super .close ();
77110 }
78111
79- public void setOnline (boolean isOnline ) {
80- online = isOnline ;
112+ /**
113+ * Specify if the log records should be displayed to the console before the tool exit.
114+ *
115+ * @param recap if true a recap of the log records will be performed
116+ */
117+ public void setRecap (boolean recap ) {
118+ this .recap = recap ;
81119 }
82120
83- public boolean isOnline () {
84- return online ;
121+ /**
122+ * Return whether the log records should be displayed to the console.
123+ *
124+ * @return true if a recap of the log records is specified
125+ */
126+ public boolean isRecap () {
127+ return recap ;
85128 }
86129
130+ /**
131+ * This method is called by the tool to complete the SummaryHandler, and display the recap and total information
132+ * to the console. The log records are only displayed to the console if the tool was run in online mode.
133+ * This compensates for wlst writing spurious blank lines to the console during online mode.
134+ *
135+ * @param onlineMode if true, a recap of the log records will be displayed
136+ */
87137 @ Override
88- public void logEnd (boolean online ) {
89- setOnline ( online );
138+ public void logEnd (boolean onlineMode ) {
139+ setRecap ( onlineMode );
90140 push ();
91141 }
92142
@@ -100,26 +150,20 @@ public static Properties getHandlerProperties() {
100150 Properties properties = new Properties ();
101151 properties .setProperty (LEVEL_PROPERTY , Level .INFO .getName ());
102152 properties .setProperty (TARGET_PROPERTY , WLSDeployLoggingConsoleHandler .class .getName ());
103- properties .setProperty (FORMATTER_PROPERTY , SummaryFormatter .class .getName ());
153+ properties .setProperty (FORMATTER_PROPERTY , WLSDeployLogFormatter .class .getName ());
104154 return properties ;
105155 }
106156
107- public class SummaryFormatter extends Formatter {
108- public synchronized String format (LogRecord logRecord ) {
109- // for now, only format the message in summary - maybe add logger name or other later
110- return formatMessage (logRecord );
111- }
112- }
113-
114157 private void addLevelHandler (Level level ) {
115- LevelHandler handler = null ;
158+ LevelHandler handler ;
116159 if (getLevel ().intValue () <= level .intValue ()) {
117160 handler = new LevelHandler (target , bufferSize , level );
118- handler .setLevel (level );
119- handler .setFilter (getFilter ());
120- //handler.setFormatter(new SummaryFormatter());
121- handlers .add (handler );
161+ } else {
162+ handler = new NoActionHandler (target , bufferSize , level );
122163 }
164+ handler .setLevel (level );
165+ handler .setFilter (getFilter ());
166+ handlers .add (handler );
123167 }
124168
125169 private class LevelHandler extends MemoryHandler {
@@ -140,12 +184,14 @@ public synchronized void publish(LogRecord record) {
140184 }
141185
142186 public synchronized int pushSection () {
143- if (isOnline ()) {
187+ if (isRecap ()) {
144188 logStart ();
145189 super .push ();
146190 logEnd ();
147191 }
148- return getTotalRecords ();
192+ int result = totalRecords ;
193+ totalRecords = 0 ;
194+ return result ;
149195 }
150196
151197 int getTotalRecords () {
0 commit comments