Search in sources :

Example 36 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project gerrit by GerritCodeReview.

the class HttpLog method log.

@Override
public void log(final Request req, final Response rsp) {
    final LoggingEvent event = new //
    LoggingEvent(// fqnOfCategoryClass
    Logger.class.getName(), // logger
    log, // when
    TimeUtil.nowMs(), // level
    Level.INFO, // message text
    "", // thread name
    "HTTPD", // exception information
    null, // current NDC string
    null, // caller location
    null, // MDC properties
    null);
    String uri = req.getRequestURI();
    String qs = req.getQueryString();
    if (qs != null) {
        uri = uri + "?" + qs;
    }
    String user = (String) req.getAttribute(GetUserFilter.REQ_ATTR_KEY);
    if (user != null) {
        event.setProperty(P_USER, user);
    }
    set(event, P_HOST, req.getRemoteAddr());
    set(event, P_METHOD, req.getMethod());
    set(event, P_RESOURCE, uri);
    set(event, P_PROTOCOL, req.getProtocol());
    set(event, P_STATUS, rsp.getStatus());
    set(event, P_CONTENT_LENGTH, rsp.getContentCount());
    set(event, P_REFERER, req.getHeader("Referer"));
    set(event, P_USER_AGENT, req.getHeader("User-Agent"));
    async.append(event);
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Logger(org.apache.log4j.Logger)

Example 37 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project symmetric-ds by JumpMind.

the class SymRollingFileAppenderTest method testMixedMessages.

@Test
public void testMixedMessages() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    SymRollingFileAppender appender = getAppenderForTest(os);
    Exception ex = new Exception("Test exception.");
    Exception ex2 = new Exception("Test exception.");
    LoggingEvent event1 = getLoggingEventForTest("Test Exception.", ex);
    LoggingEvent event2 = getLoggingEventForTest("Test Exception.", ex2);
    LoggingEvent event3 = getLoggingEventForTest("Test Exception.", ex);
    LoggingEvent event4 = getLoggingEventForTest("Test Exception.", ex2);
    appender.append(event1);
    appender.append(event2);
    appender.append(event3);
    appender.append(event4);
    String logging = os.toString("UTF8");
    // 2016-08-11 11:55:38,487 ERROR [] [SymRollingFileAppenderTest] [main] Test Exception. StackTraceKey.init [Exception:1478675418]
    Pattern initPattern = Pattern.compile(".*StackTraceKey.init \\[Exception:([0-9]*)\\].*", Pattern.DOTALL);
    Matcher m = initPattern.matcher(logging);
    if (m.matches()) {
        {
            String stackTraceKey = "Exception:" + m.group(1);
            assertEquals(2, StringUtils.countMatches(logging, stackTraceKey));
        }
        m.matches();
        {
            String stackTraceKey = "Exception:" + m.group(1);
            assertEquals(2, StringUtils.countMatches(logging, stackTraceKey));
        }
    } else {
        fail("Didn't find proper logging pattern.");
    }
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.Test)

Example 38 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project symmetric-ds by JumpMind.

the class SymRollingFileAppenderTest method testDistinctLogMessages.

@Test
public void testDistinctLogMessages() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    SymRollingFileAppender appender = getAppenderForTest(os);
    Exception ex = new Exception("Test exception.");
    Exception ex2 = new Exception("Test exception.");
    LoggingEvent event1 = getLoggingEventForTest("Test Exception.", ex);
    LoggingEvent event2 = getLoggingEventForTest("Test Exception.", ex2);
    appender.append(event1);
    appender.append(event2);
    String logging = os.toString("UTF8");
    // 2016-08-11 11:55:38,487 ERROR [] [SymRollingFileAppenderTest] [main] Test Exception. StackTraceKey.init [Exception:1478675418]
    Pattern initPattern = Pattern.compile(".*StackTraceKey.init \\[Exception:([0-9]*)\\].*", Pattern.DOTALL);
    Matcher m = initPattern.matcher(logging);
    if (m.matches()) {
        {
            String stackTraceKey = "Exception:" + m.group(1);
            assertEquals(1, StringUtils.countMatches(logging, stackTraceKey));
        }
        m.matches();
        {
            String stackTraceKey = "Exception:" + m.group(1);
            assertEquals(1, StringUtils.countMatches(logging, stackTraceKey));
        }
    } else {
        fail("Didn't find proper logging pattern.");
    }
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.Test)

Example 39 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project symmetric-ds by JumpMind.

the class SymRollingFileAppender method supressStackTrace.

protected LoggingEvent supressStackTrace(LoggingEvent event, String key) {
    String message = getMessageWithKey(event, key);
    LoggingEvent eventClone = new LoggingEvent(event.getFQNOfLoggerClass(), event.getLogger(), event.getTimeStamp(), event.getLevel(), message, event.getThreadName(), null, event.getNDC(), event.getLocationInformation(), event.getProperties());
    return eventClone;
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent)

Example 40 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project symmetric-ds by JumpMind.

the class SymRollingFileAppenderTest method testDebugMode.

@Test
public void testDebugMode() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    SymRollingFileAppender appender = getAppenderForTest(os);
    Exception ex = new Exception("Test exception.");
    LoggingEvent event1 = getLoggingEventForTest("Test Exception.", ex, Level.DEBUG);
    LoggingEvent event2 = getLoggingEventForTest("Test Exception.", ex, Level.DEBUG);
    LoggingEvent event3 = getLoggingEventForTest("Test Exception.", ex, Level.DEBUG);
    appender.append(event1);
    appender.append(event2);
    appender.append(event3);
    String logging = os.toString("UTF8");
    // 2016-08-11 11:55:38,487 ERROR [] [SymRollingFileAppenderTest] [main] Test Exception. StackTraceKey.init [Exception:1478675418]
    Pattern initPattern = Pattern.compile(".*StackTraceKey.init \\[Exception:([0-9]*)\\].*", Pattern.DOTALL);
    Matcher m = initPattern.matcher(logging);
    // everything should get logged with a DEBUG logger, so we should't find the StackTraceKey pattern.
    assertFalse(m.matches());
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

LoggingEvent (org.apache.log4j.spi.LoggingEvent)48 Test (org.junit.Test)21 Logger (org.apache.log4j.Logger)12 Pattern (java.util.regex.Pattern)7 Matcher (java.util.regex.Matcher)6 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)6 AppenderSkeleton (org.apache.log4j.AppenderSkeleton)3 LoggingException (com.axway.ats.log.autodb.exceptions.LoggingException)2 AbstractLoggingEvent (com.axway.ats.log.autodb.model.AbstractLoggingEvent)2 IOException (java.io.IOException)2 Appender (org.apache.log4j.Appender)2 Category (org.apache.log4j.Category)2 Level (org.apache.log4j.Level)2 Filter (org.apache.log4j.spi.Filter)2 LocationInfo (org.apache.log4j.spi.LocationInfo)2 InsertMessageEvent (com.axway.ats.log.autodb.events.InsertMessageEvent)1 CacheableEvent (com.axway.ats.log.autodb.model.CacheableEvent)1 SystemLogLevel (com.axway.ats.log.model.SystemLogLevel)1 CurrentUser (com.google.gerrit.server.CurrentUser)1 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)1