Skip to content

Commit d9fd39d

Browse files
Refinement of handler search on exit. Add javadoc and cleanupt
1 parent be181a1 commit d9fd39d

File tree

7 files changed

+453
-70
lines changed

7 files changed

+453
-70
lines changed

core/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@
4040
<artifactId>junit</artifactId>
4141
<scope>test</scope>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.easymock</groupId>
45+
<artifactId>easymock</artifactId>
46+
<version>3.6</version>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.powermock</groupId>
51+
<artifactId>powermock-api-easymock</artifactId>
52+
<version>1.6.6</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.powermock</groupId>
57+
<artifactId>powermock-module-junit4</artifactId>
58+
<version>1.6.6</version>
59+
<scope>test</scope>
60+
</dependency>
4361
</dependencies>
4462

4563
<build>

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
*/
55
package oracle.weblogic.deploy.logging;
66

7+
import java.util.ArrayList;
8+
import java.util.Enumeration;
9+
import java.util.List;
710
import java.util.logging.Level;
11+
import java.util.logging.LogManager;
812
import java.util.logging.LogRecord;
913
import java.util.logging.Logger;
1014

@@ -526,6 +530,23 @@ public void warning(String msg, Throwable thrown) {
526530
logger.logp(Level.WARNING, details.clazz, details.method, msg, thrown);
527531
}
528532

533+
/**
534+
* Return a list of the current loggers.
535+
*
536+
* @return List of Logger from the Log Manager
537+
*/
538+
public static List<Logger> getLoggers() {
539+
LogManager manager = LogManager.getLogManager();
540+
Enumeration<String> e = manager.getLoggerNames();
541+
List<Logger> topList = new ArrayList<>();
542+
while (e.hasMoreElements()) {
543+
String loggerName = e.nextElement();
544+
Logger logger = manager.getLogger(loggerName);
545+
topList.add(logger);
546+
}
547+
return topList;
548+
}
549+
529550
/**
530551
* Obtains caller details, class name and method, to be provided to the actual Logger. This
531552
* code is adapted from ODLLogRecord, which should yield consistency in reporting using

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

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* The Universal Permissive License (UPL), Version 1.0
4+
*/
15
package 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+
*/
932
public 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() {

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* The Universal Permissive License (UPL), Version 1.0
4+
*/
15
package oracle.weblogic.deploy.logging;
26

37
import oracle.weblogic.deploy.util.StringUtils;
@@ -7,11 +11,41 @@
711
import java.util.*;
812
import java.util.logging.Handler;
913

14+
/**
15+
* Use this class to add custom handlers to the root WLSDEPLOY logger. The WLSDeployLoggingConfig
16+
* loads the properties to the LogManager. .
17+
*
18+
* <p>You must add this class to the -Djava.util.logging.config.class parameter before the tool JVM is started.
19+
* To inject one or more handlers into the WLSDEPLOY logger, you may either add the comma separated list to the
20+
* logger.properties file as <WLSDEPLOY_LOGGER_NAME>.handlers=<list>, or set the list on the environment variable
21+
* WLSDEPLOY_LOG_HANDLERS_ENV_VARIABLE. The property in the logging.properties file takes precedence
22+
* over the environment variable.
23+
*
24+
* <p>This class will check each handler in the list to see if it has implemented the WLSDEPLOY_HANDLER_METHOD. This method
25+
* returns a handler's properties. Any property that already exists in the logging.properties will be discarded -
26+
* as logging.properties takes precedence. Any property that does not start with the handler class name will
27+
* be rejected. This class' ONLY purpose is to dynamically add a handler and/or its properties to the
28+
* WLSDEPLOY root logger.
29+
*
30+
* <p>The WLSDEPLOY logger is instantiated by WLSDeployLoggingConfig (and thus, the handlers are
31+
* instantiated). The WLSDEPLOY logger's parameters are not inherited by a child logger unless the
32+
* parameter instances already exist before the child logger is added.
33+
*/
1034
public class WLSDeployCustomizeLoggingConfig extends WLSDeployLoggingConfig {
1135

1236
private static final String WLSDEPLOY_HANDLER_PROP = WLSDEPLOY_LOGGER_NAME + ".handlers";
1337
private static final String WLSDEPLOY_HANDLER_METHOD = "getHandlerProperties";
1438

39+
/**
40+
* Check the logging.properties file for a "WLSDEPLOY".handlers property. If the property does not exist,
41+
* check to see if the WLSDEPLOY_LOG_HANDLERS_ENV_VARIABLE environment variable has been set.
42+
*
43+
* <p>Check each handler to see if has implemented the WLSDEPLOY_HANDLER_METHOD. A property
44+
* returned from the method is added to the provided properties if the property does not exist in the list.
45+
*
46+
* @param programName name of the tool calling this method
47+
* @param logProps properties comprised by the WLSDeployLoggingConfig class as the base list
48+
*/
1549
@Override
1650
public void customizeLoggingProperties(String programName, Properties logProps) {
1751
List<Class<?>> classList = new ArrayList<>();
@@ -39,6 +73,7 @@ private static Set<String> findExtraHandlers(Properties logProps) {
3973
}
4074
return handlers;
4175
}
76+
4277
private static Class<?> getHandlerClass(String handlerName) {
4378
String message = null;
4479
Class<?> handler = null;
@@ -62,8 +97,6 @@ private static void addHandlerProperties(Properties logProps, Class<?> clazz) {
6297
Properties props = null;
6398
// a forEach would be good here!
6499
String clazzName = clazz.getName();
65-
// make sure some formatter is set
66-
//logProps.setProperty(clazzName + HANDLER_FORMATTER_PROP, LOG_FORMATTER);
67100
Set<String> propSet = logProps.stringPropertyNames();
68101
try {
69102
// only look in this class

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
package oracle.weblogic.deploy.logging;
66

77
/**
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
8+
* This interface should be used by a Logger Handler that wishes to perform some action or clean-up
9+
* before the wlsdeploy tool exits. A class that implements this interface should include
1010
* the appropriate wrap-up action in the logEnd method.
1111
*
12-
* The corresponding WLSDeployExit will look through the Handlers on the LogManager and call the logEnd
13-
* method if the interface is implemented.
12+
* <p>If the tool calls the WLSDeployExit.exit(), that method will search the Handlers on all WLSDEPLOY
13+
* loggers. If a handler implements this interface, the logEnd method is called. The handlers' methods are called
14+
* in parent / child order. If the same handler instance is found on more than one logger, the handler is only called
15+
* once, with parent / child order insured for all.
16+
*
17+
* @see oracle.weblogic.deploy.util.WLSDeployExit
1418
*/
1519
public interface WLSDeployLogEndHandler {
1620

21+
/**
22+
* The handler performs any wrap-up action.
23+
*
24+
* @param online true if the tool is running in online mode.
25+
*/
1726
void logEnd(boolean online);
1827

1928
}

0 commit comments

Comments
 (0)