Search in sources :

Example 66 with PatternLayout

use of org.apache.log4j.PatternLayout in project n4js by eclipse.

the class N4jsUiLoggingInitializer method init.

/**
 * Initializes the log4j logging with an additional appender which routes the logging to the Eclipse ErrorView.
 */
public static void init() {
    Logger rootLogger = Logger.getRootLogger();
    // # This appender will write to the Eclipse error log. It will ONLY log ERROR and FATAL messages.
    // log4j.appender.eclipse=org.eclipse.xtext.logging.EclipseLogAppender
    EclipseLogAppender eclipseAppender = new EclipseLogAppender();
    eclipseAppender.setName("eclipse");
    rootLogger.addAppender(eclipseAppender);
    // log4j.appender.eclipse.layout=org.apache.log4j.PatternLayout
    // log4j.appender.eclipse.layout.ConversionPattern=%c %x - %m%n
    eclipseAppender.setLayout(new PatternLayout("%c %x - %m%n"));
    // # EclipseLogAppender must not log to the 'eclipse' appender as that would cause a stack overflow!
    // log4j.additivity.org.eclipse.xtext.logging.EclipseLogAppender=false
    // log4j.logger.org.eclipse.xtext.logging.EclipseLogAppender=WARN, default
    Logger eclipseAppenderLogger = Logger.getLogger(EclipseLogAppender.class);
    eclipseAppenderLogger.setAdditivity(false);
    eclipseAppenderLogger.setLevel(Level.WARN);
    Appender defaultAppender = eclipseAppenderLogger.getAppender("default");
    if (eclipseAppenderLogger.getAllAppenders().hasMoreElements()) {
        eclipseAppenderLogger.removeAllAppenders();
    }
    if (defaultAppender != null) {
        eclipseAppenderLogger.addAppender(defaultAppender);
    }
}
Also used : Appender(org.apache.log4j.Appender) PatternLayout(org.apache.log4j.PatternLayout) Logger(org.apache.log4j.Logger)

Example 67 with PatternLayout

use of org.apache.log4j.PatternLayout in project incubator-servicecomb-java-chassis by apache.

the class Log4JMetricsFileWriter method initLogger.

private RollingFileAppender initLogger(String loggerName, String filePrefix) {
    String fileName = Paths.get(rootPath, filePrefix + "." + loggerName + ".dat").toString();
    RollingFileAppender fileAppender = new RollingFileAppender();
    fileAppender.setName(loggerName);
    fileAppender.setFile(fileName);
    fileAppender.setLayout(new PatternLayout("%m%n"));
    fileAppender.setAppend(true);
    fileAppender.setMaxFileSize(maxFileSize);
    fileAppender.setMaxBackupIndex(maxFileCount);
    fileAppender.activateOptions();
    return fileAppender;
}
Also used : RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout)

Example 68 with PatternLayout

use of org.apache.log4j.PatternLayout in project walkmod-core by walkmod.

the class AbstractWalkmodExecutionTest method run.

public String run(String[] args) throws Exception {
    ByteArrayOutputStream mem = new ByteArrayOutputStream();
    BufferedOutputStream stream = new BufferedOutputStream(mem);
    PrintStream ps = new PrintStream(stream);
    PrintStream old = System.out;
    System.setOut(ps);
    WalkModFacade.log = Logger.getLogger(WalkModFacade.class.getName());
    WalkModFacade.log.removeAllAppenders();
    ConfigurationImpl.log = Logger.getLogger(ConfigurationImpl.class.getName());
    ConfigurationImpl.log.removeAllAppenders();
    ConsoleAppender appender = new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
    appender.setName("stdout");
    WalkModFacade.log.addAppender(appender);
    ConfigurationImpl.log.addAppender(appender);
    String result = "";
    try {
        WalkModDispatcher.main(args);
        stream.flush();
        result = mem.toString();
    } finally {
        System.setOut(old);
        ps.close();
        stream.close();
        mem.close();
    }
    return result;
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) PrintStream(java.io.PrintStream) PatternLayout(org.apache.log4j.PatternLayout) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 69 with PatternLayout

use of org.apache.log4j.PatternLayout in project QueryAnalysis by Wikidata.

the class LoggingHandler method initFileLog.

/**
 * Defines an appender that writes all log messages of level levelToWrite or higher to the file fileToWrite.
 *
 * @param fileToWrite File to write the log to.
 */
public static void initFileLog(String fileToWrite, Level levelToWrite) {
    TimestampFileAppender fileAppender = new TimestampFileAppender();
    fileAppender.setName("FileLogger");
    fileAppender.setLayout(new PatternLayout(" [%p] %d [%t] %x %c %M - %m%n"));
    fileAppender.setFile(fileToWrite);
    fileAppender.setThreshold(levelToWrite);
    fileAppender.activateOptions();
    Logger.getRootLogger().addAppender(fileAppender);
}
Also used : PatternLayout(org.apache.log4j.PatternLayout)

Example 70 with PatternLayout

use of org.apache.log4j.PatternLayout 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)

Aggregations

PatternLayout (org.apache.log4j.PatternLayout)101 ConsoleAppender (org.apache.log4j.ConsoleAppender)51 Logger (org.apache.log4j.Logger)27 IOException (java.io.IOException)21 Appender (org.apache.log4j.Appender)18 FileAppender (org.apache.log4j.FileAppender)17 Layout (org.apache.log4j.Layout)15 File (java.io.File)14 RollingFileAppender (org.apache.log4j.RollingFileAppender)12 Test (org.junit.Test)8 CommandLineParser (org.apache.commons.cli.CommandLineParser)7 GnuParser (org.apache.commons.cli.GnuParser)7 ParseException (org.apache.commons.cli.ParseException)7 FileInputStream (java.io.FileInputStream)6 Properties (java.util.Properties)6 CommandLine (org.apache.commons.cli.CommandLine)6 Option (org.apache.commons.cli.Option)6 Options (org.apache.commons.cli.Options)6 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)6 WriterAppender (org.apache.log4j.WriterAppender)6