/** @see org.apache.log4j.spi.Filter#decide(org.apache.log4j.spi.LoggingEvent) */ @Override public int decide(LoggingEvent event) { if (nestedDiagnosticContext.equals(event.getNDC())) { return ACCEPT; } return DENY; }
@Override public String format(LoggingEvent event) { if (sbuf.capacity() > MAX_CAPACITY) { sbuf = new StringBuffer(BUF_SIZE); } else { sbuf.setLength(0); } sbuf.append(Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP); sbuf.append("<td title=\"Level\">"); if (event.getLevel().equals(Level.DEBUG)) { sbuf.append("<font color=\"#339933\">"); sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel()))); sbuf.append("</font>"); } else if (event.getLevel().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); 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(); }
public static String extractFileInformation(LoggingEvent loggingEvent) { String ndc = loggingEvent.getNDC(); if (ndc != null && ndc.lastIndexOf("file:") != -1) { System.out.println(ndc); ndc = ndc.substring(ndc.lastIndexOf("file:")).replaceAll("file:|]|\\[sect:ASSAYS", "").trim(); return ndc; } return null; }
/** * 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; }
/** * Creates a new <code>EventDetails</code> instance. * * @param aEvent a <code>LoggingEvent</code> value */ EventDetails(LoggingEvent aEvent) { this( aEvent.timeStamp, aEvent.getLevel(), aEvent.getLoggerName(), aEvent.getNDC(), aEvent.getThreadName(), aEvent.getRenderedMessage(), aEvent.getThrowableStrRep(), (aEvent.getLocationInformation() == null) ? null : aEvent.getLocationInformation().fullInfo); }
/* */ public String convert(LoggingEvent event) /* */ { /* 393 */ switch (this.type) { /* */ case 2000: /* 395 */ return Long.toString(event.timeStamp - LoggingEvent.getStartTime()); /* */ case 2001: /* 397 */ return event.getThreadName(); /* */ case 2002: /* 399 */ return event.getLevel().toString(); /* */ case 2003: /* 401 */ return event.getNDC(); /* */ case 2004: /* 403 */ return event.getRenderedMessage(); /* */ } /* 405 */ return null; /* */ }
public String convert(LoggingEvent event) { switch (type) { case RELATIVE_TIME_CONVERTER: return (String.valueOf(event.timeStamp - LoggingEvent.getStartTime())); case THREAD_CONVERTER: return event.getThreadName(); case LEVEL_CONVERTER: return event.getLevel().toString(); case NDC_CONVERTER: return event.getNDC(); case MESSAGE_CONVERTER: { return event.getRenderedMessage(); } default: return null; } }
public static final GelfMessage makeMessage(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 = event.getRenderedMessage(); String shortMessage; if (renderedMessage == null) { renderedMessage = ""; } if (renderedMessage.length() > MAX_SHORT_MESSAGE_LENGTH) { shortMessage = renderedMessage.substring(0, MAX_SHORT_MESSAGE_LENGTH - 1); } else { shortMessage = renderedMessage; } if (provider.isExtractStacktrace()) { ThrowableInformation throwableInformation = event.getThrowableInformation(); if (throwableInformation != null) { renderedMessage += "\n\r" + extractStacktrace(throwableInformation); } } 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 = MDC.getContext(); if (mdc != null) { for (Map.Entry<String, Object> entry : mdc.entrySet()) { gelfMessage.addField(entry.getKey(), entry.getValue().toString()); } } // Get NDC and add a GELF field String ndc = event.getNDC(); if (ndc != null) { gelfMessage.addField(LOGGER_NDC, ndc); } } return gelfMessage; }
/*(non-Javadoc) * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent) */ protected void append(LoggingEvent event) { String ndcStr = null; int depth = -1; { // IMPORTANT: this section is something MUST be called! ndcStr = event.getNDC(); depth = NDC.getDepth(); event.getThreadName(); // Get a copy of this thread's MDC. event.getMDCCopy(); } if (shallRecursionPrevented(this.getClass(), event.getLoggerName()) || shallRecursionPrevented(ZMLog.class, event.getLoggerName())) return; { // IMPORTANT: this section is something MUST be called! event.getRenderedMessage(); event.getThrowableStrRep(); /* * SCENARIO: * while tl!=started, we suppose to start a new tl. * log4j shoulden't terminate other's tl, it can only terminate it's selves. * * depth=0, tl==started, controlBySelf [complete] this tl is reaching it's end, should be terminated. * depth=0, tl==started, controlByOthers [recording] this tl is controlled by others, log4j is simply an interceptor, do recording. * * depth=0, tl!=started, controlBySelf [doNothing] because tl can only be started while depth>0. * depth=0, tl!=started, controlByOthers [doNothing] because tl can only be started while depth>0. * * depth>0, tl==started, controlBySelf [recording] watch out the depth problem. * depth>0, tl==started, controlByOthers [recording] watch out the depth problem. * * depth>0, tl!=started, controlBySelf [do Start] * depth>0, tl!=started, controlByOthers [do Start] * */ TimelineLifecycle lfc = ZMonitorManager.getInstance().getTimelineLifecycle(); String mesg = event.getRenderedMessage(); if (depth == 0) { if (ZMonitor.isTimelineStarted()) { if (isControlledBySelf(lfc)) { complete(event, mesg, lfc); } else { record(event, depth, lfc, ndcStr); } } // do nothing... // tl.start must satisfy (depth > 0), otherwise there's no way for appender to know when to // complete timeline. } else { if (ZMonitor.isTimelineStarted()) { record(event, depth, lfc, ndcStr); } else { start(event, depth, lfc, ndcStr); setControlledBySelf(lfc); } } } }
protected void append(LoggingEvent event) { Connection connection = null; try { connection = connectionSource.getConnection(); connection.setAutoCommit(false); PreparedStatement insertStatement; if (cnxSupportsGetGeneratedKeys) { insertStatement = connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS); } else { insertStatement = connection.prepareStatement(insertSQL); } /* insertStatement.setLong(1, event.getSequenceNumber());*/ insertStatement.setLong(1, 0); insertStatement.setLong(2, event.getTimeStamp()); insertStatement.setString(3, event.getRenderedMessage()); insertStatement.setString(4, event.getLoggerName()); insertStatement.setString(5, event.getLevel().toString()); insertStatement.setString(6, event.getNDC()); insertStatement.setString(7, event.getThreadName()); insertStatement.setShort(8, DBHelper.computeReferenceMask(event)); LocationInfo li; if (event.locationInformationExists() || locationInfo) { li = event.getLocationInformation(); } else { li = LocationInfo.NA_LOCATION_INFO; } insertStatement.setString(9, li.getFileName()); insertStatement.setString(10, li.getClassName()); insertStatement.setString(11, li.getMethodName()); insertStatement.setString(12, li.getLineNumber()); int updateCount = insertStatement.executeUpdate(); if (updateCount != 1) { LogLog.warn("Failed to insert loggingEvent"); } ResultSet rs = null; Statement idStatement = null; boolean gotGeneratedKeys = false; if (cnxSupportsGetGeneratedKeys) { try { rs = (ResultSet) GET_GENERATED_KEYS_METHOD.invoke(insertStatement, null); gotGeneratedKeys = true; } catch (InvocationTargetException ex) { Throwable target = ex.getTargetException(); if (target instanceof SQLException) { throw (SQLException) target; } throw ex; } catch (IllegalAccessException ex) { LogLog.warn("IllegalAccessException invoking PreparedStatement.getGeneratedKeys", ex); } } if (!gotGeneratedKeys) { insertStatement.close(); insertStatement = null; idStatement = connection.createStatement(); idStatement.setMaxRows(1); rs = idStatement.executeQuery(sqlDialect.getSelectInsertId()); } // A ResultSet cursor is initially positioned before the first row; the // first call to the method next makes the first row the current row rs.next(); int eventId = rs.getInt(1); rs.close(); // we no longer need the insertStatement if (insertStatement != null) { insertStatement.close(); insertStatement = null; } if (idStatement != null) { idStatement.close(); idStatement = null; } Set propertiesKeys = event.getPropertyKeySet(); if (propertiesKeys.size() > 0) { PreparedStatement insertPropertiesStatement = connection.prepareStatement(insertPropertiesSQL); for (Iterator i = propertiesKeys.iterator(); i.hasNext(); ) { String key = (String) i.next(); String value = event.getProperty(key); // LogLog.info("id " + eventId + ", key " + key + ", value " + value); insertPropertiesStatement.setInt(1, eventId); insertPropertiesStatement.setString(2, key); insertPropertiesStatement.setString(3, value); if (cnxSupportsBatchUpdates) { insertPropertiesStatement.addBatch(); } else { insertPropertiesStatement.execute(); } } if (cnxSupportsBatchUpdates) { insertPropertiesStatement.executeBatch(); } insertPropertiesStatement.close(); insertPropertiesStatement = null; } String[] strRep = event.getThrowableStrRep(); if (strRep != null) { LogLog.debug("Logging an exception"); PreparedStatement insertExceptionStatement = connection.prepareStatement(insertExceptionSQL); for (short i = 0; i < strRep.length; i++) { insertExceptionStatement.setInt(1, eventId); insertExceptionStatement.setShort(2, i); insertExceptionStatement.setString(3, strRep[i]); if (cnxSupportsBatchUpdates) { insertExceptionStatement.addBatch(); } else { insertExceptionStatement.execute(); } } if (cnxSupportsBatchUpdates) { insertExceptionStatement.executeBatch(); } insertExceptionStatement.close(); insertExceptionStatement = null; } connection.commit(); } catch (Throwable sqle) { LogLog.error("problem appending event", sqle); } finally { DBHelper.closeConnection(connection); } }