Search in sources :

Example 1 with ThreadLocalFileAppender

use of com.revolsys.logging.log4j.ThreadLocalFileAppender in project com.revolsys.open by revolsys.

the class ProcessorPipelineTool method processFile.

private void processFile(final File sourceFile, final File targetFile, final File logFile) {
    final long startTime = System.currentTimeMillis();
    if (this.excludePattern != null) {
        try {
            if (sourceFile.getCanonicalPath().matches(this.excludePattern)) {
                return;
            }
        } catch (final IOException e) {
            log.error(e.getMessage(), e);
        }
    }
    final ThreadLocalFileAppender localAppender = ThreadLocalFileAppender.getAppender();
    if (localAppender != null) {
        final File parentFile = logFile.getParentFile();
        if (parentFile != null) {
            parentFile.mkdirs();
        }
        localAppender.setLocalFile(logFile.getAbsolutePath());
    }
    log.info("Processing file '" + sourceFile + "' to '" + targetFile + "'");
    System.out.println("Processing file '" + sourceFile + "' to '" + targetFile + "'");
    System.setProperty("sourceFile", sourceFile.getAbsolutePath());
    System.setProperty("targetFile", targetFile.getAbsolutePath());
    final BeanFactory beans = new FileSystemXmlApplicationContext("file:" + this.scriptFile.getAbsolutePath());
    try {
        final File parentFile = targetFile.getParentFile();
        if (parentFile != null) {
            parentFile.mkdirs();
        }
        final Object bean = beans.getBean("pipeline");
        final ProcessNetwork pipeline = (ProcessNetwork) bean;
        pipeline.startAndWait();
    } catch (final BeanCreationException e) {
        final Throwable cause = getBeanExceptionCause(e);
        cause.printStackTrace();
    }
    final long endTime = System.currentTimeMillis();
    final long time = endTime - startTime;
    long seconds = time / 1000;
    final long minutes = seconds / 60;
    seconds = seconds % 60;
    log.info(minutes + " minutes " + seconds + " seconds");
    System.out.println(minutes + " minutes " + seconds + " seconds");
}
Also used : FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) BeanCreationException(org.springframework.beans.factory.BeanCreationException) ThreadLocalFileAppender(com.revolsys.logging.log4j.ThreadLocalFileAppender) ProcessNetwork(com.revolsys.parallel.process.ProcessNetwork) BeanFactory(org.springframework.beans.factory.BeanFactory) IOException(java.io.IOException) File(java.io.File)

Example 2 with ThreadLocalFileAppender

use of com.revolsys.logging.log4j.ThreadLocalFileAppender in project com.revolsys.open by revolsys.

the class ScriptTool method run.

private void run() {
    final long startTime = System.currentTimeMillis();
    final ThreadLocalFileAppender localAppender = ThreadLocalFileAppender.getAppender();
    if (localAppender != null && this.logFile != null) {
        final File parentFile = this.logFile.getParentFile();
        if (parentFile != null) {
            parentFile.mkdirs();
        }
        localAppender.setLocalFile(this.logFile.getAbsolutePath());
    } else if (this.logFile != null) {
        final org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
        try {
            final Layout layout = new PatternLayout("%d\t%p\t%m%n");
            final Appender appender = new FileAppender(layout, this.logFile.getAbsolutePath(), false);
            rootLogger.addAppender(appender);
        } catch (final IOException e) {
            final Layout layout = new PatternLayout("%p\t%m%n");
            final Appender appender = new ConsoleAppender(layout);
            rootLogger.addAppender(appender);
            Logs.error(this, "Cannot find log file " + this.logFile, e);
        }
    }
    final StringBuilder message = new StringBuilder("Processing ");
    message.append(" -s ");
    message.append(this.scriptFileName);
    if (this.propertiesName != null) {
        message.append(" -p ");
        message.append(this.propertiesName);
    }
    for (final Entry<String, String> parameter : this.parameters.entrySet()) {
        message.append(" ");
        message.append(parameter.getKey());
        message.append("=");
        message.append(parameter.getValue());
    }
    System.out.println(message);
    if (System.getProperty("applicationHome") == null) {
        ThreadSharedProperties.setProperty("applicationHome", ".");
        System.setProperty("applicationHome", ".");
    }
    try {
        final GenericApplicationContext beans = new GenericApplicationContext();
        AnnotationConfigUtils.registerAnnotationConfigProcessors(beans, null);
        beans.getBeanFactory().addPropertyEditorRegistrar(new ResourceEditorRegistrar());
        if (this.scriptFile != null) {
            new XmlBeanDefinitionReader(beans).loadBeanDefinitions("file:" + this.scriptFile.getAbsolutePath());
        } else {
            new XmlBeanDefinitionReader(beans).loadBeanDefinitions("classpath:" + this.scriptFileName);
        }
        beans.refresh();
        try {
            Logs.info(this, message.toString());
            final Object bean = beans.getBean("processNetwork");
            final ProcessNetwork pipeline = (ProcessNetwork) bean;
            pipeline.startAndWait();
        } finally {
            beans.close();
        }
    } catch (final BeanCreationException e) {
        final Throwable cause = getBeanExceptionCause(e);
        Logs.error(this, cause.getMessage(), cause);
        cause.printStackTrace();
        System.err.flush();
    }
    final long endTime = System.currentTimeMillis();
    final long time = endTime - startTime;
    long seconds = time / 1000;
    final long minutes = seconds / 60;
    seconds = seconds % 60;
    Logs.info(this, minutes + " minutes " + seconds + " seconds");
    System.out.println(minutes + " minutes " + seconds + " seconds");
}
Also used : Appender(org.apache.log4j.Appender) ThreadLocalFileAppender(com.revolsys.logging.log4j.ThreadLocalFileAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) ThreadLocalFileAppender(com.revolsys.logging.log4j.ThreadLocalFileAppender) FileAppender(org.apache.log4j.FileAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) BeanCreationException(org.springframework.beans.factory.BeanCreationException) ResourceEditorRegistrar(com.revolsys.beans.propertyeditor.ResourceEditorRegistrar) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) PatternLayout(org.apache.log4j.PatternLayout) ProcessNetwork(com.revolsys.parallel.process.ProcessNetwork) IOException(java.io.IOException) ThreadLocalFileAppender(com.revolsys.logging.log4j.ThreadLocalFileAppender) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) File(java.io.File)

Example 3 with ThreadLocalFileAppender

use of com.revolsys.logging.log4j.ThreadLocalFileAppender in project com.revolsys.open by revolsys.

the class ThreadLocalAppenderBean method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
    final ThreadLocalFileAppender localAppender = ThreadLocalFileAppender.getAppender();
    final String logFilePath = this.logFile.getAbsolutePath();
    if (localAppender != null && this.logFile != null) {
        final File parentFile = this.logFile.getParentFile();
        if (parentFile != null) {
            parentFile.mkdirs();
        }
        localAppender.setLocalFile(logFilePath);
    } else if (this.logFile != null) {
        final org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
        try {
            final Layout layout = new PatternLayout("%d\t%p\t%m%n");
            final Appender appender = new ThreadLocalFileAppender(layout, logFilePath, false);
            appender.setName("ThreadLocalLog");
            rootLogger.addAppender(appender);
        } catch (final IOException e) {
            final Layout layout = new PatternLayout("%p\t%m%n");
            final Appender appender = new ConsoleAppender(layout);
            rootLogger.addAppender(appender);
            Logs.error(this, "Cannot find log file " + this.logFile, e);
        }
    }
}
Also used : Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) ThreadLocalFileAppender(com.revolsys.logging.log4j.ThreadLocalFileAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) ThreadLocalFileAppender(com.revolsys.logging.log4j.ThreadLocalFileAppender) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) File(java.io.File)

Aggregations

ThreadLocalFileAppender (com.revolsys.logging.log4j.ThreadLocalFileAppender)3 File (java.io.File)3 IOException (java.io.IOException)3 ProcessNetwork (com.revolsys.parallel.process.ProcessNetwork)2 Appender (org.apache.log4j.Appender)2 ConsoleAppender (org.apache.log4j.ConsoleAppender)2 Layout (org.apache.log4j.Layout)2 PatternLayout (org.apache.log4j.PatternLayout)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 ResourceEditorRegistrar (com.revolsys.beans.propertyeditor.ResourceEditorRegistrar)1 FileAppender (org.apache.log4j.FileAppender)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1 FileSystemXmlApplicationContext (org.springframework.context.support.FileSystemXmlApplicationContext)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1