Skip to content

Commit 906bc47

Browse files
CarolynRountreeddsharpe
authored andcommitted
separate console STDERR and STDOUT (#429)
1 parent 66fc3fb commit 906bc47

10 files changed

+336
-66
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
*/
5+
package oracle.weblogic.deploy.logging;
6+
7+
import java.text.MessageFormat;
8+
import java.util.Properties;
9+
import java.util.logging.Handler;
10+
import java.util.logging.LogRecord;
11+
12+
import static oracle.weblogic.deploy.logging.WLSDeployLoggingConfig.ERROR_EXIT_CODE;
13+
14+
public class LoggingUtils {
15+
16+
public static <T extends Handler> Class<T> getHandlerClass(String handlerName) {
17+
Class<T> handler = null;
18+
try {
19+
Class<?> checkClass = Class.forName(handlerName);
20+
handler = (Class<T>)checkClass.asSubclass(Class.forName(handlerName));
21+
} catch(ClassNotFoundException | ClassCastException cnf) {
22+
exitWithError(
23+
MessageFormat.format("Unable to find handler class {0} so skipping logging configuration",
24+
handlerName));
25+
}
26+
return handler;
27+
}
28+
29+
public static <T extends Handler> T getHandlerInstance(Class<T> handlerClass) {
30+
T handler = null;
31+
try {
32+
handler = handlerClass.newInstance();
33+
} catch (InstantiationException | IllegalAccessException e){
34+
exitWithError(MessageFormat.format("Unable to instantiate Handler for Class {0}", handlerClass));
35+
}
36+
return handler;
37+
}
38+
39+
public static <T extends Handler> T getHandlerInstance(String handlerClassName) {
40+
return getHandlerInstance(LoggingUtils.<T>getHandlerClass(handlerClassName));
41+
}
42+
43+
public static void exitWithError(String message) {
44+
System.err.println(message);
45+
System.exit(ERROR_EXIT_CODE);
46+
}
47+
48+
public static LogRecord cloneRecordWithoutException(LogRecord record) {
49+
LogRecord newRecord = new LogRecord(record.getLevel(), record.getMessage());
50+
51+
newRecord.setLoggerName(record.getLoggerName());
52+
newRecord.setMillis(record.getMillis());
53+
newRecord.setParameters(record.getParameters());
54+
newRecord.setResourceBundle(record.getResourceBundle());
55+
newRecord.setResourceBundleName(record.getResourceBundleName());
56+
newRecord.setSequenceNumber(record.getSequenceNumber());
57+
newRecord.setSourceClassName(record.getSourceClassName());
58+
newRecord.setSourceMethodName(record.getSourceMethodName());
59+
newRecord.setThreadID(record.getThreadID());
60+
// Skip thrown
61+
return newRecord;
62+
}
63+
64+
public static void printLogProperties(Properties logProps, String prefix) {
65+
if (logProps != null) {
66+
for (String propName : logProps.stringPropertyNames()) {
67+
System.err.println(prefix + propName + '=' + logProps.getProperty(propName));
68+
}
69+
}
70+
}
71+
72+
}

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.Properties;
10-
import java.util.logging.ConsoleHandler;
1110
import java.util.logging.Formatter;
1211
import java.util.logging.Handler;
1312
import java.util.logging.Level;
@@ -16,7 +15,6 @@
1615
import java.util.logging.MemoryHandler;
1716

1817
import oracle.weblogic.deploy.util.WLSDeployContext;
19-
import oracle.weblogic.deploy.util.WLSDeployExit;
2018
import oracle.weblogic.deploy.util.WebLogicDeployToolingVersion;
2119

2220

@@ -111,16 +109,16 @@ public synchronized void logEnd(WLSDeployContext modelContext) {
111109
}
112110

113111
/**
114-
* The WLSDeployLoggingConfig will call this method to add to the logging.properties files.
115-
* If the logging.properties already contains the property, the property in this list will be ignored.
112+
* The WLSDeployLoggingConfig will call this method to add the SummaryHandler properties to the logging.properties
113+
* files. If the logging.properties already contains the property, the property in this list will be ignored.
116114
*
117115
* @return properties to set in logging.properties
118116
*/
119117
public static Properties getHandlerProperties() {
120118
Properties properties = new Properties();
121119
properties.setProperty(LEVEL_PROPERTY, Level.INFO.getName());
122-
properties.setProperty(TARGET_PROPERTY, WLSDeployLoggingConsoleHandler.class.getName());
123-
properties.setProperty(FORMATTER_PROPERTY, WLSDeployLogFormatter.class.getName());
120+
properties.setProperty(TARGET_PROPERTY, WLSDeployLoggingConfig.getStdoutHandler());
121+
properties.setProperty(FORMATTER_PROPERTY, WLSDeployConsoleFormatter.class.getName());
124122
return properties;
125123
}
126124

@@ -207,7 +205,9 @@ public synchronized void publish(LogRecord record) {
207205
super.publish(record);
208206
}
209207
}
210-
208+
public synchronized void push() {
209+
super.push();
210+
}
211211
int getTotalRecords() {
212212
return totalRecords;
213213
}
@@ -231,19 +231,12 @@ private int getSize(String propSize) {
231231
return handlerSize;
232232
}
233233

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-
}
234+
private Handler getConsoleHandler() {
235+
Handler handler = LoggingUtils.getHandlerInstance(WLSDeployLoggingConfig.getStdoutHandler());
236+
handler.setFilter(null);
243237
return handler;
244238
}
245239

246-
247240
private LogRecord getLogRecord(String msg, Object... params) {
248241
LogRecord record = new LogRecord(Level.INFO, msg);
249242
record.setLoggerName(LOGGER.getName());
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
*/
5+
package oracle.weblogic.deploy.logging;
6+
7+
import java.util.logging.Filter;
8+
import java.util.logging.Level;
9+
import java.util.logging.LogRecord;
10+
11+
/**
12+
* This Class queries the information in the LogRecord to determine if it can be written to the OutputStream
13+
* associated with Error log record types.
14+
*/
15+
public class WLSDeployConsoleErrorFilter implements Filter {
16+
17+
@Override
18+
public boolean isLoggable(LogRecord record) {
19+
boolean stdErr = false;
20+
int level = record.getLevel() == null ? 0 : record.getLevel().intValue();
21+
if (level == Level.WARNING.intValue() || level == Level.SEVERE.intValue()) {
22+
stdErr = true;
23+
}
24+
return stdErr;
25+
}
26+
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
*/
5+
package oracle.weblogic.deploy.logging;
6+
7+
import java.util.logging.Formatter;
8+
import java.util.logging.LogRecord;
9+
10+
/**
11+
* This class removes the Exception record from the LogRecord before formatting so that a
12+
* stack trace will not print to the Console. The WLSDeployLogFormatter is by default called to
13+
* format the LogRecord into a localized String. To select a different formatter, inject
14+
* that instance into the set formatter of this Instance.
15+
*/
16+
public class WLSDeployConsoleFormatter extends Formatter {
17+
18+
// Default Formatter if another is not injected
19+
private Formatter formatter = new WLSDeployLogFormatter();
20+
21+
public String format(LogRecord logRecord) {
22+
LogRecord cloned = LoggingUtils.cloneRecordWithoutException(logRecord);
23+
return formatter.format(cloned);
24+
}
25+
26+
public void setFormatter(Formatter formatter) {
27+
if (formatter != null) {
28+
this.formatter = formatter;
29+
}
30+
}
31+
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
*/
5+
package oracle.weblogic.deploy.logging;
6+
7+
import java.util.logging.Filter;
8+
import java.util.logging.Level;
9+
import java.util.logging.LogRecord;
10+
11+
/**
12+
* This Class queries the information in the LogRecord to determine if it can be written to the OutputStream
13+
* associated with standard console log record types.
14+
*/
15+
public class WLSDeployConsoleOutFilter implements Filter {
16+
17+
@Override
18+
public boolean isLoggable(LogRecord record) {
19+
boolean stdOut = true;
20+
int level = record.getLevel() == null ? 0 : record.getLevel().intValue();
21+
if (level == Level.WARNING.intValue() || level == Level.SEVERE.intValue()) {
22+
stdOut = false;
23+
}
24+
return stdOut;
25+
}
26+
27+
}

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88

99
import java.lang.reflect.Method;
1010
import java.text.MessageFormat;
11-
import java.util.*;
12-
import java.util.logging.Handler;
11+
import java.util.Arrays;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.Properties;
15+
import java.util.HashSet;
16+
import java.util.Map;
17+
import java.util.Set;
1318

1419
/**
1520
* Use this class to add custom handlers to the root WLSDEPLOY logger. The WLSDeployLoggingConfig
@@ -21,10 +26,10 @@
2126
* WLSDEPLOY_LOG_HANDLERS_ENV_VARIABLE. The property in the logging.properties file takes precedence
2227
* over the environment variable.
2328
*
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
29+
* <p>This class will check each handler in the list to see if it has implemented the WLSDEPLOY_HANDLER_METHOD.
30+
* This method returns a handler's properties. Any property that already exists in the logging.properties
31+
* will be discarded - as logging.properties takes precedence. Any property that does not start with the handler
32+
* class name will be rejected. This class' ONLY purpose is to dynamically add a handler and/or its properties to the
2833
* WLSDEPLOY root logger.
2934
*
3035
* <p>The WLSDEPLOY logger is instantiated by WLSDeployLoggingConfig (and thus, the handlers are
@@ -75,25 +80,12 @@ private static Set<String> findExtraHandlers(Properties logProps) {
7580
}
7681

7782
private static Class<?> getHandlerClass(String handlerName) {
78-
String message = null;
79-
Class<?> handler = null;
80-
try {
81-
handler = Class.forName(handlerName);
82-
if (!Handler.class.isAssignableFrom(handler)) {
83-
message = MessageFormat.format("Class {0} is not a Handler", handlerName);
84-
}
85-
} catch(ClassNotFoundException cnf) {
86-
message = MessageFormat.format("Unable to find handler class {0} so skipping logging configuration",
87-
handlerName);
88-
}
89-
if (message != null) {
90-
System.err.println(message);
91-
System.exit(ERROR_EXIT_CODE);
92-
}
93-
return handler;
83+
return LoggingUtils.getHandlerClass(handlerName);
9484
}
9585

9686
private static void addHandlerProperties(Properties logProps, Class<?> clazz) {
87+
// Uncomment to debug the properties
88+
// LoggingUtils.printLogProperties(logProps, "Before: ");
9789
Properties props = null;
9890
// a forEach would be good here!
9991
String clazzName = clazz.getName();
@@ -125,6 +117,8 @@ private static void addHandlerProperties(Properties logProps, Class<?> clazz) {
125117
}
126118
}
127119
}
120+
// Uncomment to debug the log properties
121+
// LoggingUtils.printLogProperties(logProps, "After: ");
128122
}
129123

130124
}

0 commit comments

Comments
 (0)