Search in sources :

Example 1 with SocketAppender

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;
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender)

Example 2 with SocketAppender

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();
    }
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender) Test(org.junit.jupiter.api.Test)

Example 3 with SocketAppender

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;
}
Also used : SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender)

Example 4 with SocketAppender

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));
}
Also used : SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender) Appender(org.apache.logging.log4j.core.Appender) AbstractSocketManager(org.apache.logging.log4j.core.net.AbstractSocketManager) SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender)

Aggregations

SocketAppender (org.apache.logging.log4j.core.appender.SocketAppender)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Logger (org.apache.logging.log4j.Logger)1 Appender (org.apache.logging.log4j.core.Appender)1 LogEvent (org.apache.logging.log4j.core.LogEvent)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)1 AbstractSocketManager (org.apache.logging.log4j.core.net.AbstractSocketManager)1 Test (org.junit.jupiter.api.Test)1