/** * This method differentiates RollingFileAppender from its super class. * * @since 0.9.0 */ @Override protected void subAppend(LoggingEvent event) { if (id == null) { id = "" + new Random().nextInt(); this.qw.write("#Session:" + id + ";" + Calendar.getInstance().getTime()); this.qw.write(Layout.LINE_SEP); } this.qw.write(id); this.qw.write("#"); this.qw.write(this.layout.format(event)); if (layout.ignoresThrowable()) { String[] s = event.getThrowableStrRep(); if (s != null) { Thread.dumpStack(); int len = s.length; for (int i = 0; i < len; i++) { this.qw.write(id + "#[" + event.getThreadName() + "] " + event.getLevel() + " - \t"); this.qw.write(s[i]); this.qw.write(Layout.LINE_SEP); } } } if (this.immediateFlush) { this.qw.flush(); } if (fileName != null && ((CountingQuietWriter) qw).getCount() >= maxFileSize) { this.rollOver(); } }
/* 245: */ /* 246: */ public void append(LoggingEvent event) /* 247: */ { /* 248:298 */ if (!isAsSevereAsThreshold(event.getLevel())) { /* 249:299 */ return; /* 250: */ } /* 251:302 */ if (this.sqw == null) /* 252: */ { /* 253:303 */ this.errorHandler.error( "No syslog host is set for SyslogAppedender named \"" + this.name + "\"."); /* 254: */ /* 255:305 */ return; /* 256: */ } /* 257:308 */ if (!this.layoutHeaderChecked) /* 258: */ { /* 259:309 */ if ((this.layout != null) && (this.layout.getHeader() != null)) { /* 260:310 */ sendLayoutMessage(this.layout.getHeader()); /* 261: */ } /* 262:312 */ this.layoutHeaderChecked = true; /* 263: */ } /* 264:315 */ String hdr = getPacketHeader(event.timeStamp); /* 265: */ String packet; /* 266: */ String packet; /* 267:317 */ if (this.layout == null) { /* 268:318 */ packet = String.valueOf(event.getMessage()); /* 269: */ } else { /* 270:320 */ packet = this.layout.format(event); /* 271: */ } /* 272:322 */ if ((this.facilityPrinting) || (hdr.length() > 0)) /* 273: */ { /* 274:323 */ StringBuffer buf = new StringBuffer(hdr); /* 275:324 */ if (this.facilityPrinting) { /* 276:325 */ buf.append(this.facilityStr); /* 277: */ } /* 278:327 */ buf.append(packet); /* 279:328 */ packet = buf.toString(); /* 280: */ } /* 281:331 */ this.sqw.setLevel(event.getLevel().getSyslogEquivalent()); /* 282:335 */ if (packet.length() > 256) { /* 283:336 */ splitPacket(hdr, packet); /* 284: */ } else { /* 285:338 */ this.sqw.write(packet); /* 286: */ } /* 287:341 */ if ((this.layout == null) || (this.layout.ignoresThrowable())) /* 288: */ { /* 289:342 */ String[] s = event.getThrowableStrRep(); /* 290:343 */ if (s != null) { /* 291:344 */ for (int i = 0; i < s.length; i++) { /* 292:345 */ if (s[i].startsWith("\t")) { /* 293:346 */ this.sqw.write(hdr + " " + s[i].substring(1)); /* 294: */ } else { /* 295:348 */ this.sqw.write(hdr + s[i]); /* 296: */ } /* 297: */ } /* 298: */ } /* 299: */ } /* 300: */ }
/** * 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); }
private String asString(LoggingEvent event) { StringBuilder builder = new StringBuilder(layout.format(event)); if (layout.ignoresThrowable()) { String[] s = event.getThrowableStrRep(); if (s != null) { int len = s.length; for (int i = 0; i < len; i++) { builder.append(s[i]); builder.append(Layout.LINE_SEP); } } } return builder.toString(); }
@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(); }
private TextMessage createEventMessage(LoggingEvent event, String service, String environment) throws JMSException { TextMessage message = createMessage(this.layout.format(event)); message.setStringProperty(Constants.LEVEL, event.getLevel().toString()); message.setLongProperty(Constants.TIME_STAMP, event.getTimeStamp()); message.setStringProperty( Constants.EXCEPTION, StringUtils.join(event.getThrowableStrRep(), "\n")); message.setStringProperty(Constants.SERVICE, service); message.setStringProperty(Constants.ENVIRONMENT, environment); @SuppressWarnings("unchecked") Map<String, Object> context = MDC.getContext(); if (context != null) { for (String key : context.keySet()) { message.setStringProperty("context." + key, MDC.get(key).toString()); } } return message; }
/* 159: */ /* 160: */ protected void subAppend(LoggingEvent event) /* 161: */ { /* 162:310 */ this.qw.write(this.layout.format(event)); /* 163:312 */ if (this.layout.ignoresThrowable()) /* 164: */ { /* 165:313 */ String[] s = event.getThrowableStrRep(); /* 166:314 */ if (s != null) /* 167: */ { /* 168:315 */ int len = s.length; /* 169:316 */ for (int i = 0; i < len; i++) /* 170: */ { /* 171:317 */ this.qw.write(s[i]); /* 172:318 */ this.qw.write(Layout.LINE_SEP); /* 173: */ } /* 174: */ } /* 175: */ } /* 176:323 */ if (shouldFlush(event)) { /* 177:324 */ this.qw.flush(); /* 178: */ } /* 179: */ }
/** * Implements AppenderSkeleton Append method, handles time and format * * @event event to log */ @Override protected void append(LoggingEvent event) { // Render the event according to layout String formattedEvent = layout.format(event); // Append stack trace if present String[] stack = event.getThrowableStrRep(); if (stack != null) { int len = stack.length; formattedEvent += ", "; for (int i = 0; i < len; i++) { formattedEvent += stack[i]; if (i < len - 1) formattedEvent += "\u2028"; } } // Prepare to be queued this.le_async.addLineToQueue(formattedEvent); }
// print to stream, lazily instantiating and opening console if necessary private void print(final LoggingEvent event) { // ensure in UI thread assert Display.getCurrent() != null; // ensure console view is open if (WorkbenchUtil.getView(IConsoleConstants.ID_CONSOLE_VIEW) == null) { try { IWorkbenchPage activePage = WorkbenchUtil.getActivePage(); if (activePage != null) { activePage.showView(IConsoleConstants.ID_CONSOLE_VIEW); } } catch (PartInitException pie) { // ignore for now } } // lazily instantiate console if necessary if (_consoleStream == null) { IConsoleManager mgr = ConsolePlugin.getDefault().getConsoleManager(); MessageConsole logConsole = new MessageConsole( Activator.getResourceString("MessageConsoleAppender.Log"), // $NON-NLS-1$ null); mgr.addConsoles(new IConsole[] {logConsole}); mgr.showConsoleView(logConsole); _consoleStream = logConsole.newMessageStream(); } // print message _consoleStream.print(getLayout().format(event)); if (getLayout().ignoresThrowable()) { String[] lines = event.getThrowableStrRep(); if (lines != null) { for (String line : lines) { _consoleStream.println(line); } } } }
/** * Description:getLogStatement * * @param event * @return String * @author wgq * @version 1.0 */ @Override public String getLogStatement(LoggingEvent event) { StringBuffer sbuf = new StringBuffer(); sbuf.append(layout.format(event)); if (layout.ignoresThrowable()) { sbuf.delete(sbuf.length() - 2, sbuf.length()); String[] s = event.getThrowableStrRep(); if (s != null) { for (int j = 0; j < 10; j++) { sbuf.append("\r\n"); sbuf.append(s[j]); } } sbuf.append("')"); } return sbuf.toString(); }
@SuppressWarnings("unchecked") @Override protected void append(LoggingEvent event) { StringBuilder sb = this.stringBuilder.getSB(); sb.setLength(0); ISO8601.format(new java.util.Date(), sb).append(fieldDelim); sb.append(event.getLevel()).append(fieldDelim).append(event.getLoggerName()); Object obj = event.getMessage(); // time UTC^]Level^]Map if (obj instanceof Map) { Map map = (Map) event.getMessage(); Iterator it = map.keySet().iterator(); Object key = null; while (it.hasNext()) { key = it.next(); sb.append(fieldDelim).append(key.toString()).append(fieldEqual).append(map.get(key)); } } else { // time UTC^]Level^]String sb.append(fieldDelim).append(obj.toString()); } // Extract exceptions String[] s = event.getThrowableStrRep(); if (s != null) { sb.append(fieldDelim).append("Exception").append(fieldEqual); int len = s.length; for (int i = 0; i < len; i++) { sb.append(s[i]).append("\n"); } } consumer.process(sb.toString()); }
void verifyWithLevelAndException( LoggingEvent le, XLogger.Level level, String expectedMsg, Throwable t) { verify(le, expectedMsg); assertEquals(t.toString(), le.getThrowableStrRep()[0]); assertEquals(le.getLevel().toString(), level.toString()); }
void verifyWithException(LoggingEvent le, String expectedMsg, Throwable t) { verify(le, expectedMsg); assertEquals(t.toString(), le.getThrowableStrRep()[0]); }
/*(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); } }