Search in sources :

Example 6 with LocationInfo

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

the class PurgeUtilityHTMLLayout 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=\"Purge File/Folder\">");
    sbuf.append(Transform.escapeTags(MDC.get(PurgeUtilityLog.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(MDC.get(PurgeUtilityLogger.CODE_LINE)));
        // 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 7 with LocationInfo

use of org.apache.log4j.spi.LocationInfo in project xian by happyyangyuan.

the class GelfMessageFactory method makeMessage.

@SuppressWarnings("unchecked")
public static GelfMessage makeMessage(Layout layout, LoggingEvent event, GelfMessageProvider provider) {
    long timeStamp = Log4jVersionChecker.getTimeStamp(event);
    Level level = event.getLevel();
    String file = null;
    String lineNumber = null;
    if (provider.isIncludeLocation()) {
        LocationInfo locationInformation = event.getLocationInformation();
        file = locationInformation.getFileName();
        lineNumber = locationInformation.getLineNumber();
    }
    String renderedMessage = layout != null ? layout.format(event) : event.getRenderedMessage();
    String shortMessage;
    if (renderedMessage == null) {
        renderedMessage = "";
    }
    if (provider.isExtractStacktrace()) {
        ThrowableInformation throwableInformation = event.getThrowableInformation();
        if (throwableInformation != null) {
            String stackTrace = extractStacktrace(throwableInformation);
            if (stackTrace != null) {
                renderedMessage += "\n\r" + extractStacktrace(throwableInformation);
            }
        }
    }
    if (renderedMessage.length() > MAX_SHORT_MESSAGE_LENGTH) {
        shortMessage = renderedMessage.substring(0, MAX_SHORT_MESSAGE_LENGTH - 1);
    } else {
        shortMessage = renderedMessage;
    }
    GelfMessage gelfMessage = new GelfMessage(shortMessage, renderedMessage, timeStamp, String.valueOf(level.getSyslogEquivalent()), lineNumber, file);
    if (provider.getOriginHost() != null) {
        gelfMessage.setHost(provider.getOriginHost());
    }
    if (provider.getFacility() != null) {
        gelfMessage.setFacility(provider.getFacility());
    }
    Map<String, String> fields = provider.getFields();
    for (Map.Entry<String, String> entry : fields.entrySet()) {
        if (entry.getKey().equals(ORIGIN_HOST_KEY) && gelfMessage.getHost() == null) {
            gelfMessage.setHost(fields.get(ORIGIN_HOST_KEY));
        } else {
            gelfMessage.addField(entry.getKey(), entry.getValue());
        }
    }
    if (provider.isAddExtendedInformation()) {
        gelfMessage.addField(THREAD_NAME, event.getThreadName());
        gelfMessage.addField(LOGGER_NAME, event.getLoggerName());
        gelfMessage.addField(JAVA_TIMESTAMP, Long.toString(gelfMessage.getJavaTimestamp()));
        // Get MDC and add a GELF field for each key/value pair
        Map<String, Object> mdc = event.getProperties();
        if (mdc != null) {
            for (Map.Entry<String, Object> entry : mdc.entrySet()) {
                Object value = provider.transformExtendedField(entry.getKey(), entry.getValue());
                gelfMessage.addField(entry.getKey(), value);
            }
        }
        // Get NDC and add a GELF field
        String ndc = event.getNDC();
        if (ndc != null) {
            gelfMessage.addField(LOGGER_NDC, ndc);
        }
    }
    return gelfMessage;
}
Also used : ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) Level(org.apache.log4j.Level) Map(java.util.Map) LocationInfo(org.apache.log4j.spi.LocationInfo)

Example 8 with LocationInfo

use of org.apache.log4j.spi.LocationInfo in project fabric8 by jboss-fuse.

the class Log4jLogQuery method toLogEvent.

protected LogEvent toLogEvent(LoggingEvent element) {
    LogEvent answer = new LogEvent();
    answer.setClassName(element.getFQNOfLoggerClass());
    // TODO
    // answer.setContainerName(element.get);
    ThrowableInformation throwableInformation = element.getThrowableInformation();
    if (throwableInformation != null) {
        ThrowableFormatter renderer = new ThrowableFormatter();
        String[] stack = renderer.doRender(throwableInformation.getThrowable());
        if (stack == null) {
            stack = element.getThrowableStrRep();
        }
        answer.setException(stack);
    }
    LocationInfo locationInformation = element.getLocationInformation();
    if (locationInformation != null) {
        answer.setFileName(locationInformation.getFileName());
        answer.setClassName(locationInformation.getClassName());
        answer.setMethodName(locationInformation.getMethodName());
        answer.setLineNumber(locationInformation.getLineNumber());
    }
    Level level = element.getLevel();
    if (level != null) {
        answer.setLevel(level.toString());
    }
    // TODO
    answer.setLogger(element.getLoggerName());
    Category logger = element.getLogger();
    Object message = element.getMessage();
    if (message != null) {
        // TODO marshal differently?
        answer.setMessage(message.toString());
    }
    answer.setProperties(element.getProperties());
    // TODO
    answer.setSeq(element.getTimeStamp());
    answer.setTimestamp(new Date(element.getTimeStamp()));
    answer.setThread(element.getThreadName());
    answer.setHost(getHostName());
    return answer;
}
Also used : Category(org.apache.log4j.Category) LogEvent(io.fabric8.insight.log.LogEvent) ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) Level(org.apache.log4j.Level) Date(java.util.Date) LocationInfo(org.apache.log4j.spi.LocationInfo)

Example 9 with LocationInfo

use of org.apache.log4j.spi.LocationInfo in project bender by Nextdoor.

the class BenderLayout method format.

@Override
public String format(LoggingEvent event) {
    BenderLogEntry entry = new BenderLogEntry();
    entry.threadName = event.getThreadName();
    entry.posixTimestamp = event.getTimeStamp();
    entry.timestamp = FORMATTER.print(entry.posixTimestamp);
    entry.message = event.getRenderedMessage();
    entry.level = event.getLevel().toString();
    entry.logger = event.getLogger().getName();
    entry.alias = ALIAS;
    entry.version = VERSION;
    if (event.getThrowableInformation() != null) {
        final ThrowableInformation throwableInfo = event.getThrowableInformation();
        ExceptionLog ex = new ExceptionLog();
        if (throwableInfo.getThrowable().getClass().getCanonicalName() != null) {
            ex.clazz = throwableInfo.getThrowable().getClass().getCanonicalName();
        }
        if (throwableInfo.getThrowable().getMessage() != null) {
            ex.message = throwableInfo.getThrowable().getMessage();
        }
        if (throwableInfo.getThrowableStrRep() != null) {
            Arrays.asList(throwableInfo.getThrowableStrRep()).forEach(m -> {
                ex.stacktrace.add(m.replaceAll("\\t", "   "));
            });
        }
        entry.exception = ex;
    }
    LocationInfo locinfo = event.getLocationInformation();
    entry.file = locinfo.getFileName();
    entry.lineNumber = Integer.parseInt(locinfo.getLineNumber());
    entry.method = locinfo.getMethodName();
    entry.clazz = locinfo.getClassName();
    return GSON.toJson(entry) + "\n";
}
Also used : ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) LocationInfo(org.apache.log4j.spi.LocationInfo)

Example 10 with LocationInfo

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

the class MapRewritePolicy method rewrite.

/**
 * {@inheritDoc}
 */
public LoggingEvent rewrite(final LoggingEvent source) {
    Object msg = source.getMessage();
    if (msg instanceof MapMessage || msg instanceof Map) {
        Map<String, String> props = source.getProperties() != null ? new HashMap<>(source.getProperties()) : new HashMap<>();
        @SuppressWarnings("unchecked") Map<String, Object> eventProps = msg instanceof Map ? (Map) msg : ((MapMessage) msg).getData();
        // 
        // if the map sent in the logging request
        // has "message" entry, use that as the message body
        // otherwise, use the entire map.
        // 
        Message newMessage = null;
        Object newMsg = eventProps.get("message");
        if (newMsg != null) {
            newMessage = new SimpleMessage(newMsg.toString());
            for (Map.Entry<String, Object> entry : eventProps.entrySet()) {
                if (!("message".equals(entry.getKey()))) {
                    props.put(entry.getKey(), entry.getValue().toString());
                }
            }
        } else {
            return source;
        }
        LogEvent event;
        if (source instanceof LogEventAdapter) {
            event = new Log4jLogEvent.Builder(((LogEventAdapter) source).getEvent()).setMessage(newMessage).setContextData(new SortedArrayStringMap(props)).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(props)).setLevel(OptionConverter.convertLevel(source.getLevel())).setLoggerFqcn(source.getFQNOfLoggerClass()).setMarker(null).setMessage(newMessage).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);
    } else {
        return source;
    }
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) MapMessage(org.apache.logging.log4j.message.MapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LocationInfo(org.apache.log4j.spi.LocationInfo) 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)

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