use of org.apache.logging.log4j.core.appender.SocketAppender in project vertigo by KleeGroup.
the class SocketLoggerAnalyticsConnectorPlugin method createLogger.
private static Logger createLogger(final String loggerName, final String hostName, final int port) {
// If it doesn't exist we create it with the right appender
final Logger logger = LogManager.getLogger(loggerName);
// we create appender
final SocketAppender appender = SocketAppender.newBuilder().withName("socketAnalytics").withLayout(SerializedLayout.createLayout()).withHost(hostName).withPort(port).withReconnectDelayMillis(// we make only one try
0).build();
appender.start();
// on ne close pas : car ca stop le context
final LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
config.getLoggerConfig(loggerName).addAppender(appender, null, null);
Configurator.setLevel(loggerName, Level.INFO);
return logger;
}
use of org.apache.logging.log4j.core.appender.SocketAppender in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_against_SocketAppender.
@Test
void test_against_SocketAppender() throws Exception {
// Craft nasty events.
final List<LogEvent> logEvents = createNastyLogEvents();
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the server.
final int port = AvailablePortFinder.getNextAvailable();
try (final JsonAcceptingTcpServer server = new JsonAcceptingTcpServer(port, 1)) {
// Create the appender.
final SocketAppender appender = SocketAppender.newBuilder().setHost("localhost").setBufferedIo(false).setPort(port).setReconnectDelayMillis(100).setName("test").setImmediateFail(false).setIgnoreExceptions(false).setLayout(layout).build();
// Start the appender.
appender.start();
// Transfer and verify the log events.
for (int logEventIndex = 0; logEventIndex < logEvents.size(); logEventIndex++) {
// Send the log event.
final LogEvent logEvent = logEvents.get(logEventIndex);
appender.append(logEvent);
appender.getManager().flush();
// Pull the parsed log event.
final JsonNode node = server.receivedNodes.poll(3, TimeUnit.SECONDS);
assertThat(node).as("logEventIndex=%d", logEventIndex).isNotNull();
// Verify the received content.
final String expectedMessage = logEvent.getMessage().getFormattedMessage();
final String expectedMessageChars = explainChars(expectedMessage);
final String actualMessage = point(node, "message").asText();
final String actualMessageChars = explainChars(actualMessage);
assertThat(actualMessageChars).as("logEventIndex=%d", logEventIndex).isEqualTo(expectedMessageChars);
}
// Verify that there were no overflows.
assertThat(server.droppedNodeCount).isZero();
}
}
use of org.apache.logging.log4j.core.appender.SocketAppender in project logging-log4j2 by apache.
the class LogstashIT method createStartedAppender.
private static SocketAppender createStartedAppender(final Layout<?> layout, final int port) {
LOGGER.info("creating the appender");
final SocketAppender appender = SocketAppender.newBuilder().setConfiguration(CONFIGURATION).setHost(HOST_NAME).setPort(port).setReconnectDelayMillis(100).setName("LogstashItAppender").setBufferedIo(false).setImmediateFail(true).setIgnoreExceptions(false).setLayout(layout).build();
appender.start();
return appender;
}
use of org.apache.logging.log4j.core.appender.SocketAppender in project logging-log4j2 by apache.
the class SyslogAppenderConfigurationTest method checkProtocol.
private void checkProtocol(final Protocol expected, final Configuration configuration) {
final Map<String, Appender> appenders = configuration.getAppenders();
assertNotNull(appenders);
final String appenderName = "syslog";
final Appender appender = appenders.get(appenderName);
assertNotNull(appender, "Missing appender " + appenderName);
final SocketAppender socketAppender = (SocketAppender) appender;
@SuppressWarnings("resource") final AbstractSocketManager manager = socketAppender.getManager();
final String prefix = expected + ":";
assertTrue(manager.getName().startsWith(prefix), () -> String.format("'%s' does not start with '%s'", manager.getName(), prefix));
}
Aggregations