Skip to content
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle Corporation and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Oracle Corporation and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.logging;
Expand All @@ -13,7 +13,7 @@
* format the LogRecord into a localized String. To select a different formatter, inject
* that instance into the set formatter of this Instance.
*/
public class WLSDeployConsoleFormatter extends Formatter {
public class ConsoleFormatter extends Formatter {

// Default Formatter if another is not injected
private Formatter formatter = new WLSDeployLogFormatter();
Expand All @@ -23,10 +23,10 @@ public String format(LogRecord logRecord) {
return formatter.format(cloned);
}

@SuppressWarnings("unused")
public void setFormatter(Formatter formatter) {
if (formatter != null) {
this.formatter = formatter;
}
}

}
}
76 changes: 25 additions & 51 deletions core/src/main/java/oracle/weblogic/deploy/logging/LoggingUtils.java
Original file line number Diff line number Diff line change
@@ -1,64 +1,39 @@
/*
* Copyright (c) 2019, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Oracle Corporation and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.logging;

import java.text.MessageFormat;
import java.util.Properties;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import static oracle.weblogic.deploy.logging.WLSDeployLoggingConfig.ERROR_EXIT_CODE;

/**
* Utility class with methods used by the logging framework.
*/
public class LoggingUtils {

public static <T extends Handler> Class<T> getHandlerClass(String handlerName) {
Class<T> handler = null;
try {
Class<?> checkClass = Class.forName(handlerName);
@SuppressWarnings("unchecked")
Class<T> castHandler = (Class<T>)checkClass.asSubclass(Class.forName(handlerName));
handler = castHandler;
} catch(ClassNotFoundException | ClassCastException cnf) {
exitWithError(
MessageFormat.format("Unable to find handler class {0} so skipping logging configuration",
handlerName));
}
return handler;
}

public static <T extends Handler> T getHandlerInstance(Class<T> handlerClass) {
T handler = null;
try {
handler = handlerClass.newInstance();
} catch (InstantiationException | IllegalAccessException e){
exitWithError(MessageFormat.format("Unable to instantiate Handler for Class {0}", handlerClass));
}
return handler;
}

public static <T extends Handler> T getHandlerInstance(String handlerClassName) {
return getHandlerInstance(LoggingUtils.<T>getHandlerClass(handlerClassName));
}

public static void exitWithError(String message) {
System.err.println(message);
System.exit(ERROR_EXIT_CODE);
private LoggingUtils() {
// hide the constructor
}

public static LogRecord cloneRecordWithoutException(LogRecord record) {
LogRecord newRecord = new LogRecord(record.getLevel(), record.getMessage());

newRecord.setLoggerName(record.getLoggerName());
newRecord.setMillis(record.getMillis());
newRecord.setParameters(record.getParameters());
newRecord.setResourceBundle(record.getResourceBundle());
newRecord.setResourceBundleName(record.getResourceBundleName());
newRecord.setSequenceNumber(record.getSequenceNumber());
newRecord.setSourceClassName(record.getSourceClassName());
newRecord.setSourceMethodName(record.getSourceMethodName());
newRecord.setThreadID(record.getThreadID());
/**
* Make a copy of a log record without the exception.
*
* @param logRecord the log record to copy
* @return the cloned log record without the exception
*/
public static LogRecord cloneRecordWithoutException(LogRecord logRecord) {
LogRecord newRecord = new LogRecord(logRecord.getLevel(), logRecord.getMessage());

newRecord.setLoggerName(logRecord.getLoggerName());
newRecord.setMillis(logRecord.getMillis());
newRecord.setParameters(logRecord.getParameters());
newRecord.setResourceBundle(logRecord.getResourceBundle());
newRecord.setResourceBundleName(logRecord.getResourceBundleName());
newRecord.setSequenceNumber(logRecord.getSequenceNumber());
newRecord.setSourceClassName(logRecord.getSourceClassName());
newRecord.setSourceMethodName(logRecord.getSourceMethodName());
newRecord.setThreadID(logRecord.getThreadID());
// Skip thrown
return newRecord;
}
Expand All @@ -70,5 +45,4 @@ public static void printLogProperties(Properties logProps, String prefix) {
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.logging;
Expand Down Expand Up @@ -341,6 +341,7 @@ public boolean isFinestEnabled() {
*
* @return whether or not the INFO level is enabled
*/
@SuppressWarnings("unused")
public boolean isInfoEnabled() {
return logger.isLoggable(Level.INFO);
}
Expand Down Expand Up @@ -369,6 +370,7 @@ public boolean isSevereEnabled() {
*
* @return whether or not the WARNING level is enabled
*/
@SuppressWarnings("unused")
public boolean isWarningEnabled() {
return logger.isLoggable(Level.WARNING);
}
Expand Down Expand Up @@ -535,6 +537,7 @@ public void warning(String msg, Throwable thrown) {
*
* @return List of Logger from the Log Manager
*/
@SuppressWarnings("unused")
public static List<Logger> getLoggers() {
LogManager manager = LogManager.getLogManager();
Enumeration<String> e = manager.getLoggerNames();
Expand Down Expand Up @@ -577,18 +580,18 @@ CallerDetails inferCaller() {
}

private LogRecord getLogRecord(Level level, CallerDetails details, String msg, Throwable error, Object... params) {
LogRecord record = new LogRecord(level, msg);
record.setLoggerName(this.getName());
record.setMillis(System.currentTimeMillis());
LogRecord logRecord = new LogRecord(level, msg);
logRecord.setLoggerName(this.getName());
logRecord.setMillis(System.currentTimeMillis());
if (params != null && params.length != 0) {
record.setParameters(params);
logRecord.setParameters(params);
}
record.setResourceBundle(logger.getResourceBundle());
record.setSourceClassName(details.clazz);
record.setSourceMethodName(details.method);
record.setThreadID((int)Thread.currentThread().getId());
record.setThrown(error);
return record;
logRecord.setResourceBundle(logger.getResourceBundle());
logRecord.setSourceClassName(details.clazz);
logRecord.setSourceMethodName(details.method);
logRecord.setThreadID((int)Thread.currentThread().getId());
logRecord.setThrown(error);
return logRecord;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle Corporation and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Oracle Corporation and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.logging;
Expand All @@ -12,16 +12,15 @@
* This Class queries the information in the LogRecord to determine if it can be written to the OutputStream
* associated with Error log record types.
*/
public class WLSDeployConsoleErrorFilter implements Filter {

@SuppressWarnings("unused")
public class StderrFilter implements Filter {
@Override
public boolean isLoggable(LogRecord record) {
public boolean isLoggable(LogRecord logRecord) {
boolean stdErr = false;
int level = record.getLevel() == null ? 0 : record.getLevel().intValue();
int level = logRecord.getLevel() == null ? 0 : logRecord.getLevel().intValue();
if (level == Level.WARNING.intValue() || level == Level.SEVERE.intValue()) {
stdErr = true;
}
return stdErr;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle Corporation and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Oracle Corporation and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.logging;
Expand All @@ -8,22 +8,19 @@
import java.util.logging.StreamHandler;

/**
* This Class extends the StreamHandler to write log records to STDERR. The "wlsdeploy" Logger
* is configured with an instance of this Handler class and an instance of Class @WLSDeployStdoutHandler@
* to log records to the STDOUT and STDERR output streams.
* <p>
* Attach a logger or filter to this Handler to direct log records to the STDERR output stream
* This Class extends the StreamHandler to write log records to STDERR.
*/
public class WLSDeployLoggingStderrHandler extends StreamHandler {
@SuppressWarnings("unused")
public class StderrHandler extends StreamHandler {

public WLSDeployLoggingStderrHandler() {
public StderrHandler() {
super();
setOutputStream(System.err);
}

@Override
public void publish(LogRecord record) {
super.publish(record);
public synchronized void publish(LogRecord logRecord) {
super.publish(logRecord);
flush();
}

Expand All @@ -33,8 +30,7 @@ public void publish(LogRecord record) {
* close <tt>System.err</tt>.
*/
@Override
public void close() {
public synchronized void close() {
flush();
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle Corporation and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Oracle Corporation and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.logging;
Expand All @@ -12,16 +12,14 @@
* This Class queries the information in the LogRecord to determine if it can be written to the OutputStream
* associated with standard console log record types.
*/
public class WLSDeployConsoleOutFilter implements Filter {

public class StdoutFilter implements Filter {
@Override
public boolean isLoggable(LogRecord record) {
public boolean isLoggable(LogRecord logRecord) {
boolean stdOut = true;
int level = record.getLevel() == null ? 0 : record.getLevel().intValue();
int level = logRecord.getLevel() == null ? 0 : logRecord.getLevel().intValue();
if (level == Level.WARNING.intValue() || level == Level.SEVERE.intValue()) {
stdOut = false;
}
return stdOut;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2019, 2022, Oracle Corporation and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.logging;

import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;

/**
* This Class extends the StreamHandler to write log records to STDOUT.
*/
@SuppressWarnings("unused")
public class StdoutHandler extends StreamHandler {

public StdoutHandler() {
super();
setOutputStream(System.out);
}

@Override
public synchronized void publish(LogRecord logRecord) {
super.publish(logRecord);
flush();
}

/**
* Override <tt>StreamHandler.close</tt> to do a flush but not
* to close the output stream. That is, we do <b>not</b>
* close <tt>System.out</tt>.
*/
@Override
public synchronized void close() {
flush();
}
}
Loading