Search in sources :

Example 1 with LocationInfo

use of org.apache.log4j.spi.LocationInfo in project pentaho-platform by pentaho.

the class RepositoryImportHTMLLayout method format.

public String format(LoggingEvent event) {
    Level logLevel = event.getLevel();
    if (sbuf.capacity() > MAX_CAPACITY) {
        sbuf = new StringBuffer(BUF_SIZE);
    } else {
        sbuf.setLength(0);
    }
    if (showTimeColumn()) {
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date date = new Date();
        date.setTime(event.timeStamp);
        String time = null;
        try {
            time = df.format(date);
        } catch (Exception ex) {
            LogLog.error("Error occured while converting date.", ex);
        }
        sbuf.append(Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP);
        sbuf.append("<td>");
        sbuf.append(Transform.escapeTags(time));
        sbuf.append("</td>" + Layout.LINE_SEP);
    }
    sbuf.append("<td title=\"importFile\">");
    sbuf.append(Transform.escapeTags(MDC.get(Log4JRepositoryImportLog.FILE_KEY)));
    sbuf.append("</td>" + Layout.LINE_SEP);
    if (showLevelColumn()) {
        sbuf.append("<td title=\"Level\">");
        if (logLevel.equals(Level.DEBUG)) {
            sbuf.append("<font color=\"#339933\">");
            sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
            sbuf.append("</font>");
        } else if (logLevel.isGreaterOrEqual(Level.WARN)) {
            sbuf.append("<font color=\"#993300\"><strong>");
            sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
            sbuf.append("</strong></font>");
        } else {
            sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
        }
        sbuf.append("</td>" + Layout.LINE_SEP);
    }
    if (showCodeLineColumn()) {
        LocationInfo locInfo = event.getLocationInformation();
        sbuf.append("<td>");
        sbuf.append(Transform.escapeTags(locInfo.getFileName()));
        sbuf.append(':');
        sbuf.append(locInfo.getLineNumber());
        sbuf.append("</td>" + Layout.LINE_SEP);
    }
    sbuf.append("<td title=\"Message\">");
    sbuf.append(Transform.escapeTags(event.getRenderedMessage()));
    sbuf.append("</td>" + Layout.LINE_SEP);
    sbuf.append("</tr>" + Layout.LINE_SEP);
    if (event.getNDC() != null) {
        sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : " + "xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">");
        sbuf.append("NDC: " + Transform.escapeTags(event.getNDC()));
        sbuf.append("</td></tr>" + Layout.LINE_SEP);
    }
    String[] s = event.getThrowableStrRep();
    if (s != null) {
        sbuf.append("<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">");
        appendThrowableAsHTML(s, sbuf);
        sbuf.append("</td></tr>" + Layout.LINE_SEP);
    }
    return sbuf.toString();
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Level(org.apache.log4j.Level) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) LocationInfo(org.apache.log4j.spi.LocationInfo)

Example 2 with LocationInfo

use of org.apache.log4j.spi.LocationInfo in project samza by apache.

the class LoggingEventJsonSerde method encodeToMap.

/**
 * Encodes a LoggingEvent into a HashMap using the logstash JSON format.
 *
 * @param loggingEvent
 *          The LoggingEvent to encode.
 * @param includeLocationInfo
 *          Whether to include LocationInfo in the map, or not.
 * @return A Map representing the LoggingEvent, which is suitable to be
 *         serialized by a JSON encoder such as Jackson.
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> encodeToMap(LoggingEvent loggingEvent, boolean includeLocationInfo) {
    Map<String, Object> logstashEvent = new LoggingEventMap();
    String threadName = loggingEvent.getThreadName();
    long timestamp = loggingEvent.getTimeStamp();
    HashMap<String, Object> exceptionInformation = new HashMap<String, Object>();
    Map mdc = loggingEvent.getProperties();
    String ndc = loggingEvent.getNDC();
    logstashEvent.put("@version", VERSION);
    logstashEvent.put("@timestamp", dateFormat(timestamp));
    logstashEvent.put("source_host", getHostname());
    logstashEvent.put("message", loggingEvent.getRenderedMessage());
    if (loggingEvent.getThrowableInformation() != null) {
        final ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
        if (throwableInformation.getThrowable().getClass().getCanonicalName() != null) {
            exceptionInformation.put("exception_class", throwableInformation.getThrowable().getClass().getCanonicalName());
        }
        if (throwableInformation.getThrowable().getMessage() != null) {
            exceptionInformation.put("exception_message", throwableInformation.getThrowable().getMessage());
        }
        if (throwableInformation.getThrowableStrRep() != null) {
            StringBuilder stackTrace = new StringBuilder();
            for (String line : throwableInformation.getThrowableStrRep()) {
                stackTrace.append(line);
                stackTrace.append("\n");
            }
            exceptionInformation.put("stacktrace", stackTrace);
        }
        logstashEvent.put("exception", exceptionInformation);
    }
    if (includeLocationInfo) {
        LocationInfo info = loggingEvent.getLocationInformation();
        logstashEvent.put("file", info.getFileName());
        logstashEvent.put("line_number", info.getLineNumber());
        logstashEvent.put("class", info.getClassName());
        logstashEvent.put("method", info.getMethodName());
    }
    logstashEvent.put("logger_name", loggingEvent.getLoggerName());
    logstashEvent.put("mdc", mdc);
    logstashEvent.put("ndc", ndc);
    logstashEvent.put("level", loggingEvent.getLevel().toString());
    logstashEvent.put("thread_name", threadName);
    return logstashEvent;
}
Also used : HashMap(java.util.HashMap) ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) Map(java.util.Map) HashMap(java.util.HashMap) LocationInfo(org.apache.log4j.spi.LocationInfo)

Example 3 with LocationInfo

use of org.apache.log4j.spi.LocationInfo in project logging-log4j2 by apache.

the class PropertyRewritePolicy method rewrite.

/**
 * {@inheritDoc}
 */
public LoggingEvent rewrite(final LoggingEvent source) {
    if (!properties.isEmpty()) {
        Map<String, String> rewriteProps = source.getProperties() != null ? new HashMap<>(source.getProperties()) : new HashMap<>();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            if (!rewriteProps.containsKey(entry.getKey())) {
                rewriteProps.put(entry.getKey(), entry.getValue());
            }
        }
        LogEvent event;
        if (source instanceof LogEventAdapter) {
            event = new Log4jLogEvent.Builder(((LogEventAdapter) source).getEvent()).setContextData(new SortedArrayStringMap(rewriteProps)).build();
        } else {
            LocationInfo info = source.getLocationInformation();
            StackTraceElement element = new StackTraceElement(info.getClassName(), info.getMethodName(), info.getFileName(), Integer.parseInt(info.getLineNumber()));
            Thread thread = getThread(source.getThreadName());
            long threadId = thread != null ? thread.getId() : 0;
            int threadPriority = thread != null ? thread.getPriority() : 0;
            event = Log4jLogEvent.newBuilder().setContextData(new SortedArrayStringMap(rewriteProps)).setLevel(OptionConverter.convertLevel(source.getLevel())).setLoggerFqcn(source.getFQNOfLoggerClass()).setMarker(null).setMessage(new SimpleMessage(source.getRenderedMessage())).setSource(element).setLoggerName(source.getLoggerName()).setThreadName(source.getThreadName()).setThreadId(threadId).setThreadPriority(threadPriority).setThrown(source.getThrowableInformation().getThrowable()).setTimeMillis(source.getTimeStamp()).setNanoTime(0).setThrownProxy(null).build();
        }
        return new LogEventAdapter(event);
    }
    return source;
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LocationInfo(org.apache.log4j.spi.LocationInfo) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEventAdapter(org.apache.log4j.bridge.LogEventAdapter) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with LocationInfo

use of org.apache.log4j.spi.LocationInfo in project commons by twitter.

the class JULBridgeHandlerTest method checkToLoggingEvent.

@Test
public void checkToLoggingEvent() {
    LogRecord record = new LogRecord(Level.FINEST, "test is {0}");
    record.setParameters(new Object[] { "successful" });
    record.setThreadID(42);
    Throwable t = new Throwable();
    record.setThrown(t);
    // source class and method names are usually inferred, but because there's no JUL in the stack
    // frame, it won't work as expected.
    record.setSourceClassName(getClass().getName());
    record.setSourceMethodName("checkToLoggingEvent");
    Logger log4jLogger = new JULBridgeHandler().getLogger(record);
    org.apache.log4j.Level log4jLevel = JULBridgeLevelConverter.toLog4jLevel(Level.FINEST);
    LoggingEvent event = JULBridgeHandler.toLoggingEvent(record, log4jLogger, log4jLevel, false);
    assertThat(event.getLogger(), is((Category) log4jLogger));
    assertThat(event.getLevel(), is(log4jLevel));
    assertThat(event.getMessage(), is((Object) "test is successful"));
    assertThat(event.getThreadName(), is("42"));
    assertThat(event.getTimeStamp(), is(record.getMillis()));
    assertThat(event.getThrowableInformation().getThrowable(), is(sameInstance(t)));
    LocationInfo info = event.getLocationInformation();
    assertThat(info.getClassName(), is(getClass().getName()));
    assertThat(info.getMethodName(), is("checkToLoggingEvent"));
}
Also used : JULBridgeHandler(com.twitter.common.logging.julbridge.JULBridgeHandler) LoggingEvent(org.apache.log4j.spi.LoggingEvent) Category(org.apache.log4j.Category) LogRecord(java.util.logging.LogRecord) Logger(org.apache.log4j.Logger) LocationInfo(org.apache.log4j.spi.LocationInfo) Test(org.junit.Test)

Example 5 with LocationInfo

use of org.apache.log4j.spi.LocationInfo in project commons by twitter.

the class JULBridgeHandler method toLoggingEvent.

/**
 * Converts a JUL log record into a Log4J logging event.
 *
 * @param record the JUL log record to convert
 * @param logger the Log4J logger to use for the logging event
 * @param level the Log4J level to use for the logging event
 * @param useExtendedLocationInfo if false, do no try to get source file and line informations
 * @return a Log4J logging event
 */
static LoggingEvent toLoggingEvent(LogRecord record, Logger logger, Level level, boolean useExtendedLocationInfo) {
    LocationInfo locationInfo = useExtendedLocationInfo ? new LocationInfo(new Throwable(), record.getSourceClassName()) : new LocationInfo("?", record.getSourceClassName(), record.getSourceMethodName(), "?");
    // Getting thread name from thread id? complicated...
    String threadName = String.valueOf(record.getThreadID());
    ThrowableInformation throwableInformation = record.getThrown() == null ? null : new ThrowableInformation(record.getThrown());
    return new LoggingEvent(record.getSourceClassName(), logger, record.getMillis(), level, formatMessage(record), threadName, throwableInformation, null, /* ndc */
    locationInfo, null);
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) LocationInfo(org.apache.log4j.spi.LocationInfo)

Aggregations

LocationInfo (org.apache.log4j.spi.LocationInfo)11 ThrowableInformation (org.apache.log4j.spi.ThrowableInformation)5 Map (java.util.Map)4 Level (org.apache.log4j.Level)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 LogRecord (java.util.logging.LogRecord)2 Category (org.apache.log4j.Category)2 LogEventAdapter (org.apache.log4j.bridge.LogEventAdapter)2 LoggingEvent (org.apache.log4j.spi.LoggingEvent)2 LogEvent (org.apache.logging.log4j.core.LogEvent)2 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)2 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)2 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)2 JULBridgeHandler (com.twitter.common.logging.julbridge.JULBridgeHandler)1 LogEvent (io.fabric8.insight.log.LogEvent)1 Logger (org.apache.log4j.Logger)1 MapMessage (org.apache.logging.log4j.message.MapMessage)1