Java Code Examples for org.apache.log4j.spi.LoggingEvent#getProperties()
The following examples show how to use
org.apache.log4j.spi.LoggingEvent#getProperties() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleDiagnosticsAppender.java From samza with Apache License 2.0 | 6 votes |
@Override protected void append(LoggingEvent loggingEvent) { try { // if an event with a non-null throwable is received => exception event if (loggingEvent.getThrowableInformation() != null) { DiagnosticsExceptionEvent diagnosticsExceptionEvent = new DiagnosticsExceptionEvent(loggingEvent.timeStamp, loggingEvent.getThrowableInformation().getThrowable(), loggingEvent.getProperties()); diagnosticsManager.addExceptionEvent(diagnosticsExceptionEvent); } } catch (Exception e) { // blanket catch of all exceptions so as to not impact any job System.err.println("Exception in logging event parsing " + e); } }
Example 2
Source File: LoggerUtilTest.java From attic-apex-core with Apache License 2.0 | 5 votes |
@Override public void append(LoggingEvent event) { mdcProperties = event.getProperties(); lastMessage = event.getRenderedMessage(); level = event.getLevel(); }
Example 3
Source File: Log4JAppenderWrapper.java From mrgeo with Apache License 2.0 | 5 votes |
@Override public void doAppend(LoggingEvent event) { assert (wrappedAppender != null); String msg = event.getMessage().toString(); Matcher m = crlf.matcher(msg); if (m.find()) { String lines[] = msg.split("\r\n|\n|\r"); //msg.split("\\r?\\n"); for (String line : lines) { String clean = "(Encoded) " + line; LoggingEvent encoded = new LoggingEvent(event.getFQNOfLoggerClass(), event.getLogger(), event.getTimeStamp(), event.getLevel(), clean, event.getThreadName(), event.getThrowableInformation(), event.getNDC(), event.getLocationInformation(), event.getProperties()); wrappedAppender.doAppend(encoded); } } else { wrappedAppender.doAppend(event); } }
Example 4
Source File: LoghubAppender.java From aliyun-log-log4j-appender with Apache License 2.0 | 4 votes |
@Override protected void append(LoggingEvent event) { LogItem logItem = new LogItem(); logItem.SetTime((int) (event.getTimeStamp() / 1000)); DateTime dateTime = new DateTime(event.getTimeStamp()); logItem.PushBack("time", dateTime.toString(formatter)); logItem.PushBack("level", event.getLevel().toString()); logItem.PushBack("thread", event.getThreadName()); logItem.PushBack("location", event.getLocationInformation().fullInfo); Object message = event.getMessage(); if(message==null){ logItem.PushBack("message", ""); }else{ logItem.PushBack("message", event.getMessage().toString()); } String throwable = getThrowableStr(event); if (throwable != null) { logItem.PushBack("throwable", throwable); } if (getLayout() != null) { logItem.PushBack("log", getLayout().format(event)); } Map properties = event.getProperties(); if (properties.size() > 0) { Object[] keys = properties.keySet().toArray(); Arrays.sort(keys); for (int i = 0; i < keys.length; i++) { logItem.PushBack(keys[i].toString(), properties.get(keys[i]) .toString()); } } try { producer.send(projectConfig.getProject(), logStore, topic, source, logItem, new LoghubAppenderCallback(projectConfig.getProject(), logStore, topic, source, logItem)); } catch (Exception e) { LogLog.error( "Failed to send log, project=" + project + ", logStore=" + logStore + ", topic=" + topic + ", source=" + source + ", logItem=" + logItem, e); } }
Example 5
Source File: StreamAppender.java From samza with Apache License 2.0 | 4 votes |
private LoggingEvent subLog(LoggingEvent event) { return new LoggingEvent(event.getFQNOfLoggerClass(), event.getLogger(), event.getTimeStamp(), event.getLevel(), subAppend(event), event.getThreadName(), event.getThrowableInformation(), event.getNDC(), event.getLocationInformation(), event.getProperties()); }
Example 6
Source File: LoggingEventJsonSerde.java From samza with Apache License 2.0 | 4 votes |
/** * 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; }
Example 7
Source File: Log4jUtil.java From otroslogviewer with Apache License 2.0 | 4 votes |
public static LogData translateLog4j(LoggingEvent event) { LogData ld = new LogData(); ld.setDate(new Date(event.getTimeStamp())); StringBuilder sb = new StringBuilder(); sb.append(event.getMessage()); if (event.getThrowableInformation() != null) { String[] throwableStrRep = event.getThrowableInformation().getThrowableStrRep(); for (String string : throwableStrRep) { sb.append('\n'); sb.append(string); } } ld.setMessage(sb.toString().trim()); ld.setLevel(parseLevel(event.getLevel().toString())); ld.setClazz(event.getLocationInformation().getClassName()); ld.setMethod(event.getLocationInformation().getMethodName()); ld.setFile(event.getLocationInformation().getFileName()); ld.setLine(event.getLocationInformation().getLineNumber()); ld.setNDC(event.getNDC()); ld.setThread(event.getThreadName()); ld.setLoggerName(event.getLoggerName()); ld.setProperties(IMMUTABLE_EMPTY_MAP); Map properties = event.getProperties(); if (properties != null) { Map<String, String> props = new HashMap<>(properties.size()); for (Object key : properties.keySet()) { String value = Optional .ofNullable(properties.get(key)) .map(Object::toString) .orElse(""); if (StringUtils.isNotBlank(value)) { props.put(key.toString(), value); } } if (props.size() > 0) { ld.setProperties(props); } } return ld; }
Example 8
Source File: CollectAppender.java From knox with Apache License 2.0 | 4 votes |
@Override protected void append( LoggingEvent event ) { event.getProperties(); queue.add( event ); }
Example 9
Source File: PropertyRewritePolicy.java From logging-log4j2 with Apache License 2.0 | 4 votes |
/** * {@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; }
Example 10
Source File: LogEventWrapper.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public LogEventWrapper(LoggingEvent event) { this.event = event; this.contextData = new ContextDataMap(event.getProperties()); this.contextStack = new MutableThreadContextStack(NDC.cloneStack()); }