Java 类org.apache.logging.log4j.core.layout.SerializedLayout 实例源码

项目:log4j2-extended-jsonlayout    文件:ListAppender.java   
@Override
public synchronized void append(final LogEvent event) {
    final Layout<? extends Serializable> layout = getLayout();
    if (layout == null) {
        if (event instanceof MutableLogEvent) {
            // must take snapshot or subsequent calls to logger.log() will modify this event
            events.add(((MutableLogEvent) event).createMemento());
        } else {
            events.add(event);
        }
    } else if (layout instanceof SerializedLayout) {
        final byte[] header = layout.getHeader();
        final byte[] content = layout.toByteArray(event);
        final byte[] record = new byte[header.length + content.length];
        System.arraycopy(header, 0, record, 0, header.length);
        System.arraycopy(content, 0, record, header.length, content.length);
        data.add(record);
    } else {
        write(layout.toByteArray(event));
    }
    if (countDownLatch != null) {
        countDownLatch.countDown();
    }
}
项目:graylog-plugin-internal-logs    文件:SerializedLogEventTransportTest.java   
@Test
public void launchCreatesAndStartsAppenderAndProcessesMessages() throws Exception {
    final MessageInput messageInput = mock(MessageInput.class);
    transport.launch(messageInput);

    final DirectConsumingAppender appender = transport.getAppender();
    assertThat(appender.getName()).isEqualTo("graylog-plugin-internal-logs");
    assertThat(appender.getLayout()).isInstanceOf(SerializedLayout.class);
    assertThat(appender.isStarted()).isTrue();

    final MutableLogEvent logEvent = new MutableLogEvent();
    logEvent.setMessage(new SimpleMessage("Processed"));
    logEvent.setLevel(Level.ERROR);
    appender.append(logEvent);
    verify(messageInput, times(1)).processRawMessage(any(RawMessage.class));

    final MutableLogEvent ignoredLogEvent = new MutableLogEvent();
    ignoredLogEvent.setMessage(new SimpleMessage("Ignored"));
    ignoredLogEvent.setLevel(Level.TRACE);
    appender.append(ignoredLogEvent);
    verifyNoMoreInteractions(messageInput);
}
项目:log4j2    文件:ListAppender.java   
@Override
public synchronized void append(final LogEvent event) {
    final Layout<? extends Serializable> layout = getLayout();
    if (layout == null) {
        events.add(event);
    } else if (layout instanceof SerializedLayout) {
        final byte[] header = layout.getHeader();
        final byte[] content = layout.toByteArray(event);
        final byte[] record = new byte[header.length + content.length];
        System.arraycopy(header, 0, record, 0, header.length);
        System.arraycopy(content, 0, record, header.length, content.length);
        data.add(record);
    } else {
        write(layout.toByteArray(event));
    }
}
项目:logging-log4j2    文件:ListAppender.java   
@Override
public synchronized void append(final LogEvent event) {
    final Layout<? extends Serializable> layout = getLayout();
    if (layout == null) {
        if (event instanceof MutableLogEvent) {
            // must take snapshot or subsequent calls to logger.log() will modify this event
            events.add(((MutableLogEvent) event).createMemento());
        } else {
            events.add(event);
        }
    } else if (layout instanceof SerializedLayout) {
        final byte[] header = layout.getHeader();
        final byte[] content = layout.toByteArray(event);
        final byte[] record = new byte[header.length + content.length];
        System.arraycopy(header, 0, record, 0, header.length);
        System.arraycopy(content, 0, record, header.length, content.length);
        data.add(record);
    } else {
        write(layout.toByteArray(event));
    }
    if (countDownLatch != null) {
        countDownLatch.countDown();
    }
}
项目:log4j2-extended-jsonlayout    文件:ListAppender.java   
public ListAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, final boolean newline,
                    final boolean raw) {
    super(name, filter, layout);
    this.newLine = newline;
    this.raw = raw;
    if (layout != null && !(layout instanceof SerializedLayout)) {
        final byte[] bytes = layout.getHeader();
        if (bytes != null) {
            write(bytes);
        }
    }
}
项目:log4j2-kafka-appender    文件:KafkaAppender.java   
@PluginFactory
public static KafkaAppender createAppender(@PluginAttribute("name") final String name,
                                           @PluginElement("Filter") final Filter filter,
                                           @PluginAttribute("ignoreExceptions") final String ignore,
                                           @PluginAttribute("topic") final String topic,
                                           @PluginAttribute("enable") String enable,
                                           @PluginAttribute("syncsend") String syncsend,
                                           @PluginElement("Layout") Layout<? extends Serializable> layout,
                                           @PluginElement("Properties") final Property[] properties) {
    boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    boolean enableKafka = Booleans.parseBoolean(enable, true);
    boolean sync = Booleans.parseBoolean(syncsend, false);

    KafkaProducer<String, String> producer = null;
    Map<String, Object> props = new HashMap<String, Object>();
    for (Property property : properties) {
        props.put(property.getName(), property.getValue());
    }
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

    if(enableKafka)
       producer = new KafkaProducer<String, String>(props);

    if(layout == null) {
        layout = SerializedLayout.createLayout();
    }

    return new KafkaAppender(name, filter, layout, ignoreExceptions, producer , topic, sync);

}
项目:log4j2    文件:JMSQueueAppender.java   
/**
 * Create a JMSQueueAppender.
 * @param name The name of the Appender.
 * @param factoryName The fully qualified class name of the InitialContextFactory.
 * @param providerURL The URL of the provider to use.
 * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that
 * will create a URL context factory
 * @param securityPrincipalName The name of the identity of the Principal.
 * @param securityCredentials The security credentials of the Principal.
 * @param factoryBindingName The name to locate in the Context that provides the QueueConnectionFactory.
 * @param queueBindingName The name to use to locate the Queue.
 * @param userName The user ID to use to create the Queue Connection.
 * @param password The password to use to create the Queue Connection.
 * @param layout The layout to use (defaults to SerializedLayout).
 * @param filter The Filter or null.
 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
 *               they are propagated to the caller.
 * @return The JMSQueueAppender.
 */
@PluginFactory
public static JMSQueueAppender createAppender(
        @PluginAttribute("name") final String name,
        @PluginAttribute("factoryName") final String factoryName,
        @PluginAttribute("providerURL") final String providerURL,
        @PluginAttribute("urlPkgPrefixes") final String urlPkgPrefixes,
        @PluginAttribute("securityPrincipalName") final String securityPrincipalName,
        @PluginAttribute("securityCredentials") final String securityCredentials,
        @PluginAttribute("factoryBindingName") final String factoryBindingName,
        @PluginAttribute("queueBindingName") final String queueBindingName,
        @PluginAttribute("userName") final String userName,
        @PluginAttribute("password") final String password,
        @PluginElement("Layout") Layout<? extends Serializable> layout,
        @PluginElement("Filter") final Filter filter,
        @PluginAttribute("ignoreExceptions") final String ignore) {
    if (name == null) {
        LOGGER.error("No name provided for JMSQueueAppender");
        return null;
    }
    final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    final JMSQueueManager manager = JMSQueueManager.getJMSQueueManager(factoryName, providerURL, urlPkgPrefixes,
        securityPrincipalName, securityCredentials, factoryBindingName, queueBindingName, userName, password);
    if (manager == null) {
        return null;
    }
    if (layout == null) {
        layout = SerializedLayout.createLayout();
    }
    return new JMSQueueAppender(name, filter, layout, manager, ignoreExceptions);
}
项目:log4j2    文件:JMSTopicAppender.java   
/**
 * Create a JMSTopicAppender.
 * @param name The name of the Appender.
 * @param factoryName The fully qualified class name of the InitialContextFactory.
 * @param providerURL The URL of the provider to use.
 * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that
 * will create a URL context factory
 * @param securityPrincipalName The name of the identity of the Principal.
 * @param securityCredentials The security credentials of the Principal.
 * @param factoryBindingName The name to locate in the Context that provides the TopicConnectionFactory.
 * @param topicBindingName The name to use to locate the Topic.
 * @param userName The userid to use to create the Topic Connection.
 * @param password The password to use to create the Topic Connection.
 * @param layout The layout to use (defaults to SerializedLayout).
 * @param filter The Filter or null.
 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
 *               they are propagated to the caller.
 * @return The JMSTopicAppender.
 */
@PluginFactory
public static JMSTopicAppender createAppender(
        @PluginAttribute("name") final String name,
        @PluginAttribute("factoryName") final String factoryName,
        @PluginAttribute("providerURL") final String providerURL,
        @PluginAttribute("urlPkgPrefixes") final String urlPkgPrefixes,
        @PluginAttribute("securityPrincipalName") final String securityPrincipalName,
        @PluginAttribute("securityCredentials") final String securityCredentials,
        @PluginAttribute("factoryBindingName") final String factoryBindingName,
        @PluginAttribute("topicBindingName") final String topicBindingName,
        @PluginAttribute("userName") final String userName,
        @PluginAttribute("password") final String password,
        @PluginElement("Layout") Layout<? extends Serializable> layout,
        @PluginElement("Filters") final Filter filter,
        @PluginAttribute("ignoreExceptions") final String ignore) {

    if (name == null) {
        LOGGER.error("No name provided for JMSQueueAppender");
        return null;
    }
    final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    final JMSTopicManager manager = JMSTopicManager.getJMSTopicManager(factoryName, providerURL, urlPkgPrefixes,
        securityPrincipalName, securityCredentials, factoryBindingName, topicBindingName, userName, password);
    if (manager == null) {
        return null;
    }
    if (layout == null) {
        layout = SerializedLayout.createLayout();
    }
    return new JMSTopicAppender(name, filter, layout, manager, ignoreExceptions);
}
项目:log4j2    文件:ListAppender.java   
public ListAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, final boolean newline,
                    final boolean raw) {
    super(name, filter, layout);
    this.newLine = newline;
    this.raw = raw;
    if (layout != null && !(layout instanceof SerializedLayout)) {
        final byte[] bytes = layout.getHeader();
        if (bytes != null) {
            write(bytes);
        }
    }
}
项目:logging-log4j2    文件:KafkaAppender.java   
private void tryAppend(final LogEvent event) throws ExecutionException, InterruptedException, TimeoutException {
    final Layout<? extends Serializable> layout = getLayout();
    byte[] data;
    if (layout instanceof SerializedLayout) {
        final byte[] header = layout.getHeader();
        final byte[] body = layout.toByteArray(event);
        data = new byte[header.length + body.length];
        System.arraycopy(header, 0, data, 0, header.length);
        System.arraycopy(body, 0, data, header.length, body.length);
    } else {
        data = layout.toByteArray(event);
    }
    manager.send(data);
}
项目:logging-log4j2    文件:ListAppender.java   
public ListAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, final boolean newline,
                    final boolean raw) {
    super(name, filter, layout);
    this.newLine = newline;
    this.raw = raw;
    if (layout != null && !(layout instanceof SerializedLayout)) {
        final byte[] bytes = layout.getHeader();
        if (bytes != null) {
            write(bytes);
        }
    }
}
项目:rocketmq-rocketmq-all-4.1.0-incubating    文件:RocketmqLog4j2Appender.java   
private Builder() {
    this.layout = SerializedLayout.createLayout();
    this.ignoreExceptions = true;
}
项目:graylog-plugin-internal-logs    文件:DirectConsumingAppenderTest.java   
@Test
public void appenderUsesSerializedLayout() throws Exception {
    assertThat(appender.getLayout()).isInstanceOf(SerializedLayout.class);
}
项目:rocketmq    文件:RocketmqLog4j2Appender.java   
private Builder() {
    this.layout = SerializedLayout.createLayout();
    this.ignoreExceptions = true;
}
项目: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);
}