@Test public void testException() throws Throwable { Exception e = new NoRouteToHostException("that box caught fire 3 years ago"); ThrowableInformation ti = new ThrowableInformation(e); Log4Json l4j = new Log4Json(); long timeStamp = Time.now(); String outcome = l4j.toJson(new StringWriter(), "testException", timeStamp, "INFO", "quoted\"", "new line\n and {}", ti) .toString(); println("testException", outcome); }
private String getLoggingMesage( LoggingEvent event ) { Throwable throwable = null; ThrowableInformation throwableInfo = event.getThrowableInformation(); if (throwableInfo != null && throwableInfo.getThrowable() != null) { // logging through methods like error(new Exception); throwable = throwableInfo.getThrowable(); } else if (event.getMessage() instanceof Throwable) { // logging through methods like error("some message", new Exception); throwable = (Throwable) event.getMessage(); } // first format the message using the layout String message = layout.format(event); // then append the exception stack trace if (throwable != null) { message = getExceptionMsg(throwable, message); } return message; }
/** * Extracts Stack trace of Throwable contained in LogginEvent, if there is * any * * @param aLoggingEvent * logging event * @return stack trace of throwable */ public String getThrowableRepresentationFromLoggingEvent(LoggingEvent aLoggingEvent) { // extract throwable information from loggingEvent if available ThrowableInformation throwableinfo = aLoggingEvent.getThrowableInformation(); StringBuffer throwableStringBuffer = new StringBuffer(); if (throwableinfo != null) { String[] lines = throwableinfo.getThrowableStrRep(); for (int index = 0; index < lines.length; index++) { throwableStringBuffer = (StringBuffer) throwableStringBuffer.append(lines[index] + "\r\n"); } } String result = quotedString(throwableStringBuffer.toString()); if (this.getThrowableMaxChars() != -1 && result.length() > this.getThrowableMaxChars()) { result = result.substring(0, this.getThrowableMaxChars() - 1); } return result; }
protected void alarm(LoggingEvent event, String level) { String message = event.getRenderedMessage(); Throwable t = null; { ThrowableInformation throwableInformation = event.getThrowableInformation(); if (throwableInformation != null) { t = throwableInformation.getThrowable(); } } try { // this.sendToRobot(message, t); this.robotService.errorlog(level, message, t); } catch (Exception e) { e.printStackTrace(); } }
@Override protected void append(final LoggingEvent loggingEvent) { final StringBuilder buf = new StringBuilder(); buf.append(getLayout().format(loggingEvent)); final ThrowableInformation ti = loggingEvent.getThrowableInformation(); if (ti != null) { final String[] cause = ti.getThrowableStrRep(); for (final String line : cause) { buf.append(line).append('\n'); } } j2DClient.get().addEventLine(new HeaderLessEventLine(buf.toString(), NotificationType.CLIENT)); }
/** * {@inheritDoc} */ public void format(final LoggingEvent event, final StringBuffer toAppendTo) { if (maxLines != 0) { ThrowableInformation information = event.getThrowableInformation(); if (information != null) { String[] stringRep = information.getThrowableStrRep(); int length = stringRep.length; if (maxLines < 0) { length += maxLines; } else if (length > maxLines) { length = maxLines; } for (int i = 0; i < length; i++) { String string = stringRep[i]; toAppendTo.append(string).append("\n"); } } } }
@BeforeClass public static void setupBeforeClass() throws Exception { Logger xenqtt = Logger.getLogger("xenqtt"); assertNotNull(xenqtt); Appender console = Logger.getRootLogger().getAppender("console"); assertNotNull(console); console.addFilter(new Filter() { @Override public int decide(LoggingEvent event) { entries.add(event.getRenderedMessage()); ThrowableInformation info = event.getThrowableInformation(); if (info != null) { Throwable t = info.getThrowable(); if (t != null) { entries.add(t.getMessage()); } } return Filter.ACCEPT; } }); }
/** * @see com.stackify.api.common.log.EventAdapter#getThrowable(java.lang.Object) */ @Override public Throwable getThrowable(final LoggingEvent event) { ThrowableInformation throwableInfo = event.getThrowableInformation(); if (throwableInfo != null) { Throwable t = throwableInfo.getThrowable(); if (t != null) { return t; } } Object message = event.getMessage(); if (message != null) { if (message instanceof Throwable) { return (Throwable) message; } } return null; }
/** * Prepares the LoggingEvent for beeing processes in another thread, by saving away the NDC, MDC, thread name, * throwable information and location information (if parameter getLocationInformation is true). * * @param loggingEvent the logging event to prepare. * @param getLocationInformation flag indicating if location information should be fetched for the logging event. * * @since 2.0 */ public static void prepareLoggingEvent(final LoggingEvent loggingEvent, final boolean getLocationInformation) { // Set the NDC and thread name for the calling thread as these LoggingEvent fields were not set at event creation time. loggingEvent.getNDC(); loggingEvent.getThreadName(); // Get a copy of this thread's MDC. loggingEvent.getMDCCopy(); ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation(); if( throwableInformation != null ) { // Make the ThrowableInformation of the LoggingEvent store away the representation of the stack trace. throwableInformation.getThrowableStrRep(); } if( getLocationInformation ) { loggingEvent.getLocationInformation(); } }
public String convert(LoggingEvent event) { ThrowableInformation throwableInformation = event.getThrowableInformation(); if(throwableInformation != null) { Throwable error = throwableInformation.getThrowable(); if( error != null ) { StringWriter strWriter = new StringWriter(); error.printStackTrace(new PrintWriter(strWriter)); return strWriter.toString().replaceAll("\\s+", " "); } } return ""; }
public static String getThrowable(LoggingEvent le) { ThrowableInformation tinfo = le.getThrowableInformation(); Throwable t = null; try { t = tinfo.getThrowable(); } catch (Exception ex) { } if (t == null) { return ""; } java.io.StringWriter stringWriter; stringWriter = new java.io.StringWriter(); t.printStackTrace(new java.io.PrintWriter(stringWriter)); return stringWriter.getBuffer().toString(); }
/** * Pull out details of exception in a Hashmap (if they exist) * * @param loggingEvent * @return */ protected void exceptionInformation( final LoggingEvent loggingEvent) { if (loggingEvent.getThrowableInformation() != null) { final ThrowableInformation throwableInformation = loggingEvent .getThrowableInformation(); if (throwableInformation.getThrowable().getClass() .getCanonicalName() != null) { ExceptionField.put(EXCEPTION.CLASS, throwableInformation.getThrowable().getClass() .getCanonicalName()); } if (throwableInformation.getThrowable().getMessage() != null) { ExceptionField.put(EXCEPTION.MESSAGE, throwableInformation.getThrowable().getMessage()); } if (throwableInformation.getThrowableStrRep() != null) { final String stackTrace = StringUtils.join( throwableInformation.getThrowableStrRep(), "\n"); ExceptionField.put(EXCEPTION.STACKTRACE, stackTrace); } } }
/** * Convert an event to JSON * * @param writer the destination writer * @param event the event -must not be null * @return the writer * @throws IOException on problems generating the JSON */ public Writer toJson(final Writer writer, final LoggingEvent event) throws IOException { ThrowableInformation ti = event.getThrowableInformation(); toJson(writer, event.getLoggerName(), event.getTimeStamp(), event.getLevel().toString(), event.getThreadName(), event.getRenderedMessage(), ti); return writer; }
/** * Build a JSON entry from the parameters. This is public for testing. * * @param writer destination * @param loggerName logger name * @param timeStamp time_t value * @param level level string * @param threadName name of the thread * @param message rendered message * @param ti nullable thrown information * @return the writer * @throws IOException on any problem */ public Writer toJson(final Writer writer, final String loggerName, final long timeStamp, final String level, final String threadName, final String message, final ThrowableInformation ti) throws IOException { JsonGenerator json = factory.createJsonGenerator(writer); json.writeStartObject(); json.writeStringField(NAME, loggerName); json.writeNumberField(TIME, timeStamp); Date date = new Date(timeStamp); json.writeStringField(DATE, dateFormat.format(date)); json.writeStringField(LEVEL, level); json.writeStringField(THREAD, threadName); json.writeStringField(MESSAGE, message); if (ti != null) { //there is some throwable info, but if the log event has been sent over the wire, //there may not be a throwable inside it, just a summary. Throwable thrown = ti.getThrowable(); String eclass = (thrown != null) ? thrown.getClass().getName() : ""; json.writeStringField(EXCEPTION_CLASS, eclass); String[] stackTrace = ti.getThrowableStrRep(); json.writeArrayFieldStart(STACK); for (String row : stackTrace) { json.writeString(row); } json.writeEndArray(); } json.writeEndObject(); json.flush(); json.close(); return writer; }
@Test public void testNestedException() throws Throwable { Exception e = new NoRouteToHostException("that box caught fire 3 years ago"); Exception ioe = new IOException("Datacenter problems", e); ThrowableInformation ti = new ThrowableInformation(ioe); Log4Json l4j = new Log4Json(); long timeStamp = Time.now(); String outcome = l4j.toJson(new StringWriter(), "testNestedException", timeStamp, "INFO", "quoted\"", "new line\n and {}", ti) .toString(); println("testNestedException", outcome); ContainerNode rootNode = Log4Json.parse(outcome); assertEntryEquals(rootNode, Log4Json.LEVEL, "INFO"); assertEntryEquals(rootNode, Log4Json.NAME, "testNestedException"); assertEntryEquals(rootNode, Log4Json.TIME, timeStamp); assertEntryEquals(rootNode, Log4Json.EXCEPTION_CLASS, ioe.getClass().getName()); JsonNode node = assertNodeContains(rootNode, Log4Json.STACK); assertTrue("Not an array: " + node, node.isArray()); node = assertNodeContains(rootNode, Log4Json.DATE); assertTrue("Not a string: " + node, node.isTextual()); //rather than try and make assertions about the format of the text //message equalling another ISO date, this test asserts that the hypen //and colon characters are in the string. String dateText = node.getTextValue(); assertTrue("No '-' in " + dateText, dateText.contains("-")); assertTrue("No '-' in " + dateText, dateText.contains(":")); }
public void appendersCalled(LoggingEvent event) { ThrowableInformation throwableInfo = event.getThrowableInformation(); String throwableName = null; if (throwableInfo != null) { Throwable throwable = throwableInfo.getThrowable(); throwableName = (throwable != null) ? throwable.getClass().getName() : null; } LogEventTracker.LogLevel level = LogEventTracker.LogLevel.valueOf(event.getLevel().toString()); tracker.track(level, (throwableInfo != null), throwableName); }
public int countExceptionsWithMessage(final String text) { int count = 0; for (LoggingEvent e: getLog()) { ThrowableInformation t = e.getThrowableInformation(); if (t != null) { String m = t.getThrowable().getMessage(); if (m.contains(text)) { count++; } } } return count; }
@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"; }