Skip to content

Commit

Permalink
Merge pull request #141 from Gaboso/master
Browse files Browse the repository at this point in the history
Refactoring in Logs
  • Loading branch information
pbrant committed Jul 7, 2018
2 parents 90b5ec3 + ed6eff6 commit 7759d79
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 152 deletions.
Expand Up @@ -21,31 +21,29 @@
*/
package org.xhtmlrenderer.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.Handler;
import java.util.logging.Formatter;
import java.util.Properties;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Enumeration;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* An {@link XRLogger} interface that uses <code>java.util.logging</code>.
*/
public class JDKXRLogger implements XRLogger {

private static boolean initPending = true;

/** {@inheritdoc} */

/**
* {@inheritdoc}
*/
public void log(String where, Level level, String msg) {
if (initPending) {
init();
Expand All @@ -54,7 +52,9 @@ public void log(String where, Level level, String msg) {
getLogger(where).log(level, msg);
}

/** {@inheritdoc} */
/**
* {@inheritdoc}
*/
public void log(String where, Level level, String msg, Throwable th) {
if (initPending) {
init();
Expand All @@ -63,7 +63,9 @@ public void log(String where, Level level, String msg, Throwable th) {
getLogger(where).log(level, msg, th);
}

/** {@inheritdoc} */
/**
* {@inheritdoc}
*/
public void setLevel(String logger, Level level) {
getLogger(logger).setLevel(level);
}
Expand All @@ -90,7 +92,7 @@ private static void init() {
try {
Properties props = retrieveLoggingProperties();

if(!XRLog.isLoggingEnabled()) {
if (!XRLog.isLoggingEnabled()) {
Configuration.setConfigLogger(Logger.getLogger(XRLog.CONFIG));
return;
}
Expand All @@ -99,8 +101,6 @@ private static void init() {
Configuration.setConfigLogger(Logger.getLogger(XRLog.CONFIG));
} catch (SecurityException e) {
// may happen in a sandbox environment
} catch (FileNotFoundException e) {
throw new XRRuntimeException("Could not initialize logs. " + e.getLocalizedMessage(), e);
} catch (IOException e) {
throw new XRRuntimeException("Could not initialize logs. " + e.getLocalizedMessage(), e);
}
Expand All @@ -123,19 +123,20 @@ private static Properties retrieveLoggingProperties() {
}

private static void initializeJDKLogManager(final Properties fsLoggingProperties) throws IOException {
final List loggers = retrieveLoggers();
final List<Logger> loggers = retrieveLoggers();

configureLoggerHandlerForwarding(fsLoggingProperties, loggers);

// load our properties into our log manager
Enumeration keys = fsLoggingProperties.keys();
Map handlers = new HashMap();
Map handlerFormatterMap = new HashMap();
Map<String, Handler> handlers = new HashMap<String, Handler>();
Map<String, String> handlerFormatterMap = new HashMap<String, String>();

while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String prop = fsLoggingProperties.getProperty(key);
if (key.endsWith("level")) {
configureLogLevel(key.substring(0, key.lastIndexOf(".")), prop);
configureLogLevel(key.substring(0, key.lastIndexOf('.')), prop);
} else if (key.endsWith("handlers")) {
handlers = configureLogHandlers(loggers, prop);
} else if (key.endsWith("formatter")) {
Expand All @@ -146,30 +147,29 @@ private static void initializeJDKLogManager(final Properties fsLoggingProperties

// formatters apply to a specific handler we have initialized previously,
// hence we need to wait until we've parsed the handler class
for (Iterator it = handlerFormatterMap.keySet().iterator(); it.hasNext();) {
String handlerClassName = (String) it.next();
String formatterClassName = (String) handlerFormatterMap.get(handlerClassName);
for (String handlerClassName : handlerFormatterMap.keySet()) {
String formatterClassName = handlerFormatterMap.get(handlerClassName);
assignFormatter(handlers, handlerClassName, formatterClassName);
}
}

private static void configureLoggerHandlerForwarding(Properties fsLoggingProperties, List loggers) {
private static void configureLoggerHandlerForwarding(Properties fsLoggingProperties, List<Logger> loggers) {
String val = fsLoggingProperties.getProperty("use-parent-handler");

boolean flag = val == null ? false : Boolean.valueOf(val).booleanValue();
for (Iterator it = loggers.iterator(); it.hasNext();) {
Logger logger = (Logger) it.next();
boolean flag = val != null && Boolean.valueOf(val);
for (Logger logger : loggers) {
logger.setUseParentHandlers(flag);
}
}

private static void assignFormatter(Map handlers, String handlerClassName, String formatterClassName) {
Handler handler = (Handler) handlers.get(handlerClassName);
private static void assignFormatter(Map<String, Handler> handlers, String handlerClassName, String formatterClassName) {
Handler handler = handlers.get(handlerClassName);

if (handler != null) {
try {
Class fclass = Class.forName(formatterClassName);
Formatter f = (Formatter) fclass.newInstance();
handler.setFormatter(f);
Formatter formatter = (Formatter) fclass.newInstance();
handler.setFormatter(formatter);
} catch (ClassNotFoundException e) {
throw new XRRuntimeException("Could not initialize logging properties; " +
"Formatter class not found: " + formatterClassName);
Expand All @@ -187,32 +187,31 @@ private static void assignFormatter(Map handlers, String handlerClassName, Strin
* Returns a List of all Logger instances used by Flying Saucer from the JDK LogManager; these will
* be automatically created if they aren't already available.
*/
private static List retrieveLoggers() {
List loggerNames = XRLog.listRegisteredLoggers();
List loggers = new ArrayList(loggerNames.size());
Iterator it = loggerNames.iterator();
while (it.hasNext()) {
final String ln = (String) it.next();
loggers.add(Logger.getLogger(ln));
private static List<Logger> retrieveLoggers() {
List<String> loggerNames = XRLog.listRegisteredLoggers();
List<Logger> loggers = new ArrayList<Logger>(loggerNames.size());

for (String loggerName : loggerNames) {
loggers.add(Logger.getLogger(loggerName));
}

return loggers;
}

/**
* For each logger provided, assigns the logger an instance of the named log output handlers. Will attempt
* to instantiate each handler; any which can't be instantiated will cause the method to throw a RuntimeException.
*
* @param loggers List of Logger instances.
* @param loggers List of Logger instances.
* @param handlerClassList A space-separated string (following the configuration convention for JDK logging
* configuration files, for handlers) of FQN of log handlers.
*
* configuration files, for handlers) of FQN of log handlers.
* @return Map of handler class names to handler instances.
*/
private static Map configureLogHandlers(List loggers, final String handlerClassList) {
private static Map<String, Handler> configureLogHandlers(List<Logger> loggers, final String handlerClassList) {
final String[] names = handlerClassList.split(" ");
final Map handlers = new HashMap(names.length);
for (int i = 0; i < names.length; i++) {
final String name = names[i];
final Map<String, Handler> handlers = new HashMap<String, Handler>(names.length);

for (final String name : names) {
try {
Class handlerClass = Class.forName(name);
Handler handler = (Handler) handlerClass.newInstance();
Expand All @@ -232,10 +231,8 @@ private static Map configureLogHandlers(List loggers, final String handlerClassL
}

// now assign each handler to each FS logger
for (Iterator iterator = loggers.iterator(); iterator.hasNext();) {
Logger logger = (Logger) iterator.next();
for (Iterator ith = handlers.values().iterator(); ith.hasNext();) {
Handler handler = (Handler) ith.next();
for (Logger logger : loggers) {
for (Handler handler : handlers.values()) {
logger.addHandler(handler);
}
}
Expand Down
Expand Up @@ -20,24 +20,26 @@
*/
package org.xhtmlrenderer.util;

import java.util.logging.*;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
* Utility class for working with java.logging Logger classes
*
* @author Patrick Wright
* @author Patrick Wright
*/
public class LoggerUtil {

/**
* Instantiate a Logger for debug messages for a given class.
*
* @param cls PARAM
* @return The debugLogger value
* @param cls PARAM
* @return The debugLogger value
*/
public static Logger getDebugLogger( Class cls ) {
Logger l = Logger.getLogger( cls.getName() );
l.setLevel( Level.ALL );
public static Logger getDebugLogger(Class cls) {
Logger l = Logger.getLogger(cls.getName());
l.setLevel(Level.ALL);
return l;
}

Expand Down
46 changes: 24 additions & 22 deletions flying-saucer-core/src/main/java/org/xhtmlrenderer/util/XRLog.java
Expand Up @@ -33,40 +33,41 @@
* @author empty
*/
public class XRLog {
private static final List LOGGER_NAMES = new ArrayList(20);
public final static String CONFIG = registerLoggerByName("org.xhtmlrenderer.config");
public final static String EXCEPTION = registerLoggerByName("org.xhtmlrenderer.exception");
public final static String GENERAL = registerLoggerByName("org.xhtmlrenderer.general");
public final static String INIT = registerLoggerByName("org.xhtmlrenderer.init");
public final static String JUNIT = registerLoggerByName("org.xhtmlrenderer.junit");
public final static String LOAD = registerLoggerByName("org.xhtmlrenderer.load");
public final static String MATCH = registerLoggerByName("org.xhtmlrenderer.match");
public final static String CASCADE = registerLoggerByName("org.xhtmlrenderer.cascade");
public final static String XML_ENTITIES = registerLoggerByName("org.xhtmlrenderer.load.xml-entities");
public final static String CSS_PARSE = registerLoggerByName("org.xhtmlrenderer.css-parse");
public final static String LAYOUT = registerLoggerByName("org.xhtmlrenderer.layout");
public final static String RENDER = registerLoggerByName("org.xhtmlrenderer.render");

private static String registerLoggerByName(final String loggerName) {
LOGGER_NAMES.add(loggerName);
return loggerName;
}
private static final List<String> LOGGER_NAMES = new ArrayList<String>(20);
public static final String CONFIG = registerLoggerByName("org.xhtmlrenderer.config");
public static final String EXCEPTION = registerLoggerByName("org.xhtmlrenderer.exception");
public static final String GENERAL = registerLoggerByName("org.xhtmlrenderer.general");
public static final String INIT = registerLoggerByName("org.xhtmlrenderer.init");
public static final String JUNIT = registerLoggerByName("org.xhtmlrenderer.junit");
public static final String LOAD = registerLoggerByName("org.xhtmlrenderer.load");
public static final String MATCH = registerLoggerByName("org.xhtmlrenderer.match");
public static final String CASCADE = registerLoggerByName("org.xhtmlrenderer.cascade");
public static final String XML_ENTITIES = registerLoggerByName("org.xhtmlrenderer.load.xml-entities");
public static final String CSS_PARSE = registerLoggerByName("org.xhtmlrenderer.css-parse");
public static final String LAYOUT = registerLoggerByName("org.xhtmlrenderer.layout");
public static final String RENDER = registerLoggerByName("org.xhtmlrenderer.render");

private static boolean initPending = true;
private static XRLogger loggerImpl;

private static boolean loggingEnabled = true;

private static String registerLoggerByName(final String loggerName) {
LOGGER_NAMES.add(loggerName);
return loggerName;
}

/**
* Returns a list of all loggers that will be accessed by XRLog. Each entry is a String with a logger
* name, which can be used to retrieve the logger using the corresponding Logging API; example name might be
* "org.xhtmlrenderer.config"
*
* @return List of loggers, never null.
*/
public static List listRegisteredLoggers() {
public static List<String> listRegisteredLoggers() {
// defensive copy
return new ArrayList(LOGGER_NAMES);
return new ArrayList<String>(LOGGER_NAMES);
}


Expand Down Expand Up @@ -216,7 +217,7 @@ public static synchronized void log(String where, Level level, String msg, Throw
}
}

public static void main(String args[]) {
public static void main(String[] args) {
try {
XRLog.cascade("Cascade msg");
XRLog.cascade(Level.WARNING, "Cascade msg");
Expand Down Expand Up @@ -264,6 +265,7 @@ public static synchronized void setLevel(String log, Level level) {

/**
* Whether logging is on or off.
*
* @return Returns true if logging is enabled, false if not. Corresponds
* to configuration file property xr.util-logging.loggingEnabled, or to
* value passed to setLoggingEnabled(bool).
Expand All @@ -276,8 +278,8 @@ public static synchronized boolean isLoggingEnabled() {
* Turns logging on or off, without affecting logging configuration.
*
* @param loggingEnabled Flag whether logging is enabled or not;
* if false, all logging calls fail silently. Corresponds
* to configuration file property xr.util-logging.loggingEnabled
* if false, all logging calls fail silently. Corresponds
* to configuration file property xr.util-logging.loggingEnabled
*/
public static synchronized void setLoggingEnabled(boolean loggingEnabled) {
XRLog.loggingEnabled = loggingEnabled;
Expand Down
Expand Up @@ -25,8 +25,11 @@
* An interface whose implementations log Flying Saucer log messages.
*/
public interface XRLogger {
public void log(String where, Level level, String msg);
public void log(String where, Level level, String msg, Throwable th);

public void setLevel(String logger, Level level);
}

void log(String where, Level level, String msg);

void log(String where, Level level, String msg, Throwable th);

void setLevel(String logger, Level level);

}

0 comments on commit 7759d79

Please sign in to comment.