Search in sources :

Example 41 with PatternLayout

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

the class Log4JInitServlet method init.

@Override
public void init() {
    String file = getInitParameter("log4j-init-file");
    ClassPathResource res = new ClassPathResource(file);
    if (!res.exists()) {
        // creating basic log4j configuration which writes to console out, Only called when not yet configured
        ConsoleAppender appender = new ConsoleAppender(new PatternLayout("%d{ABSOLUTE} %5p %c{1}:%L - %m%n"), ConsoleAppender.SYSTEM_OUT);
        appender.setThreshold(Level.INFO);
        BasicConfigurator.configure(appender);
        OLog log = Tracing.createLoggerFor(getClass());
        log.info("*****************************************************************************************");
        log.info("You don't provide a log4j config file for your OLAT instance. OLAT will just log to standard out (e.g. catalina.out)." + " Please provide a proper log config file (log4j.xml, see olat/conf for an example or read the installation guide) " + "and place it into the root of the classpath e.g. tomcat/lib or WEB-INF/classes");
        log.info("*****************************************************************************************");
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) OLog(org.olat.core.logging.OLog) PatternLayout(org.apache.log4j.PatternLayout) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 42 with PatternLayout

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

the class FailedMessagesLogger method init.

void init() {
    String rootLoggerDirectory = getRootLoggerDirectory();
    if (rootLoggerDirectory == null) {
        return;
    }
    String absolutePath = new File(rootLoggerDirectory, failedMessageFile).getAbsolutePath();
    try {
        DailyRollingFileAppender failedLogFilesAppender = new DailyRollingFileAppender(new PatternLayout(PATTERN_SPEC_TIMESTAMP_MESSAGE_NEWLINE), absolutePath, DATE_PATTERN);
        logger.addAppender(failedLogFilesAppender);
        logger.setLevel(Level.ERROR);
        logger.setAdditivity(false);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) File(java.io.File) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender)

Example 43 with PatternLayout

use of org.apache.log4j.PatternLayout in project pmd-eclipse-plugin by pmd.

the class PMDPlugin method configureLogs.

/**
 * Configure the logging
 */
private void configureLogs(IPreferences preferences) {
    try {
        Layout layout = new PatternLayout("%d{yyyy/MM/dd HH:mm:ss,SSS} %-5p %-32c{1} %m%n");
        RollingFileAppender appender = new RollingFileAppender(layout, preferences.getLogFileName());
        appender.setName(PMD_ECLIPSE_APPENDER_NAME);
        appender.setMaxBackupIndex(1);
        appender.setMaxFileSize("10MB");
        Logger.getRootLogger().addAppender(new ConsoleAppender(layout));
        Logger.getRootLogger().setLevel(Level.WARN);
        Logger.getRootLogger().setAdditivity(false);
        Logger.getLogger(ROOT_LOG_ID).addAppender(appender);
        Logger.getLogger(ROOT_LOG_ID).setLevel(preferences.getLogLevel());
        Logger.getLogger(ROOT_LOG_ID).setAdditivity(false);
    } catch (IOException e) {
        logError("IO Exception when configuring logging.", e);
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException)

Example 44 with PatternLayout

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

the class TestStreamAppender method testQueueTimeout.

@Test
public void testQueueTimeout() throws InterruptedException {
    System.setProperty("samza.container.name", "samza-container-1");
    MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender();
    systemProducerAppender.queueTimeoutS = 1;
    PatternLayout layout = new PatternLayout();
    layout.setConversionPattern("%m");
    systemProducerAppender.setLayout(layout);
    systemProducerAppender.activateOptions();
    LOG.addAppender(systemProducerAppender);
    int extraMessageCount = 5;
    // -1 because when the queue is drained there is one additional message that couldn't be added
    int expectedMessagesSent = extraMessageCount - 1;
    List<String> messages = new ArrayList<>(StreamAppender.DEFAULT_QUEUE_SIZE + extraMessageCount);
    for (int i = 0; i < StreamAppender.DEFAULT_QUEUE_SIZE + extraMessageCount; i++) {
        messages.add(String.valueOf(i));
    }
    // Set up latch
    // We expect to drop all but the extra messages
    final CountDownLatch allMessagesSent = new CountDownLatch(expectedMessagesSent);
    final CountDownLatch waitForTimeout = new CountDownLatch(1);
    MockSystemProducer.listeners.add((source, envelope) -> {
        allMessagesSent.countDown();
        try {
            waitForTimeout.await();
        } catch (InterruptedException e) {
            fail("Test could not run properly because of a thread interrupt.");
        }
    });
    // Log the messages. This is where the timeout will happen!
    messages.forEach(LOG::info);
    assertEquals(messages.size() - expectedMessagesSent, systemProducerAppender.metrics.logMessagesDropped.getCount());
    // Allow all the rest of the messages to send.
    waitForTimeout.countDown();
    // Wait for messages
    assertTrue("Thread did not send all messages. Count: " + allMessagesSent.getCount(), allMessagesSent.await(60, TimeUnit.SECONDS));
    assertEquals(expectedMessagesSent, MockSystemProducer.messagesReceived.size());
}
Also used : PatternLayout(org.apache.log4j.PatternLayout) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 45 with PatternLayout

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

the class TestStreamAppender method testSystemProducerAppenderNotInitialized.

@Test
public void testSystemProducerAppenderNotInitialized() throws InterruptedException {
    System.setProperty("samza.container.name", "samza-job-coordinator");
    // add a counter to make sure that the initial message doesn't get produced
    AtomicInteger numMessagesProduced = new AtomicInteger(0);
    MockSystemProducer.listeners.add((source, envelope) -> numMessagesProduced.incrementAndGet());
    MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender(baseConfig(), false);
    PatternLayout layout = new PatternLayout();
    layout.setConversionPattern("%m");
    systemProducerAppender.setLayout(layout);
    systemProducerAppender.activateOptions();
    LOG.addAppender(systemProducerAppender);
    // System isn't initialized yet, so this message should be dropped
    LOG.info("no-received");
    // explicitly trigger initialization to test that new messages do get sent to the stream
    systemProducerAppender.setupSystem();
    systemProducerAppender.systemInitialized = true;
    List<String> messages = Lists.newArrayList("testing3", "testing4");
    logAndVerifyMessages(messages);
    assertEquals(messages.size(), numMessagesProduced.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PatternLayout(org.apache.log4j.PatternLayout) Test(org.junit.Test)

Aggregations

PatternLayout (org.apache.log4j.PatternLayout)106 ConsoleAppender (org.apache.log4j.ConsoleAppender)49 Logger (org.apache.log4j.Logger)30 IOException (java.io.IOException)20 Appender (org.apache.log4j.Appender)20 FileAppender (org.apache.log4j.FileAppender)17 File (java.io.File)14 Layout (org.apache.log4j.Layout)14 RollingFileAppender (org.apache.log4j.RollingFileAppender)12 Test (org.junit.Test)12 WriterAppender (org.apache.log4j.WriterAppender)9 CommandLineParser (org.apache.commons.cli.CommandLineParser)7 GnuParser (org.apache.commons.cli.GnuParser)7 ParseException (org.apache.commons.cli.ParseException)7 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)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