/** * 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; }
public String convert(LoggingEvent event) { LocationInfo locationInfo = event.getLocationInformation(); switch (type) { case FULL_LOCATION_CONVERTER: return locationInfo.fullInfo; case METHOD_LOCATION_CONVERTER: return locationInfo.getMethodName(); case LINE_LOCATION_CONVERTER: return locationInfo.getLineNumber(); case FILE_LOCATION_CONVERTER: return locationInfo.getFileName(); default: return null; } }
/* */ public String convert(LoggingEvent event) /* */ { /* 483 */ LocationInfo locationInfo = event.getLocationInformation(); /* 484 */ switch (this.type) { /* */ case 1000: /* 486 */ return locationInfo.fullInfo; /* */ case 1001: /* 488 */ return locationInfo.getMethodName(); /* */ case 1003: /* 490 */ return locationInfo.getLineNumber(); /* */ case 1004: /* 492 */ return locationInfo.getFileName(); /* 493 */ case 1002: } return null; /* */ }
private ErrorEventStat convert(LoggingEvent loggingEvent) { ErrorEventStat errorEventStat = new ErrorEventStat(); LocationInfo locationInformation = loggingEvent.getLocationInformation(); errorEventStat.setEmail(email); errorEventStat.setClientVersion(getArtifactId()); errorEventStat.setOs(System.getProperty(SystemConstants.OS_NAME_PROP_KEY)); errorEventStat.setOsVersion(System.getProperty(SystemConstants.OS_VERSION_PROP_KEY)); errorEventStat.setJvm(System.getProperty(SystemConstants.JAVA_VERSION_PROP_KEY)); errorEventStat.setThreadName(loggingEvent.getThreadName()); errorEventStat.setMessage(loggingEvent.getRenderedMessage()); errorEventStat.setFileName(locationInformation.getFileName()); errorEventStat.setLineNumber(locationInformation.getLineNumber()); errorEventStat.setMethodName(locationInformation.getMethodName()); errorEventStat.setClassName(locationInformation.getClassName()); ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation(); if (throwableInformation != null) { errorEventStat.setStackTrace(throwableInformation.getThrowable().getMessage()); } return errorEventStat; }
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; }
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); } }