Search in sources :

Example 71 with PatternLayout

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

Example 72 with PatternLayout

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

the class OpenmrsUtil method setupLogAppenders.

/**
 * Setup root level log appenders.
 *
 * @since 1.9.2
 */
public static void setupLogAppenders() {
    Logger rootLogger = Logger.getRootLogger();
    FileAppender fileAppender = null;
    @SuppressWarnings("rawtypes") Enumeration appenders = rootLogger.getAllAppenders();
    while (appenders.hasMoreElements()) {
        Appender appender = (Appender) appenders.nextElement();
        if (OpenmrsConstants.LOG_OPENMRS_FILE_APPENDER.equals(appender.getName())) {
            // the appender already exists
            fileAppender = (FileAppender) appender;
            break;
        }
    }
    String logLayout = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_LOG_LAYOUT, "%p - %C{1}.%M(%L) |%d{ISO8601}| %m%n");
    PatternLayout patternLayout = new PatternLayout(logLayout);
    String logLocation = null;
    try {
        logLocation = OpenmrsUtil.getOpenmrsLogLocation();
        if (fileAppender == null) {
            fileAppender = new RollingFileAppender(patternLayout, logLocation);
            fileAppender.setName(OpenmrsConstants.LOG_OPENMRS_FILE_APPENDER);
            rootLogger.addAppender(fileAppender);
        } else {
            fileAppender.setFile(logLocation);
            fileAppender.setLayout(patternLayout);
        }
        fileAppender.activateOptions();
    } catch (IOException e) {
        log.error("Error while setting an OpenMRS log file to " + logLocation, e);
    }
}
Also used : RollingFileAppender(org.apache.log4j.RollingFileAppender) FileAppender(org.apache.log4j.FileAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) Enumeration(java.util.Enumeration) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) Logger(org.apache.log4j.Logger)

Example 73 with PatternLayout

use of org.apache.log4j.PatternLayout in project traccar by traccar.

the class Log method setupLogger.

public static void setupLogger(Config config) throws IOException {
    Layout layout = new PatternLayout("%d{" + DATE_FORMAT + "} %5p: %m%n");
    Appender appender = new DailyRollingFileAppender(layout, config.getString("logger.file"), "'.'yyyyMMdd");
    LogManager.resetConfiguration();
    LogManager.getRootLogger().addAppender(new NullAppender());
    logger = Logger.getLogger(LOGGER_NAME);
    logger.addAppender(appender);
    logger.setLevel(Level.toLevel(config.getString("logger.level"), Level.ALL));
    // Workaround for "Bug 745866 - (EDG-45) Possible netty logging config problem"
    InternalLoggerFactory.setDefaultFactory(new InternalLoggerFactory() {

        @Override
        public InternalLogger newInstance(String string) {
            return new NettyInternalLogger();
        }
    });
    Log.logSystemInfo();
    Log.info("Version: " + getAppVersion());
}
Also used : NullAppender(org.apache.log4j.varia.NullAppender) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) Appender(org.apache.log4j.Appender) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) NullAppender(org.apache.log4j.varia.NullAppender) PatternLayout(org.apache.log4j.PatternLayout) InternalLoggerFactory(org.jboss.netty.logging.InternalLoggerFactory) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) InternalLogger(org.jboss.netty.logging.InternalLogger) AbstractInternalLogger(org.jboss.netty.logging.AbstractInternalLogger)

Example 74 with PatternLayout

use of org.apache.log4j.PatternLayout in project devonfw-testing by devonfw.

the class BFLoggerInstance method createFileAppender.

private FileAppender createFileAppender() {
    PatternLayout patternLayout = new PatternLayout(logPattern);
    FileAppender appender;
    try {
        appender = new FileAppender(patternLayout, getLogFile().getPath(), true);
        appender.setName(getAppenderName());
        appender.setThreshold(Level.DEBUG);
        appender.activateOptions();
    } catch (Exception e) {
        System.out.println("Unable to create appender: " + e);
        return null;
    }
    return appender;
}
Also used : FileAppender(org.apache.log4j.FileAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 75 with PatternLayout

use of org.apache.log4j.PatternLayout in project devonfw-testing by devonfw.

the class BFLoggerInstance method createConsoleAppender.

private ConsoleAppender createConsoleAppender() {
    PatternLayout patternLayout = new PatternLayout(logPattern);
    ConsoleAppender appender;
    try {
        appender = new ConsoleAppender(patternLayout);
        appender.setTarget(ConsoleAppender.SYSTEM_OUT);
        appender.setName("Console");
        appender.setThreshold(Level.DEBUG);
        appender.activateOptions();
    } catch (Exception e) {
        System.out.println("Unable to create appender: " + e);
        return null;
    }
    return appender;
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

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