Skip to content

Commit 898862d

Browse files
first blush
1 parent ea4bc08 commit 898862d

File tree

9 files changed

+454
-13
lines changed

9 files changed

+454
-13
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
package oracle.weblogic.deploy.logging;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Properties;
6+
import java.util.logging.*;
7+
8+
9+
public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHandler {
10+
private static final String CLASS = SummaryHandler.class.getName();
11+
private static final PlatformLogger LOGGER = WLSDeployLogFactory.getLogger("wlsdeploy");
12+
private static final String LEVEL_PROPERTY = "level";
13+
private static final String TARGET_PROPERTY = "target";
14+
private static final String SIZE_PROPERTY = "size";
15+
private static final int DEFAULT_SIZE = 3000;
16+
17+
private boolean online = false;
18+
private int bufferSize;
19+
20+
private LevelHandler infoMemoryHandler = new NoActionHandler(Level.INFO);
21+
private LevelHandler warnMemoryHandler = new NoActionHandler(Level.WARNING);
22+
private LevelHandler errMemoryHandler = new NoActionHandler(Level.SEVERE);
23+
private LevelHandler[] handlers = {infoMemoryHandler, warnMemoryHandler, errMemoryHandler};
24+
private boolean closed = false;
25+
26+
public SummaryHandler() {
27+
super();
28+
configure();
29+
infoMemoryHandler = getLevelHandler(Level.INFO);
30+
infoMemoryHandler = getLevelHandler(Level.WARNING);
31+
infoMemoryHandler = getLevelHandler(Level.SEVERE);
32+
}
33+
34+
@Override
35+
public synchronized void publish(LogRecord record) {
36+
// after close, take yourself out of the mix. The stored up log messages are going to go to the
37+
// console handler anyway
38+
if (!closed) {
39+
infoMemoryHandler.publish(record);
40+
warnMemoryHandler.publish(record);
41+
errMemoryHandler.publish(record);
42+
}
43+
}
44+
45+
@Override
46+
public synchronized void push() {
47+
String METHOD = "push";
48+
LOGGER.entering(CLASS, METHOD);
49+
closed = true;
50+
List<Integer> counts = new ArrayList<>();
51+
for (LevelHandler handler : handlers) {
52+
int count = handler.pushSection();
53+
if (count >= 0) {
54+
counts.add(count);
55+
}
56+
}
57+
int size = counts.size();
58+
String msgNbr = "WLSDPLY-21004";
59+
if (size == 2) {
60+
msgNbr = "WLSDPLY-21003";
61+
} else if (size == 3) {
62+
msgNbr = "WLSDPLY-21002";
63+
}
64+
// got to fix this as this isn't allowed ? check to see if it resolves the msg nbr. Is this going to be 1 arg?
65+
LOGGER.log(Level.ALL, msgNbr, (Object[])counts.toArray(new Object[size]));
66+
LOGGER.exiting(CLASS, METHOD);
67+
}
68+
69+
@Override
70+
public void flush() {
71+
super.flush();
72+
}
73+
74+
@Override
75+
public void close() throws SecurityException {
76+
super.close();
77+
}
78+
79+
public void setOnline(boolean isOnline) {
80+
online = isOnline;
81+
}
82+
83+
public boolean isOnline() {
84+
return online;
85+
}
86+
87+
@Override
88+
public void logEnd(boolean online) {
89+
setOnline(online);
90+
push();
91+
}
92+
93+
/**
94+
* The WLSDeployLoggingConfig will call this method to add to the logging.properties files.
95+
* If the logging.properties already contains the property, the property in this list will be ignored.
96+
* @return properties to set in logging.properties
97+
*/
98+
public static Properties getHandlerProperties() {
99+
String METHOD = "getHandlerProperties";
100+
LOGGER.entering(CLASS, METHOD);
101+
Properties properties = new Properties();
102+
properties.setProperty(LEVEL_PROPERTY, Level.INFO.getName());
103+
properties.setProperty(TARGET_PROPERTY, WLSDeployLoggingConsoleHandler.class.getName());
104+
LOGGER.exiting(CLASS, METHOD, properties);
105+
return properties;
106+
}
107+
108+
private class SummaryFormatter extends Formatter {
109+
public synchronized String format(LogRecord logRecord) {
110+
// for now, only format the message in summary - maybe add logger name or other later
111+
return formatMessage(logRecord);
112+
}
113+
}
114+
115+
private LevelHandler getLevelHandler(Level compareTo) {
116+
LevelHandler handler;
117+
if (getLevel().intValue() <= compareTo.intValue()) {
118+
handler = new LevelHandler(this, bufferSize, Level.ALL);
119+
setLevel(compareTo);
120+
setFilter(getFilter());
121+
setFormatter(new SummaryFormatter());
122+
} else {
123+
handler = new NoActionHandler(compareTo);
124+
}
125+
return handler;
126+
}
127+
128+
private class LevelHandler extends MemoryHandler {
129+
130+
private int totalRecords;
131+
132+
LevelHandler(Handler handler, int size, Level level) {
133+
super(handler, size, Level.ALL);
134+
setLevel(level);
135+
}
136+
137+
LevelHandler(Level level) {
138+
setLevel(level);
139+
}
140+
141+
@Override
142+
public synchronized void publish(LogRecord record) {
143+
if (record.getLevel().intValue() == getLevel().intValue()) {
144+
++totalRecords;
145+
super.publish(record);
146+
}
147+
}
148+
149+
public int pushSection() {
150+
if (isOnline()) {
151+
logStart();
152+
super.push();
153+
logEnd();
154+
}
155+
return getTotalRecords();
156+
}
157+
158+
int getTotalRecords() {
159+
return totalRecords;
160+
}
161+
162+
void logStart() {
163+
LOGGER.log(Level.ALL, "WLSDPLY-2100", getLevel().getName());
164+
}
165+
166+
void logEnd() {
167+
LOGGER.log(Level.ALL, "WLSDPLY-2101", getLevel().getName(), getTotalRecords());
168+
}
169+
}
170+
171+
private class NoActionHandler extends LevelHandler {
172+
173+
NoActionHandler(Level level) {
174+
super(level);
175+
}
176+
177+
178+
@Override
179+
public void publish(LogRecord record) {
180+
181+
}
182+
183+
@Override
184+
public int pushSection() {
185+
return getTotalRecords();
186+
}
187+
188+
@Override
189+
public void push() {
190+
191+
}
192+
193+
@Override
194+
public void flush() {
195+
196+
}
197+
198+
@Override
199+
public void close() throws SecurityException {
200+
201+
}
202+
203+
@Override
204+
public int getTotalRecords() {
205+
return -1;
206+
}
207+
208+
@Override
209+
void logStart() {
210+
211+
}
212+
213+
@Override
214+
void logEnd() {
215+
216+
}
217+
}
218+
219+
private void configure() {
220+
LogManager manager = LogManager.getLogManager();
221+
String cname = getClass().getName();
222+
223+
bufferSize = getSize(manager.getProperty(cname + "." + SIZE_PROPERTY));
224+
}
225+
226+
private int getSize(String propSize) {
227+
Integer handlerSize;
228+
try {
229+
handlerSize = new Integer(propSize);
230+
} catch (NumberFormatException nfe) {
231+
handlerSize = DEFAULT_SIZE;
232+
}
233+
return handlerSize;
234+
}
235+
236+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3+
* The Universal Permissive License (UPL), Version 1.0
4+
*/
5+
package oracle.weblogic.deploy.logging;
6+
7+
/**
8+
* This interface should be used by a Log Handler that wishes to perform some action or clean-up
9+
* before ending within a weblogic deploy tool. A class that implements this interface should include
10+
* the appropriate wrap-up action in the logEnd method.
11+
*
12+
* The corresponding WLSDeployBeforeExitAction will look through the Handlers on the LogManager and call the logEnd
13+
* method if the interface is implemented.
14+
*/
15+
public interface WLSDeployLogEndHandler {
16+
17+
void logEnd(boolean online);
18+
19+
}

0 commit comments

Comments
 (0)