From 8500f0559a42b6494a8c17bd7e6359671a2f54fc Mon Sep 17 00:00:00 2001 From: Drew Short Date: Thu, 6 Feb 2014 15:24:32 -0500 Subject: [PATCH] Changed over to logback from log4j. Added new minimum logging configurations. Remove log4j configurations and references. --- pom.xml | 21 ++-- src/includes/log4j.properties | 34 ------ src/includes/logback.xml | 73 +++++++++++++ .../java/com/sothr/imagetools/AppConfig.java | 101 +++++++----------- src/main/resources/logback-minimum-config.xml | 23 ++++ .../sothr/imagetools/image/ImageService.scala | 23 +++- src/test/resources/logback-minimum-config.xml | 59 ++++++++++ 7 files changed, 224 insertions(+), 110 deletions(-) delete mode 100644 src/includes/log4j.properties create mode 100644 src/includes/logback.xml create mode 100644 src/main/resources/logback-minimum-config.xml create mode 100644 src/test/resources/logback-minimum-config.xml diff --git a/pom.xml b/pom.xml index 37c967a..b87df82 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ 1.7 3.8.1 2.0 - 1.2.17 + 1.0.13 1.7.5 1.0.1 2.10.3 @@ -60,18 +60,23 @@ test - log4j - log4j - ${lib.log4j.version} + ch.qos.logback + logback-core + ${lib.logback.version} - org.slf4j - slf4j-api - ${lib.slf4j.version} + ch.qos.logback + logback-classic + ${lib.logback.version} + + + ch.qos.logback + logback-access + ${lib.logback.version} org.slf4j - slf4j-log4j12 + slf4j-api ${lib.slf4j.version} diff --git a/src/includes/log4j.properties b/src/includes/log4j.properties deleted file mode 100644 index 09e024a..0000000 --- a/src/includes/log4j.properties +++ /dev/null @@ -1,34 +0,0 @@ -log4j.rootLogger=DEBUG, C, DL, IL, EL - -# Console Output -log4j.appender.C=org.apache.log4j.ConsoleAppender -log4j.appender.C.Threshold=INFO -log4j.appender.C.layout=org.apache.log4j.EnhancedPatternLayout -log4j.appender.C.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n - -# Debug Rolling Log -log4j.appender.DL=org.apache.log4j.RollingFileAppender -log4j.appender.DL.Threshold=DEBUG -log4j.appender.DL.File=Image-Tools.debug -log4j.appender.DL.MaxFileSize=500KB -log4j.appender.DL.MaxBackupIndex=1 -log4j.appender.DL.layout=org.apache.log4j.EnhancedPatternLayout -log4j.appender.DL.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n - -# Info Rolling Log -log4j.appender.IL=org.apache.log4j.RollingFileAppender -log4j.appender.IL.Threshold=INFO -log4j.appender.IL.File=Image-Tools.info -log4j.appender.IL.MaxFileSize=100KB -log4j.appender.IL.MaxBackupIndex=1 -log4j.appender.IL.layout=org.apache.log4j.EnhancedPatternLayout -log4j.appender.IL.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n - -# Error Rolling Log -log4j.appender.EL=org.apache.log4j.RollingFileAppender -log4j.appender.EL.Threshold=ERROR -log4j.appender.EL.File=Image-Tools.err -log4j.appender.EL.MaxFileSize=100KB -log4j.appender.EL.MaxBackupIndex=1 -log4j.appender.EL.layout=org.apache.log4j.EnhancedPatternLayout -log4j.appender.EL.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n diff --git a/src/includes/logback.xml b/src/includes/logback.xml new file mode 100644 index 0000000..4a298fd --- /dev/null +++ b/src/includes/logback.xml @@ -0,0 +1,73 @@ + + + + + + false + [%-5level [%c{16}] - %message%n + + + INFO + + + + + ImageTools.debug + + false + [%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n + + + DEBUG + + + 1 + ImageTools.debug.%i + + + 5MB + + + + + ImageTools.info + + false + [%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n + + + INFO + + + 1 + ImageTools.info.%i + + + 500KB + + + + + ImageTools.err + + false + [%.16thread] [%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n + + + ERROR + + + 1 + ImageTools.err.%i + + + 500KB + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/sothr/imagetools/AppConfig.java b/src/main/java/com/sothr/imagetools/AppConfig.java index 84e0dd5..366250d 100644 --- a/src/main/java/com/sothr/imagetools/AppConfig.java +++ b/src/main/java/com/sothr/imagetools/AppConfig.java @@ -1,13 +1,18 @@ package com.sothr.imagetools; +import com.sothr.imagetools.util.ResourceLoader; import com.sothr.imagetools.util.PropertiesService; import com.sothr.imagetools.util.PropertiesEnum; import net.sf.ehcache.CacheManager; -import org.apache.log4j.PropertyConfigurator; -import org.apache.log4j.BasicConfigurator; -import org.slf4j.Logger; + import org.slf4j.LoggerFactory; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.joran.JoranConfigurator; +import ch.qos.logback.core.joran.spi.JoranException; +import ch.qos.logback.core.util.StatusPrinter; + import java.io.File; import java.util.Properties; @@ -17,7 +22,7 @@ public class AppConfig { public static CacheManager cacheManager; //Logging defaults - private static final String LOGSETTINGSFILE = "./log4j.properties"; + private static final String LOGSETTINGSFILE = "./logback.xml"; private static Boolean configuredLogging = false; //Properties defaults @@ -29,78 +34,46 @@ public class AppConfig { private static Boolean configuredCache = false; public static void configureApp() { - //configSimpleLogging(); - if (!configuredLogging) { - configBasicLogging(); - loadProperties(); - resetBasicLogging(); - } else { - loadProperties(); - } + logger = (Logger)LoggerFactory.getLogger(AppConfig.class); + loadProperties(); configLogging(); configCache(); } - public static void configBasicLogging() { - BasicConfigurator.configure(); - logger = LoggerFactory.getLogger(AppConfig.class); - } - - public static void resetBasicLogging() { - logger = null; - BasicConfigurator.resetConfiguration(); - } - public static void configLogging(String location) { //Logging Config //remove previous configuration if it exists - //BasicConfigurator.resetConfiguration(); + Logger rootLogger = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + LoggerContext context = rootLogger.getLoggerContext(); + context.reset(); File file = new File(location); Boolean fromFile = false; if (file.exists()) { - fromFile = true; - PropertyConfigurator.configure(location); + fromFile = true; + try { + JoranConfigurator configurator = new JoranConfigurator(); + configurator.setContext(context); + // Call context.reset() to clear any previous configuration, e.g. default + // configuration. For multi-step configuration, omit calling context.reset(). + context.reset(); + configurator.doConfigure(location); + } catch (JoranException je) { + // StatusPrinter will handle this + } + StatusPrinter.printInCaseOfErrorsOrWarnings(context); } else { - //Simple error logging configuration - Properties defaultProps = new Properties(); - String rootLogger = "DEBUG"; - if (Boolean.valueOf(PropertiesService.get(PropertiesEnum.LogDebug().toString()))) { - //Rolling Debug logger - rootLogger += ", DL"; - defaultProps.setProperty("log4j.appender.DL","org.apache.log4j.RollingFileAppender"); - defaultProps.setProperty("log4j.appender.DL.Threshold","DEBUG"); - defaultProps.setProperty("log4j.appender.DL.File","Image-Tools.debug"); - defaultProps.setProperty("log4j.appender.DL.MaxFileSize","500KB"); - defaultProps.setProperty("log4j.appender.DL.MaxBackupIndex","1"); - defaultProps.setProperty("log4j.appender.DL.layout","org.apache.log4j.EnhancedPatternLayout"); - defaultProps.setProperty("log4j.appender.DL.layout.ConversionPattern","%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n"); - } - if (Boolean.valueOf(PropertiesService.get(PropertiesEnum.LogInfo().toString()))) { - //Rolling Info logger - rootLogger += ", IL"; - defaultProps.setProperty("log4j.appender.IL","org.apache.log4j.RollingFileAppender"); - defaultProps.setProperty("log4j.appender.IL.Threshold","INFO"); - defaultProps.setProperty("log4j.appender.IL.File","Image-Tools.info"); - defaultProps.setProperty("log4j.appender.IL.MaxFileSize","100KB"); - defaultProps.setProperty("log4j.appender.IL.MaxBackupIndex","1"); - defaultProps.setProperty("log4j.appender.IL.layout","org.apache.log4j.EnhancedPatternLayout"); - defaultProps.setProperty("log4j.appender.IL.layout.ConversionPattern","%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n"); - } - if (Boolean.valueOf(PropertiesService.get(PropertiesEnum.LogError().toString()))) { - //Rolling Error logger - rootLogger += ", EL"; - defaultProps.setProperty("log4j.appender.EL","org.apache.log4j.RollingFileAppender"); - defaultProps.setProperty("log4j.appender.EL.Threshold","ERROR"); - defaultProps.setProperty("log4j.appender.EL.File","Image-Tools.err"); - defaultProps.setProperty("log4j.appender.EL.MaxFileSize","100KB"); - defaultProps.setProperty("log4j.appender.EL.MaxBackupIndex","1"); - defaultProps.setProperty("log4j.appender.EL.layout","org.apache.log4j.EnhancedPatternLayout"); - defaultProps.setProperty("log4j.appender.EL.layout.ConversionPattern","%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n"); - } - defaultProps.setProperty("log4j.rootLogger",rootLogger); - PropertyConfigurator.configure(defaultProps); + try { + JoranConfigurator configurator = new JoranConfigurator(); + configurator.setContext(context); + // Call context.reset() to clear any previous configuration, e.g. default + // configuration. For multi-step configuration, omit calling context.reset(). + context.reset(); + configurator.doConfigure(ResourceLoader.get().getResource("logback-minimum-config.xml")); + } catch (JoranException je) { + // StatusPrinter will handle this + } + StatusPrinter.printInCaseOfErrorsOrWarnings(context); } - logger = LoggerFactory.getLogger(AppConfig.class); String message = fromFile ? "From File" : "From Defaults"; logger.info(String.format("Configured Logger %s", message)); logger.info("Detected Version: %s of Image Tools".format(PropertiesService.getVersion().toString())); diff --git a/src/main/resources/logback-minimum-config.xml b/src/main/resources/logback-minimum-config.xml new file mode 100644 index 0000000..ab0887d --- /dev/null +++ b/src/main/resources/logback-minimum-config.xml @@ -0,0 +1,23 @@ + + + + ImageTools.err + + false + [%date{HH:mm:ss}] %-5level [%c{16}] - %message%n + + + ERROR + + + 5 + ImageTools.err.%i + + + 500KB + + + + + + \ No newline at end of file diff --git a/src/main/scala/com/sothr/imagetools/image/ImageService.scala b/src/main/scala/com/sothr/imagetools/image/ImageService.scala index a574360..b7f6d8a 100644 --- a/src/main/scala/com/sothr/imagetools/image/ImageService.scala +++ b/src/main/scala/com/sothr/imagetools/image/ImageService.scala @@ -14,11 +14,27 @@ object ImageService extends Logging { val imageCache = AppConfig.cacheManager.getCache("images") + private def lookupImage(file:File):Image = { + var image:Image = null + //get from memory cache if possible + if (imageCache.isKeyInCache(file.getAbsolutePath)) image = imageCache.get(file.getAbsolutePath).getObjectValue.asInstanceOf[Image] + //get from datastore if possible + image + } + + private def saveImage(image:Image):Image = { + //save to cache + imageCache.put(new Element(image.imagePath, image)) + //save to datastore + image + } + def getImage(file:File):Image = { try { - if (imageCache.isKeyInCache(file.getAbsolutePath)) { + val image = lookupImage(file) + if (image != null) { debug(s"${file.getAbsolutePath} was already processed") - return imageCache.get(file.getAbsolutePath).getObjectValue.asInstanceOf[Image] + return image } else { val bufferedImage = ImageIO.read(file) val thumbnailPath = getThumbnailPath(bufferedImage, file) @@ -26,8 +42,7 @@ object ImageService extends Logging { val imageSize = { (bufferedImage.getWidth, bufferedImage.getHeight) } val image = new Image(file.getAbsolutePath, thumbnailPath, imageSize, hashes) debug(s"Created image: $image") - imageCache.put(new Element(file.getAbsolutePath, image)) - return image + return saveImage(image) } } catch { case ioe:IOException => error(s"Error processing ${file.getAbsolutePath}", ioe) diff --git a/src/test/resources/logback-minimum-config.xml b/src/test/resources/logback-minimum-config.xml new file mode 100644 index 0000000..d4622c7 --- /dev/null +++ b/src/test/resources/logback-minimum-config.xml @@ -0,0 +1,59 @@ + + + + ImageTools.debug + + false + [%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n + + + DEBUG + + + 1 + ImageTools.debug.%i + + + 5MB + + + + ImageTools.info + + false + [%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n + + + INFO + + + 1 + ImageTools.info.%i + + + 500KB + + + + ImageTools.err + + false + [%.16thread] [%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n + + + ERROR + + + 1 + ImageTools.err.%i + + + 500KB + + + + + + + + \ No newline at end of file