1
+ /*
2
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
+ * The Universal Permissive License (UPL), Version 1.0
4
+ */
1
5
package oracle .weblogic .deploy .logging ;
2
6
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
+ */
9
32
public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHandler {
10
33
private static final String CLASS = SummaryHandler .class .getName ();
11
34
private static final String LEVEL_PROPERTY = "level" ;
@@ -16,13 +39,16 @@ public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHand
16
39
private static final String LINE_SEPARATION = System .lineSeparator ();
17
40
18
41
private PlatformLogger LOGGER = WLSDeployLogFactory .getLogger ("wlsdeploy.exit" );
19
- private boolean online = false ;
42
+ private boolean recap = false ;
20
43
private int bufferSize ;
21
44
22
45
private Handler target ;
23
46
private List <LevelHandler > handlers = new ArrayList <>();
24
47
private boolean closed = false ;
25
48
49
+ /**
50
+ * This default constructor is populated with the handler properties loaded by the WLSDeployCustomizeLoggingConfig.
51
+ */
26
52
public SummaryHandler () {
27
53
super ();
28
54
configure ();
@@ -31,9 +57,14 @@ public SummaryHandler() {
31
57
addLevelHandler (Level .INFO );
32
58
addLevelHandler (Level .WARNING );
33
59
addLevelHandler (Level .SEVERE );
34
- System .out .println ("*** summary handler" );
35
60
}
36
61
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
+ */
37
68
@ Override
38
69
public synchronized void publish (LogRecord record ) {
39
70
// 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) {
45
76
}
46
77
}
47
78
79
+ /**
80
+ * The Summary Handler will publish the recaps and total. The log records are discarded and the total reset.
81
+ */
48
82
@ Override
49
83
public synchronized void push () {
50
- System .out .println ("i am in push " );
51
84
String METHOD = "push" ;
52
85
LOGGER .entering (CLASS , METHOD );
53
86
closed = true ;
@@ -58,7 +91,7 @@ public synchronized void push() {
58
91
int count = handler .pushSection ();
59
92
super .push ();
60
93
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 );
62
95
}
63
96
}
64
97
@@ -76,17 +109,34 @@ public void close() throws SecurityException {
76
109
super .close ();
77
110
}
78
111
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 ;
81
119
}
82
120
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 ;
85
128
}
86
129
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
+ */
87
137
@ Override
88
- public void logEnd (boolean online ) {
89
- setOnline ( online );
138
+ public void logEnd (boolean onlineMode ) {
139
+ setRecap ( onlineMode );
90
140
push ();
91
141
}
92
142
@@ -100,26 +150,20 @@ public static Properties getHandlerProperties() {
100
150
Properties properties = new Properties ();
101
151
properties .setProperty (LEVEL_PROPERTY , Level .INFO .getName ());
102
152
properties .setProperty (TARGET_PROPERTY , WLSDeployLoggingConsoleHandler .class .getName ());
103
- properties .setProperty (FORMATTER_PROPERTY , SummaryFormatter .class .getName ());
153
+ properties .setProperty (FORMATTER_PROPERTY , WLSDeployLogFormatter .class .getName ());
104
154
return properties ;
105
155
}
106
156
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
-
114
157
private void addLevelHandler (Level level ) {
115
- LevelHandler handler = null ;
158
+ LevelHandler handler ;
116
159
if (getLevel ().intValue () <= level .intValue ()) {
117
160
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 );
122
163
}
164
+ handler .setLevel (level );
165
+ handler .setFilter (getFilter ());
166
+ handlers .add (handler );
123
167
}
124
168
125
169
private class LevelHandler extends MemoryHandler {
@@ -140,12 +184,14 @@ public synchronized void publish(LogRecord record) {
140
184
}
141
185
142
186
public synchronized int pushSection () {
143
- if (isOnline ()) {
187
+ if (isRecap ()) {
144
188
logStart ();
145
189
super .push ();
146
190
logEnd ();
147
191
}
148
- return getTotalRecords ();
192
+ int result = totalRecords ;
193
+ totalRecords = 0 ;
194
+ return result ;
149
195
}
150
196
151
197
int getTotalRecords () {
0 commit comments