Java Code Examples for org.apache.logging.log4j.core.StringLayout

The following examples show how to use org.apache.logging.log4j.core.StringLayout. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private static String logWithSpanAndLog4jConfiguration(
    String log4jPattern, SpanContext spanContext, Function<Logger, Void> loggingFunction) {
  StringWriter output = new StringWriter();
  StringLayout layout = PatternLayout.newBuilder().withPattern(log4jPattern).build();
  Appender appender =
      WriterAppender.newBuilder()
          .setTarget(output)
          .setLayout(layout)
          .setName("TestAppender")
          .build();
  ((LoggerContext) LogManager.getContext(false)).updateLoggers();
  appender.start();
  logger.addAppender(appender);
  logger.setLevel(Level.ALL);
  try {
    logWithSpan(spanContext, loggingFunction, logger);
    return output.toString();
  } finally {
    logger.removeAppender(appender);
  }
}
 
Example 2
Source Project: lucene-solr   Source File: TextAreaAppender.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void append(LogEvent event) {
  readLock.lock();
  try {
    if (textArea == null) {
      // just ignore any events logged before the area is available
      return;
    }

    final String message = ((StringLayout) getLayout()).toSerializable(event);
    SwingUtilities.invokeLater(() -> {
      textArea.append(message);
    });
  } finally {
    readLock.unlock();
  }
}
 
Example 3
Source Project: logging-log4j2   Source File: WriterManager.java    License: Apache License 2.0 6 votes vote down vote up
public WriterManager(final Writer writer, final String streamName, final StringLayout layout,
        final boolean writeHeader) {
    super(null, streamName);
    this.writer = writer;
    this.layout = layout;
    if (writeHeader && layout != null) {
        final byte[] header = layout.getHeader();
        if (header != null) {
            try {
                this.writer.write(new String(header, layout.getCharset()));
            } catch (final IOException e) {
                logError("Unable to write header", e);
            }
        }
    }
}
 
Example 4
Source Project: logging-log4j2   Source File: WriterAppender.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public WriterAppender build() {
    final StringLayout layout = (StringLayout) getLayout();
    final StringLayout actualLayout = layout != null ? layout : PatternLayout.createDefaultLayout();
    return new WriterAppender(getName(), actualLayout, getFilter(), getManager(target, follow, actualLayout),
            isIgnoreExceptions(), getPropertyArray());
}
 
Example 5
Source Project: logging-log4j2   Source File: ColumnMapping.java    License: Apache License 2.0 5 votes vote down vote up
private ColumnMapping(final String name, final String source, final StringLayout layout, final String literalValue, final String parameter, final Class<?> type) {
    this.name = name;
    this.nameKey = toKey(name);
    this.source = source;
    this.layout = layout;
    this.literalValue = literalValue;
    this.parameter = parameter;
    this.type = type;
}
 
Example 6
Source Project: jmeter-debugger   Source File: LoggerPanelAppender.java    License: Apache License 2.0 4 votes vote down vote up
public StringLayout getStringLayout() {
    return (StringLayout) getLayout();
}
 
Example 7
Source Project: logging-log4j2   Source File: ClassicLogger.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected StringLayout createLayout() {
    return PatternLayout.newBuilder().setCharset(StandardCharsets.UTF_8).setPattern("%m").build();
}
 
Example 8
Source Project: logging-log4j2   Source File: AbstractWriterAppender.java    License: Apache License 2.0 4 votes vote down vote up
public StringLayout getStringLayout() {
    return (StringLayout) getLayout();
}
 
Example 9
Source Project: logging-log4j2   Source File: WriterAppender.java    License: Apache License 2.0 4 votes vote down vote up
private static WriterManager getManager(final Writer target, final boolean follow, final StringLayout layout) {
    final Writer writer = new CloseShieldWriter(target);
    final String managerName = target.getClass().getName() + "@" + Integer.toHexString(target.hashCode()) + '.'
            + follow;
    return WriterManager.getManager(managerName, new FactoryData(writer, managerName, layout), factory);
}
 
Example 10
Source Project: logging-log4j2   Source File: WriterAppender.java    License: Apache License 2.0 4 votes vote down vote up
private WriterAppender(final String name, final StringLayout layout, final Filter filter,
        final WriterManager manager, final boolean ignoreExceptions, Property[] properties) {
    super(name, layout, filter, ignoreExceptions, true, properties, manager);
}
 
Example 11
Source Project: logging-log4j2   Source File: ColumnMapping.java    License: Apache License 2.0 4 votes vote down vote up
public StringLayout getLayout() {
    return layout;
}
 
Example 12
Source Project: logging-log4j2   Source File: AbstractWriterAppender.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Instantiates.
 * 
 * @param name
 *            The name of the Appender.
 * @param layout
 *            The layout to format the message.
 * @param properties Optional properties.
 * @param manager
 *            The OutputStreamManager.
 */
protected AbstractWriterAppender(final String name, final StringLayout layout, final Filter filter,
        final boolean ignoreExceptions, final boolean immediateFlush, Property[] properties, final M manager) {
    super(name, filter, layout, ignoreExceptions, properties);
    this.manager = manager;
    this.immediateFlush = immediateFlush;
}
 
Example 13
Source Project: logging-log4j2   Source File: WriterAppender.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Creates a WriterAppender.
 * 
 * @param layout
 *            The layout to use or null to get the default layout.
 * @param filter
 *            The Filter or null.
 * @param target
 *            The target Writer
 * @param follow
 *            If true will follow changes to the underlying output stream.
 *            Use false as the default.
 * @param name
 *            The name of the Appender (required).
 * @param ignore
 *            If {@code "true"} (default) exceptions encountered when
 *            appending events are logged; otherwise they are propagated to
 *            the caller. Use true as the default.
 * @return The ConsoleAppender.
 */
@PluginFactory
public static WriterAppender createAppender(StringLayout layout, final Filter filter, final Writer target,
        final String name, final boolean follow, final boolean ignore) {
    if (name == null) {
        LOGGER.error("No name provided for WriterAppender");
        return null;
    }
    if (layout == null) {
        layout = PatternLayout.createDefaultLayout();
    }
    return new WriterAppender(name, layout, filter, getManager(target, follow, layout), ignore, Property.EMPTY_ARRAY);
}
 
Example 14
Source Project: logging-log4j2   Source File: WriterAppender.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Builds instances.
 * 
 * @param writer
 *            The OutputStream.
 * @param type
 *            The name of the target.
 * @param layout
 *            A String layout
 */
public FactoryData(final Writer writer, final String type, final StringLayout layout) {
    this.writer = writer;
    this.name = type;
    this.layout = layout;
}
 
Example 15
Source Project: logging-log4j2   Source File: ColumnMapping.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Layout of value to write to database (before type conversion). Not applicable if {@link #setType(Class)} is
 * a {@link ReadOnlyStringMap}, {@link ThreadContextMap}, or {@link ThreadContextStack}.
 *
 * @return this.
 */
public Builder setLayout(final StringLayout layout) {
    this.layout = layout;
    return this;
}