Browse Source

Updated logging to be more concise and added a failover for no logging file existing.

master
Drew Short 11 years ago
parent
commit
b8f9b05281
  1. 28
      src/includes/log4j.properties
  2. 38
      src/main/java/com/sothr/imagetools/App.java

28
src/includes/log4j.properties

@ -1,4 +1,24 @@
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n
log4j.rootLogger=DEBUG, C, IL, EL
# Console Output
log4j.appender.C=org.apache.log4j.ConsoleAppender
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
# Info Rolling Log
log4j.appender.IL.Threshold=INFO
log4j.appender.IL=org.apache.log4j.RollingFileAppender
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.Threshold=ERROR
log4j.appender.EL=org.apache.log4j.RollingFileAppender
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

38
src/main/java/com/sothr/imagetools/App.java

@ -1,13 +1,49 @@
package com.sothr.imagetools;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Properties;
/**
* Hello world!
*
*/
public class App
{
private static Logger logger = LoggerFactory.getLogger(App.class);
public static void main( String[] args )
{
System.out.println( "Hello World!" );
File file = new File("log4j.properties");
if (file.exists()) {
PropertyConfigurator.configure("log4j.properties");
} else {
//Simple error logging configuration
Properties defaultProps = new Properties();
defaultProps.setProperty("log4j.rootLogger","ERROR, A1");
//Rolling Error logger
defaultProps.setProperty("log4j.appender.A1","org.apache.log4j.RollingFileAppender");
defaultProps.setProperty("log4j.appender.A1.File","Image-Tools.err");
defaultProps.setProperty("log4j.appender.A1.MaxFileSize","100KB");
defaultProps.setProperty("log4j.appender.A1.MaxBackupIndex","1");
defaultProps.setProperty("log4j.appender.A1.layout","org.apache.log4j.EnhancedPatternLayout");
defaultProps.setProperty("log4j.appender.A1.layout.ConversionPattern","%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n");
PropertyConfigurator.configure(defaultProps);
}
logger.info("Image-Tools is starting");
try {
System.out.println( "Hello World!" );
} catch (Exception ex) {
logger.error("A fatal error has occurred: ", ex);
//show popup about the error to the user then exit
}
logger.info("Image-Tools is shutting down");
}
}
Loading…
Cancel
Save