Skip to content

Commit be181a1

Browse files
Working revision
1 parent 898862d commit be181a1

File tree

14 files changed

+303
-241
lines changed

14 files changed

+303
-241
lines changed

core/src/main/java/oracle/weblogic/deploy/logging/SummaryHandler.java

Lines changed: 71 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
11
package oracle.weblogic.deploy.logging;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
import java.util.Properties;
3+
import java.text.MessageFormat;
4+
import java.util.*;
65
import java.util.logging.*;
6+
import java.util.logging.Formatter;
77

88

99
public class SummaryHandler extends MemoryHandler implements WLSDeployLogEndHandler {
1010
private static final String CLASS = SummaryHandler.class.getName();
11-
private static final PlatformLogger LOGGER = WLSDeployLogFactory.getLogger("wlsdeploy");
1211
private static final String LEVEL_PROPERTY = "level";
1312
private static final String TARGET_PROPERTY = "target";
13+
private static final String FORMATTER_PROPERTY = "formatter";
1414
private static final String SIZE_PROPERTY = "size";
1515
private static final int DEFAULT_SIZE = 3000;
16+
private static final String LINE_SEPARATION = System.lineSeparator();
1617

18+
private PlatformLogger LOGGER = WLSDeployLogFactory.getLogger("wlsdeploy.exit");
1719
private boolean online = false;
1820
private int bufferSize;
1921

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};
22+
private Handler target;
23+
private List<LevelHandler> handlers = new ArrayList<>();
2424
private boolean closed = false;
2525

2626
public SummaryHandler() {
2727
super();
2828
configure();
29-
infoMemoryHandler = getLevelHandler(Level.INFO);
30-
infoMemoryHandler = getLevelHandler(Level.WARNING);
31-
infoMemoryHandler = getLevelHandler(Level.SEVERE);
29+
target = getConsoleHandler();
30+
LOGGER.setLevel(Level.INFO);
31+
addLevelHandler(Level.INFO);
32+
addLevelHandler(Level.WARNING);
33+
addLevelHandler(Level.SEVERE);
34+
System.out.println("*** summary handler");
3235
}
3336

3437
@Override
3538
public synchronized void publish(LogRecord record) {
3639
// after close, take yourself out of the mix. The stored up log messages are going to go to the
3740
// console handler anyway
3841
if (!closed) {
39-
infoMemoryHandler.publish(record);
40-
warnMemoryHandler.publish(record);
41-
errMemoryHandler.publish(record);
42+
for (Handler handler : handlers) {
43+
handler.publish(record);
44+
}
4245
}
4346
}
4447

4548
@Override
4649
public synchronized void push() {
50+
System.out.println("i am in push ");
4751
String METHOD = "push";
4852
LOGGER.entering(CLASS, METHOD);
4953
closed = true;
50-
List<Integer> counts = new ArrayList<>();
54+
setPushLevel(getLevel());
55+
StringBuffer buffer = new StringBuffer();
56+
java.util.Formatter fmt = new java.util.Formatter(buffer);
5157
for (LevelHandler handler : handlers) {
5258
int count = handler.pushSection();
59+
super.push();
5360
if (count >= 0) {
54-
counts.add(count);
61+
fmt.format(" %1$s : %2$,5d", handler.getLevel().getName(), count);
5562
}
5663
}
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);
64+
65+
System.out.println(LINE_SEPARATION);
66+
target.publish(getLogRecord("WLSDPLY-21002", buffer));
6767
}
6868

6969
@Override
@@ -93,48 +93,41 @@ public void logEnd(boolean online) {
9393
/**
9494
* The WLSDeployLoggingConfig will call this method to add to the logging.properties files.
9595
* If the logging.properties already contains the property, the property in this list will be ignored.
96+
*
9697
* @return properties to set in logging.properties
9798
*/
9899
public static Properties getHandlerProperties() {
99-
String METHOD = "getHandlerProperties";
100-
LOGGER.entering(CLASS, METHOD);
101100
Properties properties = new Properties();
102101
properties.setProperty(LEVEL_PROPERTY, Level.INFO.getName());
103102
properties.setProperty(TARGET_PROPERTY, WLSDeployLoggingConsoleHandler.class.getName());
104-
LOGGER.exiting(CLASS, METHOD, properties);
103+
properties.setProperty(FORMATTER_PROPERTY, SummaryFormatter.class.getName());
105104
return properties;
106105
}
107106

108-
private class SummaryFormatter extends Formatter {
107+
public class SummaryFormatter extends Formatter {
109108
public synchronized String format(LogRecord logRecord) {
110109
// for now, only format the message in summary - maybe add logger name or other later
111110
return formatMessage(logRecord);
112111
}
113112
}
114113

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);
114+
private void addLevelHandler(Level level) {
115+
LevelHandler handler = null;
116+
if (getLevel().intValue() <= level.intValue()) {
117+
handler = new LevelHandler(target, bufferSize, level);
118+
handler.setLevel(level);
119+
handler.setFilter(getFilter());
120+
//handler.setFormatter(new SummaryFormatter());
121+
handlers.add(handler);
124122
}
125-
return handler;
126123
}
127124

128125
private class LevelHandler extends MemoryHandler {
129126

130127
private int totalRecords;
131128

132129
LevelHandler(Handler handler, int size, Level level) {
133-
super(handler, size, Level.ALL);
134-
setLevel(level);
135-
}
136-
137-
LevelHandler(Level level) {
130+
super(handler, size, Level.OFF);
138131
setLevel(level);
139132
}
140133

@@ -146,7 +139,7 @@ public synchronized void publish(LogRecord record) {
146139
}
147140
}
148141

149-
public int pushSection() {
142+
public synchronized int pushSection() {
150143
if (isOnline()) {
151144
logStart();
152145
super.push();
@@ -160,18 +153,23 @@ int getTotalRecords() {
160153
}
161154

162155
void logStart() {
163-
LOGGER.log(Level.ALL, "WLSDPLY-2100", getLevel().getName());
156+
if (getTotalRecords() > 0) {
157+
System.out.println(LINE_SEPARATION);
158+
target.publish(getLogRecord("WLSDPLY-21000", getLevel().getName()));
159+
System.out.println(LINE_SEPARATION);
160+
}
164161
}
165162

166163
void logEnd() {
167-
LOGGER.log(Level.ALL, "WLSDPLY-2101", getLevel().getName(), getTotalRecords());
164+
System.out.println(LINE_SEPARATION);
165+
target.publish(getLogRecord("WLSDPLY-21001", getLevel().getName(), getTotalRecords()));
168166
}
169167
}
170168

171169
private class NoActionHandler extends LevelHandler {
172170

173-
NoActionHandler(Level level) {
174-
super(level);
171+
NoActionHandler(Handler handler, int size, Level level) {
172+
super(handler, size, level);
175173
}
176174

177175

@@ -233,4 +231,27 @@ private int getSize(String propSize) {
233231
return handlerSize;
234232
}
235233

234+
private ConsoleHandler getConsoleHandler() {
235+
ConsoleHandler handler = null;
236+
try {
237+
handler = (ConsoleHandler) Class.forName(WLSDeployLoggingConfig.getConsoleHandler()).newInstance();
238+
} catch (ClassNotFoundException | IllegalAccessException cne) {
239+
System.out.println("Class not found " + WLSDeployLoggingConfig.getConsoleHandler());
240+
} catch (InstantiationException ie) {
241+
handler = new ConsoleHandler();
242+
}
243+
return handler;
244+
}
245+
246+
private LogRecord getLogRecord(String msg, Object... params) {
247+
LogRecord record = new LogRecord(Level.INFO, msg);
248+
record.setLoggerName(LOGGER.getName());
249+
if (params != null && params.length != 0) {
250+
record.setParameters(params);
251+
}
252+
record.setSourceClassName(CLASS);
253+
record.setSourceMethodName("");
254+
record.setResourceBundle(LOGGER.getUnderlyingLogger().getResourceBundle());
255+
return record;
256+
}
236257
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package oracle.weblogic.deploy.logging;
2+
3+
import oracle.weblogic.deploy.util.StringUtils;
4+
5+
import java.lang.reflect.Method;
6+
import java.text.MessageFormat;
7+
import java.util.*;
8+
import java.util.logging.Handler;
9+
10+
public class WLSDeployCustomizeLoggingConfig extends WLSDeployLoggingConfig {
11+
12+
private static final String WLSDEPLOY_HANDLER_PROP = WLSDEPLOY_LOGGER_NAME + ".handlers";
13+
private static final String WLSDEPLOY_HANDLER_METHOD = "getHandlerProperties";
14+
15+
@Override
16+
public void customizeLoggingProperties(String programName, Properties logProps) {
17+
List<Class<?>> classList = new ArrayList<>();
18+
for (String handlerName : findExtraHandlers(logProps)) {
19+
classList.add(getHandlerClass(handlerName));
20+
}
21+
List<String> handlerList = new ArrayList<>();
22+
for (Class<?> handler : classList) {
23+
handlerList.add(handler.getName());
24+
addHandlerProperties(logProps, handler);
25+
}
26+
logProps.setProperty(WLSDEPLOY_HANDLER_PROP, StringUtils.getCommaSeparatedListString(handlerList));
27+
}
28+
29+
private static Set<String> findExtraHandlers(Properties logProps) {
30+
// The handlers are applied in order - process environment variable first, then logging properties
31+
Set<String> handlers = new HashSet<>();
32+
String[] addTo = StringUtils.splitCommaSeparatedList(System.getenv(WLSDEPLOY_LOG_HANDLERS_ENV_VARIABLE));
33+
if (addTo.length > 0) {
34+
handlers.addAll(Arrays.asList(addTo));
35+
}
36+
addTo = StringUtils.splitCommaSeparatedList(logProps.getProperty(WLSDEPLOY_HANDLER_PROP));
37+
if (addTo.length > 0) {
38+
handlers.addAll(Arrays.asList(addTo));
39+
}
40+
return handlers;
41+
}
42+
private static Class<?> getHandlerClass(String handlerName) {
43+
String message = null;
44+
Class<?> handler = null;
45+
try {
46+
handler = Class.forName(handlerName);
47+
if (!Handler.class.isAssignableFrom(handler)) {
48+
message = MessageFormat.format("Class {0} is not a Handler", handlerName);
49+
}
50+
} catch(ClassNotFoundException cnf) {
51+
message = MessageFormat.format("Unable to find handler class {0} so skipping logging configuration",
52+
handlerName);
53+
}
54+
if (message != null) {
55+
System.err.println(message);
56+
System.exit(ERROR_EXIT_CODE);
57+
}
58+
return handler;
59+
}
60+
61+
private static void addHandlerProperties(Properties logProps, Class<?> clazz) {
62+
Properties props = null;
63+
// a forEach would be good here!
64+
String clazzName = clazz.getName();
65+
// make sure some formatter is set
66+
//logProps.setProperty(clazzName + HANDLER_FORMATTER_PROP, LOG_FORMATTER);
67+
Set<String> propSet = logProps.stringPropertyNames();
68+
try {
69+
// only look in this class
70+
Method method = clazz.getDeclaredMethod(WLSDEPLOY_HANDLER_METHOD);
71+
props = (Properties)method.invoke(null);
72+
} catch (NoSuchMethodException nsm) {
73+
return;
74+
} catch (Exception e) {
75+
String message = MessageFormat.format("Unable to successfully populate properties for handler {0} " +
76+
"so skipping logging configuration : {1}", clazzName, e.getLocalizedMessage());
77+
System.err.println(message);
78+
System.exit(ERROR_EXIT_CODE);
79+
}
80+
81+
if (props != null) {
82+
for (Map.Entry<?, ?> listItem : props.entrySet()) {
83+
if (listItem.getKey() instanceof String && listItem.getValue() instanceof String) {
84+
// requires the handler property name without the handler class name
85+
// this method will add the class to make sure the handler is not setting global properties
86+
// or properties for other handlers
87+
String property = clazzName + '.' + listItem.getKey();
88+
if (!propSet.contains(property)) {
89+
// logging.properties property takes precedent
90+
logProps.setProperty(property, (String)listItem.getValue());
91+
}
92+
}
93+
}
94+
}
95+
}
96+
97+
}

core/src/main/java/oracle/weblogic/deploy/logging/WLSDeployLogEndHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* before ending within a weblogic deploy tool. A class that implements this interface should include
1010
* the appropriate wrap-up action in the logEnd method.
1111
*
12-
* The corresponding WLSDeployBeforeExitAction will look through the Handlers on the LogManager and call the logEnd
12+
* The corresponding WLSDeployExit will look through the Handlers on the LogManager and call the logEnd
1313
* method if the interface is implemented.
1414
*/
1515
public interface WLSDeployLogEndHandler {

0 commit comments

Comments
 (0)