org.apache.logging.log4j.core.Layout.getHeader()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(5.6k)|赞(0)|评价(0)|浏览(103)

本文整理了Java中org.apache.logging.log4j.core.Layout.getHeader()方法的一些代码示例,展示了Layout.getHeader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Layout.getHeader()方法的具体详情如下:
包路径:org.apache.logging.log4j.core.Layout
类名称:Layout
方法名:getHeader

Layout.getHeader介绍

[英]Returns the header for the layout format.
[中]返回布局格式的标题。

代码示例

代码示例来源:origin: org.apache.logging.log4j/log4j-core

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);
    }
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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();
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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 Destination content = new Destination();
    content.byteBuffer.put(layout.getHeader());
    layout.encode(event, content);
    content.getByteBuffer().flip();
    final byte[] record = new byte[content.getByteBuffer().remaining()];
    content.getByteBuffer().get(record);
    data.add(record);
  } else {
    final Destination content = new Destination();
    layout.encode(event, content);
    content.getByteBuffer().flip();
    final byte[] record = new byte[content.getByteBuffer().remaining()];
    content.getByteBuffer().get(record);
    write(record);
  }
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

protected void writeHeader(final Layout<?> layout, final OutputStream out) throws IOException {
  final byte[] header = layout.getHeader();
  if (header != null) {
    out.write(header);
  }
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

protected void setOutputStream(final OutputStream os) {
  final byte[] header = layout.getHeader();
  if (header != null) {
    try {
      os.write(header, 0, header.length);
      this.outputStream = os; // only update field if os.write() succeeded
    } catch (final IOException ioe) {
      logError("Unable to write header", ioe);
    }
  } else {
    this.outputStream = os;
  }
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

/**
 * Writes the layout's header to the file if it exists.
 */
private void writeHeader() {
  if (layout == null) {
    return;
  }
  final byte[] header = layout.getHeader();
  if (header == null) {
    return;
  }
  try {
    if (randomAccessFile.length() == 0) {
      // write to the file, not to the buffer: the buffer may not be empty
      randomAccessFile.write(header, 0, header.length);
    }
  } catch (final IOException e) {
    logError("Unable to write header", e);
  }
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

/**
 * @since 2.6
 * @deprecated
 */
@Deprecated
protected OutputStreamManager(final OutputStream os, final String streamName, final Layout<?> layout,
    final boolean writeHeader, final ByteBuffer byteBuffer) {
  super(null, streamName);
  this.outputStream = os;
  this.layout = layout;
  if (writeHeader && layout != null) {
    final byte[] header = layout.getHeader();
    if (header != null) {
      try {
        getOutputStream().write(header, 0, header.length);
      } catch (final IOException e) {
        logError("Unable to write header", e);
      }
    }
  }
  this.byteBuffer = Objects.requireNonNull(byteBuffer, "byteBuffer");
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

@Override
  public DatagramSocketManager createManager(final String name, final FactoryData data) {
    InetAddress inetAddress;
    try {
      inetAddress = InetAddress.getByName(data.host);
    } catch (final UnknownHostException ex) {
      LOGGER.error("Could not find address of " + data.host, ex);
      return null;
    }
    final OutputStream os = new DatagramOutputStream(data.host, data.port, data.layout.getHeader(),
        data.layout.getFooter());
    return new DatagramSocketManager(name, os, inetAddress, data.host, data.port, data.layout, data.bufferSize);
  }
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

/**
 * @since 2.7
 */
protected OutputStreamManager(final LoggerContext loggerContext, final OutputStream os, final String streamName,
    final boolean createOnDemand, final Layout<? extends Serializable> layout, final boolean writeHeader,
    final ByteBuffer byteBuffer) {
  super(loggerContext, streamName);
  if (createOnDemand && os != null) {
    LOGGER.error(
        "Invalid OutputStreamManager configuration for '{}': You cannot both set the OutputStream and request on-demand.",
        streamName);
  }
  this.layout = layout;
  this.byteBuffer = Objects.requireNonNull(byteBuffer, "byteBuffer");
  this.outputStream = os;
  if (writeHeader && layout != null) {
    final byte[] header = layout.getHeader();
    if (header != null) {
      try {
        getOutputStream().write(header, 0, header.length);
      } catch (final IOException e) {
        logError("Unable to write header for " + streamName, e);
      }
    }
  }
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

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);
}

相关文章