Java 类org.apache.logging.log4j.core.net.Protocol 实例源码

项目:logging-log4j2    文件:SocketAppender.java   
/**
 * Creates an AbstractSocketManager for TCP, UDP, and SSL.
 *
 * @throws IllegalArgumentException
 *             if the protocol cannot be handled.
 */
protected static AbstractSocketManager createSocketManager(final String name, Protocol protocol, final String host,
        final int port, final int connectTimeoutMillis, final SslConfiguration sslConfig,
        final int reconnectDelayMillis, final boolean immediateFail, final Layout<? extends Serializable> layout,
        final int bufferSize, final SocketOptions socketOptions) {
    if (protocol == Protocol.TCP && sslConfig != null) {
        // Upgrade TCP to SSL if an SSL config is specified.
        protocol = Protocol.SSL;
    }
    if (protocol != Protocol.SSL && sslConfig != null) {
        LOGGER.info("Appender {} ignoring SSL configuration for {} protocol", name, protocol);
    }
    switch (protocol) {
    case TCP:
        return TcpSocketManager.getSocketManager(host, port, connectTimeoutMillis, reconnectDelayMillis,
                immediateFail, layout, bufferSize, socketOptions);
    case UDP:
        return DatagramSocketManager.getSocketManager(host, port, layout, bufferSize);
    case SSL:
        return SslSocketManager.getSocketManager(sslConfig, host, port, connectTimeoutMillis, reconnectDelayMillis,
                immediateFail, layout, bufferSize, socketOptions);
    default:
        throw new IllegalArgumentException(protocol.toString());
    }
}
项目:logging-log4j2    文件:SyslogAppenderTest.java   
protected Builder newSyslogAppenderBuilder(final String protocol, final String format, final boolean newLine) {
    // @formatter:off
    return SyslogAppender.newSyslogAppenderBuilder()
            .withPort(PORTNUM)
            .withProtocol(EnglishEnums.valueOf(Protocol.class, protocol))
            .withReconnectDelayMillis(-1)
            .withName("TestApp")
            .withIgnoreExceptions(false)
            .setId("Audit")
            .setEnterpriseNumber(18060)
            .setMdcId("RequestContext")
            .setNewLine(newLine)
            .setAppName("TestApp")
            .setMsgId("Test")
            .setFormat(format);
    // @formatter:on
}
项目:syncope    文件:SyslogRewriteAuditAppender.java   
@Override
public void initTargetAppender() {
    targetAppender = SyslogAppender.newSyslogAppenderBuilder()
            .withName(getTargetAppenderName())
            .withHost("localhost")
            .withPort(514)
            .withProtocol(Protocol.UDP)
            .withLayout(
                    PatternLayout.newBuilder().withPattern("%d{ISO8601} %-5level %logger - %msg%n").build())
            .setFacility(Facility.LOCAL1)
            .build();
}
项目:log4j2    文件:SocketAppender.java   
protected static AbstractSocketManager createSocketManager(final Protocol p, final String host, final int port,
                                                           final int delay, final boolean immediateFail,
                                                           final Layout<? extends Serializable> layout) {
    switch (p) {
        case TCP:
            return TCPSocketManager.getSocketManager(host, port, delay, immediateFail, layout);
        case UDP:
            return DatagramSocketManager.getSocketManager(host, port, layout);
        default:
            return null;
    }
}
项目:logging-log4j2    文件:SyslogAppender.java   
@SuppressWarnings({"resource", "unchecked"})
@Override
public SyslogAppender build() {
    final Protocol protocol = getProtocol();
    final SslConfiguration sslConfiguration = getSslConfiguration();
    final boolean useTlsMessageFormat = sslConfiguration != null || protocol == Protocol.SSL;
    final Configuration configuration = getConfiguration();
    Layout<? extends Serializable> layout = getLayout();
    if (layout == null) {
        layout = RFC5424.equalsIgnoreCase(format)
                ? Rfc5424Layout.createLayout(facility, id, enterpriseNumber, includeMdc, mdcId, mdcPrefix,
                        eventPrefix, newLine, escapeNL, appName, msgId, excludes, includes, required,
                        exceptionPattern, useTlsMessageFormat, loggerFields, configuration)
                :
                // @formatter:off
                SyslogLayout.newBuilder()
                    .setFacility(facility)
                    .setIncludeNewLine(newLine)
                    .setEscapeNL(escapeNL)
                    .setCharset(charsetName)
                    .build();
                // @formatter:off
    }
    final String name = getName();
    if (name == null) {
        LOGGER.error("No name provided for SyslogAppender");
        return null;
    }
    final AbstractSocketManager manager = createSocketManager(name, protocol, getHost(), getPort(), getConnectTimeoutMillis(),
            sslConfiguration, getReconnectDelayMillis(), getImmediateFail(), layout, Constants.ENCODER_BYTE_BUFFER_SIZE, null);

    return new SyslogAppender(name, layout, getFilter(), isIgnoreExceptions(), isImmediateFlush(), manager,
            getAdvertise() ? configuration.getAdvertiser() : null);
}
项目:logging-log4j2    文件:SocketAppender.java   
@SuppressWarnings("resource")
@Override
public SocketAppender build() {
    boolean immediateFlush = isImmediateFlush();
    final boolean bufferedIo = isBufferedIo();
    final Layout<? extends Serializable> layout = getLayout();
    if (layout == null) {
        AbstractLifeCycle.LOGGER.error("No layout provided for SocketAppender");
        return null;
    }

    final String name = getName();
    if (name == null) {
        AbstractLifeCycle.LOGGER.error("No name provided for SocketAppender");
        return null;
    }

    final Protocol protocol = getProtocol();
    final Protocol actualProtocol = protocol != null ? protocol : Protocol.TCP;
    if (actualProtocol == Protocol.UDP) {
        immediateFlush = true;
    }

    final AbstractSocketManager manager = SocketAppender.createSocketManager(name, actualProtocol, getHost(), getPort(),
            getConnectTimeoutMillis(), getSslConfiguration(), getReconnectDelayMillis(), getImmediateFail(), layout, getBufferSize(), getSocketOptions());

    return new SocketAppender(name, layout, getFilter(), manager, isIgnoreExceptions(),
            !bufferedIo || immediateFlush, getAdvertise() ? getConfiguration().getAdvertiser() : null);
}
项目:logging-log4j2    文件:SocketAppender.java   
/**
 * Creates a socket appender.
 *
 * @param host
 *            The name of the host to connect to.
 * @param port
 *            The port to connect to on the target host.
 * @param protocol
 *            The Protocol to use.
 * @param sslConfig
 *            The SSL configuration file for TCP/SSL, ignored for UPD.
 * @param connectTimeoutMillis
 *            the connect timeout in milliseconds.
 * @param reconnectDelayMillis
 *            The interval in which failed writes should be retried.
 * @param immediateFail
 *            True if the write should fail if no socket is immediately available.
 * @param name
 *            The name of the Appender.
 * @param immediateFlush
 *            "true" if data should be flushed on each write.
 * @param ignoreExceptions
 *            If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise they
 *            are propagated to the caller.
 * @param layout
 *            The layout to use. Required, there is no default.
 * @param filter
 *            The Filter or null.
 * @param advertise
 *            "true" if the appender configuration should be advertised, "false" otherwise.
 * @param configuration
 *            The Configuration
 * @return A SocketAppender.
 * @deprecated Deprecated in 2.7; use {@link #newBuilder()}
 */
@Deprecated
@PluginFactory
public static SocketAppender createAppender(
        // @formatter:off
        final String host,
        final int port,
        final Protocol protocol,
        final SslConfiguration sslConfig,
        final int connectTimeoutMillis,
        final int reconnectDelayMillis,
        final boolean immediateFail,
        final String name,
        final boolean immediateFlush,
        final boolean ignoreExceptions,
        final Layout<? extends Serializable> layout,
        final Filter filter,
        final boolean advertise,
        final Configuration configuration) {
        // @formatter:on

    // @formatter:off
    return newBuilder()
        .withAdvertise(advertise)
        .setConfiguration(configuration)
        .withConnectTimeoutMillis(connectTimeoutMillis)
        .withFilter(filter)
        .withHost(host)
        .withIgnoreExceptions(ignoreExceptions)
        .withImmediateFail(immediateFail)
        .withLayout(layout)
        .withName(name)
        .withPort(port)
        .withProtocol(protocol)
        .withReconnectDelayMillis(reconnectDelayMillis)
        .withSslConfiguration(sslConfig)
        .build();
    // @formatter:on
}
项目:logging-log4j2    文件:SocketAppenderTest.java   
@Test
public void testUdpAppender() throws Exception {
    try {
        udpServer.latch.await();
    } catch (final InterruptedException ex) {
        ex.printStackTrace();
    }

    // @formatter:off
    final SocketAppender appender = SocketAppender.newBuilder()
            .withProtocol(Protocol.UDP)
            .withPort(tcpServer.getLocalPort())
            .withReconnectDelayMillis(-1)
            .withName("test")
            .withImmediateFail(false)
            .withLayout(JsonLayout.newBuilder().setProperties(true).build())
            .build();
    // @formatter:on
    appender.start();

    // set appender on root and set level to debug
    logger.addAppender(appender);
    logger.setAdditive(false);
    logger.setLevel(Level.DEBUG);
    logger.debug("This is a udp message");
    final LogEvent event = udpServer.getQueue().poll(3, TimeUnit.SECONDS);
    assertNotNull("No event retrieved", event);
    assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a udp message"));
    assertTrue("Message not delivered via UDP", udpServer.getCount() > 0);
}
项目:log4j2    文件:SocketAppender.java   
/**
 *
 * @param host The name of the host to connect to.
 * @param portNum The port to connect to on the target host.
 * @param protocol The Protocol to use.
 * @param delay The interval in which failed writes should be retried.
 * @param immediateFail True if the write should fail if no socket is immediately available.
 * @param name The name of the Appender.
 * @param immediateFlush "true" if data should be flushed on each write.
 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
 *               they are propagated to the caller.
 * @param layout The layout to use (defaults to SerializedLayout).
 * @param filter The Filter or null.
 * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
 * @param config The Configuration
 * @return A SocketAppender.
 */
@PluginFactory
public static SocketAppender createAppender(
        @PluginAttribute("host") final String host,
        @PluginAttribute("port") final String portNum,
        @PluginAttribute("protocol") final String protocol,
        @PluginAttribute("reconnectionDelay") final String delay,
        @PluginAttribute("immediateFail") final String immediateFail,
        @PluginAttribute("name") final String name,
        @PluginAttribute("immediateFlush") final String immediateFlush,
        @PluginAttribute("ignoreExceptions") final String ignore,
        @PluginElement("Layout") Layout<? extends Serializable> layout,
        @PluginElement("Filters") final Filter filter,
        @PluginAttribute("advertise") final String advertise,
        @PluginConfiguration final Configuration config) {

    boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
    final boolean isAdvertise = Boolean.parseBoolean(advertise);
    final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    final boolean fail = Booleans.parseBoolean(immediateFail, true);
    final int reconnectDelay = AbstractAppender.parseInt(delay, 0);
    final int port = AbstractAppender.parseInt(portNum, 0);
    if (layout == null) {
        layout = SerializedLayout.createLayout();
    }

    if (name == null) {
        LOGGER.error("No name provided for SocketAppender");
        return null;
    }

    final Protocol p = EnglishEnums.valueOf(Protocol.class, protocol != null ? protocol : Protocol.TCP.name());
    if (p.equals(Protocol.UDP)) {
        isFlush = true;
    }

    final AbstractSocketManager manager = createSocketManager(p, host, port, reconnectDelay, fail, layout);
    if (manager == null) {
        return null;
    }

    return new SocketAppender(name, layout, filter, manager, ignoreExceptions, isFlush,
            isAdvertise ? config.getAdvertiser() : null);
}
项目:logging-log4j2    文件:SocketAppender.java   
public Protocol getProtocol() {
    return protocol;
}
项目:logging-log4j2    文件:SocketAppender.java   
public B withProtocol(final Protocol protocol) {
    this.protocol = protocol;
    return asBuilder();
}
项目:logging-log4j2    文件:SocketAppender.java   
/**
 * Creates a socket appender.
 *
 * @param host
 *            The name of the host to connect to.
 * @param portNum
 *            The port to connect to on the target host.
 * @param protocolIn
 *            The Protocol to use.
 * @param sslConfig
 *            The SSL configuration file for TCP/SSL, ignored for UPD.
 * @param connectTimeoutMillis
 *            the connect timeout in milliseconds.
 * @param delayMillis
 *            The interval in which failed writes should be retried.
 * @param immediateFail
 *            True if the write should fail if no socket is immediately available.
 * @param name
 *            The name of the Appender.
 * @param immediateFlush
 *            "true" if data should be flushed on each write.
 * @param ignore
 *            If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise they
 *            are propagated to the caller.
 * @param layout
 *            The layout to use. Required, there is no default.
 * @param filter
 *            The Filter or null.
 * @param advertise
 *            "true" if the appender configuration should be advertised, "false" otherwise.
 * @param config
 *            The Configuration
 * @return A SocketAppender.
 * @deprecated Deprecated in 2.5; use {@link #newBuilder()}
 */
@Deprecated
public static SocketAppender createAppender(
        // @formatter:off
        final String host,
        final String portNum,
        final String protocolIn,
        final SslConfiguration sslConfig,
        final int connectTimeoutMillis,
        // deprecated
        final String delayMillis,
        final String immediateFail,
        final String name,
        final String immediateFlush,
        final String ignore,
        final Layout<? extends Serializable> layout,
        final Filter filter,
        final String advertise,
        final Configuration config) {
        // @formatter:on
    final boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
    final boolean isAdvertise = Boolean.parseBoolean(advertise);
    final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    final boolean fail = Booleans.parseBoolean(immediateFail, true);
    final int reconnectDelayMillis = AbstractAppender.parseInt(delayMillis, 0);
    final int port = AbstractAppender.parseInt(portNum, 0);
    final Protocol p = protocolIn == null ? Protocol.UDP : Protocol.valueOf(protocolIn);
    return createAppender(host, port, p, sslConfig, connectTimeoutMillis, reconnectDelayMillis, fail, name, isFlush,
            ignoreExceptions, layout, filter, isAdvertise, config);
}
项目:logging-log4j2    文件:SocketAppender.java   
/**
 * Creates an AbstractSocketManager for TCP, UDP, and SSL.
 *
 * @throws IllegalArgumentException
 *             if the protocol cannot be handled.
 * @deprecated Use {@link #createSocketManager(String, Protocol, String, int, int, SslConfiguration, int, boolean, Layout, int, SocketOptions)}.
 */
@Deprecated
protected static AbstractSocketManager createSocketManager(final String name, final Protocol protocol, final String host,
        final int port, final int connectTimeoutMillis, final SslConfiguration sslConfig, final int reconnectDelayMillis,
        final boolean immediateFail, final Layout<? extends Serializable> layout, final int bufferSize) {
    return createSocketManager(name, protocol, host, port, connectTimeoutMillis, sslConfig, reconnectDelayMillis, immediateFail, layout, bufferSize, null);
}