@Override public void append(LogEvent event) { lock.lock(); try { String json = new String(getLayout().toByteArray(event)); buffered.add(getBulkItem(json)); this.check(); } catch (Exception ex) { if (!ignoreExceptions()) { throw new AppenderLoggingException(ex); } else { LOGGER.error("Failed to process event.", ex); } } finally { lock.unlock(); } }
private void send() { try { cancelTimer(); if (buffered.size() > 0) { StringBuilder bulkRequestBody = new StringBuilder(); for (String bulkItem : buffered) { bulkRequestBody.append(bulkItem); } try { this.bulkSender.send(bulkRequestBody.toString()); } catch (Exception ex) { if (!ignoreExceptions()) { throw new AppenderLoggingException(ex); } else { LOGGER.error("Failed to send data to Elastic server.", ex); } } } } finally { buffered.clear(); } }
@Override public void append(LogEvent event) { readLock.lock(); try { String message = new String(getLayout().toByteArray(event)); messageBuffer.add(message.trim()); } catch (Exception ex) { if (!ignoreExceptions()) { throw new AppenderLoggingException(ex); } } finally { readLock.unlock(); } }
@Override public void append(LogEvent event) { readLock.lock(); try { String thrown = event.getThrown() == null ? "" : ExceptionUtils.getStackTrace(event.getThrown()); org.pascani.dsl.lib.events.LogEvent e = new org.pascani.dsl.lib.events.LogEvent( UUID.randomUUID(), event.getLoggerName(), event.getLevel().name(), event.getMessage().getFormattedMessage(), thrown, event.getSource() + ""); producer.produce(e); } catch (Exception ex) { if (!ignoreExceptions()) { throw new AppenderLoggingException(ex); } } finally { readLock.unlock(); } }
@Override public void append(final LogEvent event) { final StackTraceElement source = event.getSource(); // do nothing in case this comes from the eventbus endpoint if (EventBusEndpoint.class.getName().equals(source.getClassName())) { return; } readLock.lock(); try { final byte[] bytes = getLayout().toByteArray(event); final String message = new String(bytes); new WebSocketEvent(message); // EventBusService.getEventBus().post(webSocketEvent); } catch (final Exception ex) { if (!ignoreExceptions()) { throw new AppenderLoggingException(ex); } } finally { readLock.unlock(); } }
public ElasticsearchHttpClient(String url, String index, String type, boolean create, int maxActionsPerBulkRequest, long flushSecs, boolean logresponses) { this.url = url; this.index = index; this.type = type; this.create = create; this.maxActionsPerBulkRequest = maxActionsPerBulkRequest; this.logresponses = logresponses; this.closed = false; this.service = Executors.newScheduledThreadPool(1); service.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { flush(); } catch (IOException e) { logger.error(e.getMessage(), e); throw new AppenderLoggingException(e); } } }, flushSecs, flushSecs, TimeUnit.SECONDS); }
@Test public void test64KMessages() throws Exception { final char[] a64K = new char[1024 * 64]; Arrays.fill(a64K, 'a'); final String m1 = new String(a64K); final String m2 = MESSAGE_2 + m1; if (expectLengthException) { try { testServer(m1, m2); } catch (final AppenderLoggingException are) { assertTrue("", are.getCause() != null && are.getCause() instanceof IOException); // Failure expected. } } else { testServer(m1, m2); } }
@Override protected void writeInternal(final LogEvent event, final Serializable serializable) { if (!this.isRunning() || this.connection == null || this.connection.isClosed()) { throw new AppenderLoggingException( "Cannot write logging event; NoSQL manager not connected to the database."); } final NoSqlObject<W> entity = this.connection.createObject(); if (serializable instanceof MapMessage) { setFields((MapMessage<?, ?>) serializable, entity); } else { setFields(event, entity); } this.connection.insertObject(entity); }
@Override protected synchronized void writeToDestination(final byte[] bytes, final int offset, final int length) { try { if (randomAccessFile == null) { String fileName = getFileName(); File file = new File(fileName); FileUtils.makeParentDirs(file); createFileAfterRollover(fileName); } randomAccessFile.write(bytes, offset, length); size += length; } catch (final IOException ex) { final String msg = "Error writing to RandomAccessFile " + getName(); throw new AppenderLoggingException(msg, ex); } }
@Override public void insertObject(final NoSqlObject<BasicDBObject> object) { object.set("clientIP", NetUtils.getIp()); object.set("clientName", this.clientName); try { this.collection.insert(object.unwrap(), this.writeConcern); } catch (final MongoException e) { throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(), e); } }
@Override public void append(LogEvent event) { try { String message = new String(getLayout().toByteArray(event)); synchronized (logQueue) { logQueue.add(message); } Platform.runLater(this::pumpQueue); } catch (Exception ex) { if (!ignoreExceptions()) { throw new AppenderLoggingException(ex); } } }
public void append(LogEvent event) { try { if (producer != null) { Future<RecordMetadata> result = producer.send(new ProducerRecord<String, String>(topic, getLayout().toSerializable(event).toString())); if(syncsend) result.get(); } } catch (final Exception e) { LOGGER.error("Unable to write to kafka for appender [{}].", this.getName(), e); throw new AppenderLoggingException("Unable to write to kafka in appender: " + e.getMessage(), e); } finally { } }
@Override public void setStatus(Status status) { this.status = status; try { log.info(status.getMarker(), statusMessage); } catch (AppenderLoggingException e) { if (!Main.isShuttingDown()) { log.error("Could not contact our internal Cassandra!!!" + e); } } }
private void reportDocStatus(Status status, Document document, String message, Object... messageParams) { try { ThreadContext.put(JesterJAppender.JJ_INGEST_DOCID, document.getId()); ThreadContext.put(JesterJAppender.JJ_INGEST_SOURCE_SCANNER, document.getSourceScannerName()); document.setStatus(status); log.info(status.getMarker(), message, messageParams); } catch (AppenderLoggingException | NoHostAvailableException e) { if (!Main.isShuttingDown()) { log.error("Could not contact our internal Cassandra!!!" + e); } } finally { ThreadContext.clearAll(); } }
@Override public void insertObject(final NoSqlObject<Map<String, Object>> object) { try { client.index(object.unwrap()); } catch (Exception e) { throw new AppenderLoggingException("failed to write log event to Elasticsearch HTTP: " + e.getMessage(), e); } }
@Override public void close() { if (closed.compareAndSet(false, true)) { try { client.close(); } catch (IOException e) { throw new AppenderLoggingException("failed to close log to Elasticsearch HTTP: " + e.getMessage(), e); } } }
public ElasticsearchHttpClient index(Map<String, Object> source) { if (closed) { logger.error("logger is closed"); throw new AppenderLoggingException("logger is closed"); } try { requests.add(build(index, type, create, source)); } catch (Exception e) { logger.error(e); closed = true; } return this; }
@Override public synchronized void flush() { buffer.flip(); try { randomAccessFile.write(buffer.array(), 0, buffer.limit()); } catch (final IOException ex) { final String msg = "Error writing to RandomAccessFile " + getName(); throw new AppenderLoggingException(msg, ex); } buffer.clear(); }
@Override public void insertObject(final NoSQLObject<BasicDBObject> object) { try { final WriteResult result = this.collection.insert(object.unwrap(), this.writeConcern); if (result.getError() != null && result.getError().length() > 0) { throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + result.getError() + "."); } } catch (final MongoException e) { throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(), e); } }
@Override public void insertObject(final NoSQLObject<Map<String, Object>> object) { try { final Response response = this.client.save(object.unwrap()); if (response.getError() != null && response.getError().length() > 0) { throw new AppenderLoggingException("Failed to write log event to CouchDB due to error: " + response.getError() + "."); } } catch (final Exception e) { throw new AppenderLoggingException("Failed to write log event to CouchDB due to error: " + e.getMessage(), e); } }
@Override public void reportError(String message, Exception e) { if (e != null) { throw new AppenderLoggingException(e); } LOGGER.error(message, null, 0); }
@Test public void shouldPropagateException() throws Exception { assertThrows(AppenderLoggingException.class, new Executable() { @Override public void execute() throws Throwable { Logger logger = loggerContext.getLogger("biz.exception"); logger.info(LOG_MESSAGE); } }); }
@Override public void insertObject(final NoSqlObject<Map<String, Object>> object) { try { final Response response = this.client.save(object.unwrap()); if (Strings.isNotEmpty(response.getError())) { throw new AppenderLoggingException( "Failed to write log event to CouchDB due to error: " + response.getError() + '.'); } } catch (final Exception e) { throw new AppenderLoggingException("Failed to write log event to CouchDB due to error: " + e.getMessage(), e); } }
void send(final LogEvent event, final Serializable serializable) { if (messageProducer == null) { if (reconnector != null && !configuration.isImmediateFail()) { reconnector.latch(); } if (messageProducer == null) { throw new AppenderLoggingException( "Error sending to JMS Manager '" + getName() + "': JMS message producer not available"); } } synchronized (this) { try { createMessageAndSend(event, serializable); } catch (final JMSException causeEx) { if (configuration.isRetry() && reconnector == null) { reconnector = createReconnector(); try { closeJndiManager(); reconnector.reconnect(); } catch (NamingException | JMSException reconnEx) { LOGGER.debug("Cannot reestablish JMS connection to {}: {}; starting reconnector thread {}", configuration, reconnEx.getLocalizedMessage(), reconnector.getName(), reconnEx); reconnector.start(); throw new AppenderLoggingException( String.format("Error sending to %s for %s", getName(), configuration), causeEx); } try { createMessageAndSend(event, serializable); } catch (final JMSException e) { throw new AppenderLoggingException( String.format("Error sending to %s after reestablishing connection for %s", getName(), configuration), causeEx); } } } } }
@Override protected void connectAndStart() { try { this.entityManager = this.entityManagerFactory.createEntityManager(); this.transaction = this.entityManager.getTransaction(); this.transaction.begin(); } catch (final Exception e) { throw new AppenderLoggingException( "Cannot write logging event or flush buffer; manager cannot create EntityManager or transaction.", e ); } }
@SuppressWarnings("sync-override") // synchronization on "this" is done within the method @Override protected void write(final byte[] bytes, final int offset, final int length, final boolean immediateFlush) { if (socket == null) { if (reconnector != null && !immediateFail) { reconnector.latch(); } if (socket == null) { throw new AppenderLoggingException("Error writing to " + getName() + ": socket not available"); } } synchronized (this) { try { writeAndFlush(bytes, offset, length, immediateFlush); } catch (final IOException causeEx) { if (retry && reconnector == null) { final String config = inetAddress + ":" + port; reconnector = createReconnector(); try { reconnector.reconnect(); } catch (IOException reconnEx) { LOGGER.debug("Cannot reestablish socket connection to {}: {}; starting reconnector thread {}", config, reconnEx.getLocalizedMessage(), reconnector.getName(), reconnEx); reconnector.start(); throw new AppenderLoggingException( String.format("Error sending to %s for %s", getName(), config), causeEx); } try { writeAndFlush(bytes, offset, length, immediateFlush); } catch (IOException e) { throw new AppenderLoggingException( String.format("Error writing to %s after reestablishing connection for %s", getName(), config), causeEx); } } } } }
@Override protected void connectAndStart() { try { this.connection = this.provider.getConnection(); } catch (final Exception e) { throw new AppenderLoggingException("Failed to get connection from NoSQL connection provider.", e); } }
@Test public void testWriteInternalNotConnected01() { try (final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager("name", 0, provider)) { expectedException.expect(AppenderLoggingException.class); manager.writeInternal(mock(LogEvent.class), null); } }
@Test public void testWriteInternalNotConnected02() { given(connection.isClosed()).willReturn(true); try (final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager("name", 0, provider)) { manager.startup(); manager.connectAndStart(); then(provider).should().getConnection(); expectedException.expect(AppenderLoggingException.class); manager.writeInternal(mock(LogEvent.class), null); } }
@Override public void insertObject(final NoSqlObject<Document> object) { try { final Document unwrapped = object.unwrap(); LOGGER.debug("Inserting object {}", unwrapped); this.collection.insertOne(unwrapped); } catch (final MongoException e) { throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(), e); } }
@Override protected void connectAndStart() { try { this.connection = this.connectionSource.getConnection(); this.connection.setAutoCommit(false); logger().debug("Preparing SQL: {}", this.sqlStatement); this.statement = this.connection.prepareStatement(this.sqlStatement); } catch (final SQLException e) { throw new AppenderLoggingException( "Cannot write logging event or flush buffer; JDBC manager cannot connect to the database.", e); } }
@Override public void insertObject(final NoSqlObject<BasicDBObject> object) { try { final BasicDBObject unwrapped = object.unwrap(); LOGGER.debug("Inserting object {}", unwrapped); this.collection.insert(unwrapped, this.writeConcern); } catch (final MongoException e) { throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(), e); } }
public void flush() throws IOException { lock.lock(); try { while (!requests.isEmpty()) { try { if (closed) { logger.error("logger is closed"); return; } connection = (HttpURLConnection) new URL(url).openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("content-type", "application/x-ndjson"); StringBuilder sb = new StringBuilder(); int i = maxActionsPerBulkRequest; String request; while ((request = requests.poll()) != null && (i-- >= 0)) { sb.append(request); } OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); writer.write(sb.toString()); writer.close(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { // read response if (logresponses) { sb.setLength(0); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String s; while ((s = in.readLine()) != null) { sb.append(s); } in.close(); logger.info(sb.toString()); } } else { throw new AppenderLoggingException("no OK response: " + connection.getResponseCode() + " " + connection.getResponseMessage()); } } finally { if (connection != null) { connection.disconnect(); } } } } catch (Throwable t) { logger.error(t); closed = true; throw new AppenderLoggingException("Elasticsearch HTTP error", t); } finally { lock.unlock(); } }
@Override public void append(LogEvent event) { final Layout<? extends Serializable> layout = getLayout(); final String formattedMessage; if (layout == null) { formattedMessage = event.getMessage().getFormattedMessage(); } else { formattedMessage = new String(layout.toByteArray(event), StandardCharsets.UTF_8); } final GelfMessageBuilder builder = new GelfMessageBuilder(formattedMessage, hostName) .timestamp(event.getTimeMillis() / 1000d) .level(GelfMessageLevel.fromNumericLevel(Severity.getSeverity(event.getLevel()).getCode())) .additionalField("loggerName", event.getLoggerName()) .additionalField("threadName", event.getThreadName()); final Marker marker = event.getMarker(); if (marker != null) { builder.additionalField("marker", marker.getName()); } if (includeThreadContext) { for (Map.Entry<String, String> entry : event.getContextMap().entrySet()) { builder.additionalField(entry.getKey(), entry.getValue()); } // Guard against https://issues.apache.org/jira/browse/LOG4J2-1530 final ThreadContext.ContextStack contextStack = event.getContextStack(); if (contextStack != null) { final List<String> contextStackItems = contextStack.asList(); if (contextStackItems != null && !contextStackItems.isEmpty()) { builder.additionalField("contextStack", contextStackItems.toString()); } } } if (includeSource) { final StackTraceElement source = event.getSource(); if (source != null) { builder.additionalField("sourceFileName", source.getFileName()); builder.additionalField("sourceMethodName", source.getMethodName()); builder.additionalField("sourceClassName", source.getClassName()); builder.additionalField("sourceLineNumber", source.getLineNumber()); } } @SuppressWarnings("all") final Throwable thrown = event.getThrown(); if (includeStackTrace && thrown != null) { String stackTrace; if (includeExceptionCause) { final StringWriter stringWriter = new StringWriter(); final PrintWriter printWriter = new PrintWriter(stringWriter); thrown.printStackTrace(printWriter); stackTrace = stringWriter.toString(); } else { stackTrace = getSimpleStacktraceAsString(thrown); } builder.additionalField("exceptionClass", thrown.getClass().getCanonicalName()); builder.additionalField("exceptionMessage", thrown.getMessage()); builder.additionalField("exceptionStackTrace", stackTrace); builder.fullMessage(formattedMessage); } if (!additionalFields.isEmpty()) { builder.additionalFields(additionalFields); } final GelfMessage gelfMessage = builder.build(); try { final boolean sent = client.trySend(gelfMessage); if (!sent) { LOG.debug("Couldn't send message: {}", gelfMessage); } } catch (Exception e) { throw new AppenderLoggingException("failed to write log event to GELF server: " + e.getMessage(), e); } }
@Override protected void writeInternal(final LogEvent event) { StringReader reader = null; try { if (!this.isConnected() || this.connection == null || this.connection.isClosed()) { throw new AppenderLoggingException( "Cannot write logging event; JDBC manager not connected to the database."); } int i = 1; for (final Column column : this.columns) { if (column.isEventTimestamp) { this.statement.setTimestamp(i++, new Timestamp(event.getMillis())); } else { if (column.isClob) { reader = new StringReader(column.layout.toSerializable(event)); if (column.isUnicode) { this.statement.setNClob(i++, reader); } else { this.statement.setClob(i++, reader); } } else { if (column.isUnicode) { this.statement.setNString(i++, column.layout.toSerializable(event)); } else { this.statement.setString(i++, column.layout.toSerializable(event)); } } } } if (this.statement.executeUpdate() == 0) { throw new AppenderLoggingException( "No records inserted in database table for log event in JDBC manager."); } } catch (final SQLException e) { throw new AppenderLoggingException("Failed to insert record for log event in JDBC manager: " + e.getMessage(), e); } finally { Closer.closeSilent(reader); } }
@Test public void testReconnect() throws Exception { final List<String> list = new ArrayList<String>(); TestSocketServer server = new TestSocketServer(list); server.start(); Thread.sleep(300); //System.err.println("Initializing logger"); final Logger logger = LogManager.getLogger(SocketReconnectTest.class); String message = "Log #1"; logger.error(message); final String expectedHeader = "Header"; String msg = null; String header = null; for (int i = 0; i < 5; ++i) { Thread.sleep(100); if (list.size() > 1) { header = list.get(0); msg = list.get(1); break; } } assertNotNull("No header", header); assertEquals(expectedHeader, header); assertNotNull("No message", msg); assertEquals(message, msg); logger.error(SHUTDOWN); server.join(); list.clear(); message = "Log #2"; boolean exceptionCaught = false; for (int i = 0; i < 100; ++i) { try { logger.error(message); } catch (final AppenderLoggingException e) { exceptionCaught = true; break; // System.err.println("Caught expected exception"); } } assertTrue("No Exception thrown", exceptionCaught); message = "Log #3"; server = new TestSocketServer(list); server.start(); Thread.sleep(300); msg = null; header = null; logger.error(message); for (int i = 0; i < 5; ++i) { Thread.sleep(100); if (list.size() > 1) { header = list.get(0); msg = list.get(1); break; } } assertNotNull("No header", header); assertEquals(expectedHeader, header); assertNotNull("No message", msg); assertEquals(message, msg); logger.error(SHUTDOWN); server.join(); }
private void handleError(final String prefix) { final String msg = appenderErrorHandlerMessage(prefix); if (!appender.ignoreExceptions()) { throw new AppenderLoggingException(msg); } }
@Test public void testReconnect() throws Exception { final List<String> list = new ArrayList<>(); TestSocketServer server = new TestSocketServer(list); server.start(); Thread.sleep(300); //System.err.println("Initializing logger"); final Logger logger = context.getLogger(); String message = "Log #1"; logger.error(message); final String expectedHeader = "Header"; String msg = null; String header = null; for (int i = 0; i < 5; ++i) { Thread.sleep(100); if (list.size() > 1) { header = list.get(0); msg = list.get(1); break; } } assertNotNull("No header", header); assertEquals(expectedHeader, header); assertNotNull("No message", msg); assertEquals(message, msg); logger.error(SHUTDOWN); server.join(); list.clear(); message = "Log #2"; boolean exceptionCaught = false; for (int i = 0; i < 100; ++i) { try { logger.error(message); } catch (final AppenderLoggingException e) { exceptionCaught = true; break; // System.err.println("Caught expected exception"); } } assertTrue("No Exception thrown", exceptionCaught); message = "Log #3"; server = new TestSocketServer(list); server.start(); Thread.sleep(300); msg = null; header = null; logger.error(message); for (int i = 0; i < 5; ++i) { Thread.sleep(100); if (list.size() > 1) { header = list.get(0); msg = list.get(1); break; } } assertNotNull("No header", header); assertEquals(expectedHeader, header); assertNotNull("No message", msg); assertEquals(message, msg); logger.error(SHUTDOWN); server.join(); }
@Override protected void writeInternal(final LogEvent event, final Serializable serializable) { StringReader reader = null; try { if (!this.isRunning() || this.connection == null || this.connection.isClosed() || this.statement == null || this.statement.isClosed()) { throw new AppenderLoggingException( "Cannot write logging event; JDBC manager not connected to the database."); } if (serializable instanceof MapMessage) { setFields((MapMessage<?, ?>) serializable); } int i = 1; // JDBC indices start at 1 for (final ColumnMapping mapping : this.columnMappings) { if (ThreadContextMap.class.isAssignableFrom(mapping.getType()) || ReadOnlyStringMap.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, event.getContextData().toMap()); } else if (ThreadContextStack.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, event.getContextStack().asList()); } else if (Date.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, DateTypeConverter.fromMillis(event.getTimeMillis(), mapping.getType().asSubclass(Date.class))); } else { StringLayout layout = mapping.getLayout(); if (layout != null) { if (Clob.class.isAssignableFrom(mapping.getType())) { this.statement.setClob(i++, new StringReader(layout.toSerializable(event))); } else if (NClob.class.isAssignableFrom(mapping.getType())) { this.statement.setNClob(i++, new StringReader(layout.toSerializable(event))); } else { final Object value = TypeConverters.convert(layout.toSerializable(event), mapping.getType(), null); if (value == null) { this.statement.setNull(i++, Types.NULL); } else { this.statement.setObject(i++, value); } } } } } for (final ColumnConfig column : this.columnConfigs) { if (column.isEventTimestamp()) { this.statement.setTimestamp(i++, new Timestamp(event.getTimeMillis())); } else if (column.isClob()) { reader = new StringReader(column.getLayout().toSerializable(event)); if (column.isUnicode()) { this.statement.setNClob(i++, reader); } else { this.statement.setClob(i++, reader); } } else if (column.isUnicode()) { this.statement.setNString(i++, column.getLayout().toSerializable(event)); } else { this.statement.setString(i++, column.getLayout().toSerializable(event)); } } if (this.isBatchSupported) { this.statement.addBatch(); } else if (this.statement.executeUpdate() == 0) { throw new AppenderLoggingException( "No records inserted in database table for log event in JDBC manager."); } } catch (final SQLException e) { throw new AppenderLoggingException("Failed to insert record for log event in JDBC manager: " + e.getMessage(), e); } finally { Closer.closeSilently(reader); } }