Browse Source

Changed over to logback from log4j. Added new minimum logging configurations. Remove log4j configurations and references.

master
Drew Short 11 years ago
parent
commit
8500f0559a
  1. 21
      pom.xml
  2. 34
      src/includes/log4j.properties
  3. 73
      src/includes/logback.xml
  4. 97
      src/main/java/com/sothr/imagetools/AppConfig.java
  5. 23
      src/main/resources/logback-minimum-config.xml
  6. 23
      src/main/scala/com/sothr/imagetools/image/ImageService.scala
  7. 59
      src/test/resources/logback-minimum-config.xml

21
pom.xml

@ -33,7 +33,7 @@
<jdk.version>1.7</jdk.version> <jdk.version>1.7</jdk.version>
<lib.junit.version>3.8.1</lib.junit.version> <lib.junit.version>3.8.1</lib.junit.version>
<lib.scalatest.version>2.0</lib.scalatest.version> <lib.scalatest.version>2.0</lib.scalatest.version>
<lib.log4j.version>1.2.17</lib.log4j.version>
<lib.logback.version>1.0.13</lib.logback.version>
<lib.slf4j.version>1.7.5</lib.slf4j.version> <lib.slf4j.version>1.7.5</lib.slf4j.version>
<lib.grizzled-slf4j.version>1.0.1</lib.grizzled-slf4j.version> <lib.grizzled-slf4j.version>1.0.1</lib.grizzled-slf4j.version>
<lib.scala-library.version>2.10.3</lib.scala-library.version> <lib.scala-library.version>2.10.3</lib.scala-library.version>
@ -60,18 +60,23 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${lib.log4j.version}</version>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${lib.logback.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${lib.slf4j.version}</version>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${lib.logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>${lib.logback.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<artifactId>slf4j-api</artifactId>
<version>${lib.slf4j.version}</version> <version>${lib.slf4j.version}</version>
</dependency> </dependency>
<dependency> <dependency>

34
src/includes/log4j.properties

@ -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

73
src/includes/logback.xml

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="C" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- Sorry Windows Users -->
<withJansi>false</withJansi>
<pattern>[%-5level [%c{16}] - %message%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
<appender name="DL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
<File>ImageTools.debug</File>
<encoder>
<withJansi>false</withJansi>
<pattern>[%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>1</maxIndex>
<FileNamePattern>ImageTools.debug.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>5MB</MaxFileSize>
</triggeringPolicy>
</appender>
<appender name="IL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
<File>ImageTools.info</File>
<encoder>
<withJansi>false</withJansi>
<pattern>[%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>1</maxIndex>
<FileNamePattern>ImageTools.info.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>500KB</MaxFileSize>
</triggeringPolicy>
</appender>
<appender name="EL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
<File>ImageTools.err</File>
<encoder>
<withJansi>false</withJansi>
<pattern>[%.16thread] [%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>1</maxIndex>
<FileNamePattern>ImageTools.err.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>500KB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="C"/>
<appender-ref ref="DL"/>
<appender-ref ref="IL"/>
<appender-ref ref="EL"/>
</root>
</configuration>

97
src/main/java/com/sothr/imagetools/AppConfig.java

@ -1,13 +1,18 @@
package com.sothr.imagetools; package com.sothr.imagetools;
import com.sothr.imagetools.util.ResourceLoader;
import com.sothr.imagetools.util.PropertiesService; import com.sothr.imagetools.util.PropertiesService;
import com.sothr.imagetools.util.PropertiesEnum; import com.sothr.imagetools.util.PropertiesEnum;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.BasicConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.io.File;
import java.util.Properties; import java.util.Properties;
@ -17,7 +22,7 @@ public class AppConfig {
public static CacheManager cacheManager; public static CacheManager cacheManager;
//Logging defaults //Logging defaults
private static final String LOGSETTINGSFILE = "./log4j.properties";
private static final String LOGSETTINGSFILE = "./logback.xml";
private static Boolean configuredLogging = false; private static Boolean configuredLogging = false;
//Properties defaults //Properties defaults
@ -29,78 +34,46 @@ public class AppConfig {
private static Boolean configuredCache = false; private static Boolean configuredCache = false;
public static void configureApp() { public static void configureApp() {
//configSimpleLogging();
if (!configuredLogging) {
configBasicLogging();
logger = (Logger)LoggerFactory.getLogger(AppConfig.class);
loadProperties(); loadProperties();
resetBasicLogging();
} else {
loadProperties();
}
configLogging(); configLogging();
configCache(); 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) { public static void configLogging(String location) {
//Logging Config //Logging Config
//remove previous configuration if it exists //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); File file = new File(location);
Boolean fromFile = false; Boolean fromFile = false;
if (file.exists()) { if (file.exists()) {
fromFile = true; fromFile = true;
PropertyConfigurator.configure(location);
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 { } 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"; String message = fromFile ? "From File" : "From Defaults";
logger.info(String.format("Configured Logger %s", message)); logger.info(String.format("Configured Logger %s", message));
logger.info("Detected Version: %s of Image Tools".format(PropertiesService.getVersion().toString())); logger.info("Detected Version: %s of Image Tools".format(PropertiesService.getVersion().toString()));

23
src/main/resources/logback-minimum-config.xml

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="EL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>ImageTools.err</File>
<encoder>
<withJansi>false</withJansi>
<pattern>[%date{HH:mm:ss}] %-5level [%c{16}] - %message%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>5</maxIndex>
<FileNamePattern>ImageTools.err.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>500KB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="ERROR">
<appender-ref ref="EL"/>
</root>
</configuration>

23
src/main/scala/com/sothr/imagetools/image/ImageService.scala

@ -14,11 +14,27 @@ object ImageService extends Logging {
val imageCache = AppConfig.cacheManager.getCache("images") 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 = { def getImage(file:File):Image = {
try { try {
if (imageCache.isKeyInCache(file.getAbsolutePath)) {
val image = lookupImage(file)
if (image != null) {
debug(s"${file.getAbsolutePath} was already processed") debug(s"${file.getAbsolutePath} was already processed")
return imageCache.get(file.getAbsolutePath).getObjectValue.asInstanceOf[Image]
return image
} else { } else {
val bufferedImage = ImageIO.read(file) val bufferedImage = ImageIO.read(file)
val thumbnailPath = getThumbnailPath(bufferedImage, file) val thumbnailPath = getThumbnailPath(bufferedImage, file)
@ -26,8 +42,7 @@ object ImageService extends Logging {
val imageSize = { (bufferedImage.getWidth, bufferedImage.getHeight) } val imageSize = { (bufferedImage.getWidth, bufferedImage.getHeight) }
val image = new Image(file.getAbsolutePath, thumbnailPath, imageSize, hashes) val image = new Image(file.getAbsolutePath, thumbnailPath, imageSize, hashes)
debug(s"Created image: $image") debug(s"Created image: $image")
imageCache.put(new Element(file.getAbsolutePath, image))
return image
return saveImage(image)
} }
} catch { } catch {
case ioe:IOException => error(s"Error processing ${file.getAbsolutePath}", ioe) case ioe:IOException => error(s"Error processing ${file.getAbsolutePath}", ioe)

59
src/test/resources/logback-minimum-config.xml

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="DL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>ImageTools.debug</File>
<encoder>
<withJansi>false</withJansi>
<pattern>[%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>1</maxIndex>
<FileNamePattern>ImageTools.debug.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>5MB</MaxFileSize>
</triggeringPolicy>
</appender>
<appender name="IL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>ImageTools.info</File>
<encoder>
<withJansi>false</withJansi>
<pattern>[%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>1</maxIndex>
<FileNamePattern>ImageTools.info.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>500KB</MaxFileSize>
</triggeringPolicy>
</appender>
<appender name="EL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>ImageTools.err</File>
<encoder>
<withJansi>false</withJansi>
<pattern>[%.16thread] [%date{yy-MM-dd HH:mm:ss}] %-5level [%c{16}] - %message%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>1</maxIndex>
<FileNamePattern>ImageTools.err.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>500KB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="DL"/>
<appender-ref ref="IL"/>
<appender-ref ref="EL"/>
</root>
</configuration>
Loading…
Cancel
Save