Re-import log4j-streams module.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/1167a72d Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/1167a72d Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/1167a72d Branch: refs/heads/master Commit: 1167a72d7cd3ab93e1fa76e4d1ee9566d05eb920 Parents: b40793c Author: Matt Sicker <[email protected]> Authored: Sat Aug 30 15:52:21 2014 -0500 Committer: Matt Sicker <[email protected]> Committed: Sat Aug 30 15:52:21 2014 -0500 ---------------------------------------------------------------------- log4j-bom/pom.xml | 6 + log4j-streams/pom.xml | 212 ++++++++++++++ .../logging/log4j/streams/ByteStreamLogger.java | 150 ++++++++++ .../logging/log4j/streams/CharStreamLogger.java | 110 ++++++++ .../streams/LoggerBufferedInputStream.java | 105 +++++++ .../log4j/streams/LoggerBufferedReader.java | 92 +++++++ .../log4j/streams/LoggerInputStream.java | 92 +++++++ .../log4j/streams/LoggerOutputStream.java | 126 +++++++++ .../log4j/streams/LoggerPrintStream.java | 273 +++++++++++++++++++ .../log4j/streams/LoggerPrintWriter.java | 247 +++++++++++++++++ .../logging/log4j/streams/LoggerReader.java | 96 +++++++ .../logging/log4j/streams/LoggerStreams.java | 166 +++++++++++ .../logging/log4j/streams/LoggerWriter.java | 122 +++++++++ .../log4j/streams/helpers/ByteStreamLogger.java | 150 ++++++++++ .../log4j/streams/helpers/CharStreamLogger.java | 110 ++++++++ ...LoggerBufferedInputStreamCallerInfoTest.java | 66 +++++ .../streams/LoggerBufferedInputStreamTest.java | 29 ++ .../LoggerBufferedReaderCallerInfoTest.java | 84 ++++++ .../log4j/streams/LoggerBufferedReaderTest.java | 41 +++ .../LoggerInputStreamCallerInfoTest.java | 54 ++++ .../log4j/streams/LoggerInputStreamTest.java | 127 +++++++++ .../LoggerOutputStreamCallerInfoTest.java | 50 ++++ .../log4j/streams/LoggerOutputStreamTest.java | 125 +++++++++ .../LoggerPrintStreamCallerInfoTest.java | 145 ++++++++++ .../log4j/streams/LoggerPrintStreamTest.java | 116 ++++++++ .../LoggerPrintWriterCallerInfoTest.java | 145 ++++++++++ .../log4j/streams/LoggerPrintWriterTest.java | 118 ++++++++ .../streams/LoggerReaderCallerInfoTest.java | 57 ++++ .../logging/log4j/streams/LoggerReaderTest.java | 141 ++++++++++ .../streams/LoggerStreamsCallerInfoTesting.java | 51 ++++ .../logging/log4j/streams/LoggerWriterTest.java | 124 +++++++++ .../logging/log4j/streams/StreamTesting.java | 61 +++++ .../resources/log4j2-streams-calling-info.xml | 30 ++ .../test/resources/log4j2-streams-unit-test.xml | 30 ++ pom.xml | 1 + 35 files changed, 3652 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-bom/pom.xml ---------------------------------------------------------------------- diff --git a/log4j-bom/pom.xml b/log4j-bom/pom.xml index b84d154..2896a57 100644 --- a/log4j-bom/pom.xml +++ b/log4j-bom/pom.xml @@ -74,6 +74,12 @@ <artifactId>log4j-nosql</artifactId> <version>2.1-SNAPSHOT</version> </dependency> + <!-- Streaming API --> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-streams</artifactId> + <version>2.1-SNAPSHOT</version> + </dependency> </dependencies> </dependencyManagement> <build> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/pom.xml ---------------------------------------------------------------------- diff --git a/log4j-streams/pom.xml b/log4j-streams/pom.xml new file mode 100644 index 0000000..5a38cc9 --- /dev/null +++ b/log4j-streams/pom.xml @@ -0,0 +1,212 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with + ~ this work for additional information regarding copyright ownership. + ~ The ASF licenses this file to You under the Apache License, Version 2.0 + ~ (the "License"); you may not use this file except in compliance with + ~ the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j</artifactId> + <version>2.1-SNAPSHOT</version> + <relativePath>../</relativePath> + </parent> + <artifactId>log4j-streams</artifactId> + <version>2.1-SNAPSHOT</version> + <packaging>jar</packaging> + <name>Apache Log4j Streaming Interface</name> + <description>Provides the ability to convert streams into log statements</description> + <properties> + <log4jParentDir>${basedir}/..</log4jParentDir> + <docLabel>Streaming Documentation</docLabel> + <projectDir>/slf4j-streams</projectDir> + </properties> + <dependencies> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <scope>test</scope> + </dependency> + + <!-- TEST DEPENDENCIES --> + + <!-- Pull in useful test classes from API --> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.easymock</groupId> + <artifactId>easymock</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <!-- Include the standard NOTICE and LICENSE --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-remote-resources-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>process</goal> + </goals> + <configuration> + <skip>false</skip> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + </plugin> + </plugins> + </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-changes-plugin</artifactId> + <version>${changes.plugin.version}</version> + <reportSets> + <reportSet> + <reports> + <report>changes-report</report> + </reports> + </reportSet> + </reportSets> + <configuration> + <issueLinkTemplate>%URL%/show_bug.cgi?id=%ISSUE%</issueLinkTemplate> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-checkstyle-plugin</artifactId> + <version>2.7</version> + <configuration> + <!--<propertiesLocation>${vfs.parent.dir}/checkstyle.properties</propertiesLocation> --> + <configLocation>${log4jParentDir}/checkstyle.xml</configLocation> + <suppressionsLocation>${log4jParentDir}/checkstyle-suppressions.xml</suppressionsLocation> + <enableRulesSummary>false</enableRulesSummary> + <propertyExpansion>basedir=${basedir}</propertyExpansion> + <propertyExpansion>licensedir=${log4jParentDir}/checkstyle-header.txt</propertyExpansion> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>${javadoc.plugin.version}</version> + <configuration> + <bottom> <![CDATA[<p align="center">Copyright © {inceptionYear}-{currentYear} {organizationName}. All Rights Reserved.<br /> + Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, + and the Apache Log4j logo are trademarks of The Apache Software Foundation.</p>]]></bottom> + <!-- module link generation is completely broken in the javadoc plugin for a multi-module non-aggregating + project --> + <detectOfflineLinks>false</detectOfflineLinks> + <linksource>true</linksource> + <tags> + <tag> + <name>issue</name> + <placement>a</placement> + <head>JIRA issue:</head> + </tag> + <tag> + <name>doubt</name> + <placement>a</placement> + <head>Troublesome:</head> + </tag> + <tag> + <name>compare</name> + <placement>a</placement> + <head>Compare with:</head> + </tag> + </tags> + </configuration> + <reportSets> + <reportSet> + <id>non-aggregate</id> + <reports> + <report>javadoc</report> + </reports> + </reportSet> + </reportSets> + </plugin> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>findbugs-maven-plugin</artifactId> + <version>2.5.2</version> + <configuration> + <fork>true</fork> + <jvmArgs>-Duser.language=en</jvmArgs> + <threshold>Normal</threshold> + <effort>Default</effort> + <excludeFilterFile>${log4jParentDir}/findbugs-exclude-filter.xml</excludeFilterFile> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jxr-plugin</artifactId> + <version>2.3</version> + <reportSets> + <reportSet> + <id>non-aggregate</id> + <reports> + <report>jxr</report> + </reports> + </reportSet> + <reportSet> + <id>aggregate</id> + <reports> + <report>aggregate</report> + </reports> + </reportSet> + </reportSets> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + <version>${pmd.plugin.version}</version> + <configuration> + <targetJdk>${maven.compile.target}</targetJdk> + </configuration> + </plugin> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>cobertura-maven-plugin</artifactId> + <version>${cobertura.plugin.version}</version> + <reportSets> + <reportSet> + <!-- Disabled as it makes the site build very slow and causes the performance unit test to fail --> + <reports /> + </reportSet> + </reportSets> + </plugin> + </plugins> + </reporting> +</project> + http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/ByteStreamLogger.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/ByteStreamLogger.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/ByteStreamLogger.java new file mode 100644 index 0000000..24b62da --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/ByteStreamLogger.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +public class ByteStreamLogger { + private static final int BUFFER_SIZE = 1024; + + private final ExtendedLogger logger; + private final Level level; + private final Marker marker; + private final ByteBufferInputStream in; + private final InputStreamReader reader; + private final char[] msgBuf = new char[BUFFER_SIZE]; + private final StringBuilder msg = new StringBuilder(); + private boolean closed; + private final ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE); + + public ByteStreamLogger(final ExtendedLogger logger, final Level level, final Marker marker, final Charset charset) { + this.logger = logger; + this.level = level; + this.marker = marker; + this.in = new ByteBufferInputStream(); + this.reader = new InputStreamReader(in, charset); + } + + public void put(final String fqcn, final int b) throws IOException { + if (b >= 0) { + synchronized (msg) { + buf.put((byte) (b & 0xFF)); + extractMessages(fqcn); + } + } else { + logEnd(fqcn); + } + } + + public void put(final String fqcn, final byte[] b, int off, int len) throws IOException { + if (len >= 0) { + synchronized (msg) { + while (len > buf.remaining()) { + final int remaining = buf.remaining(); + buf.put(b, off, remaining); + len -= remaining; + off += remaining; + extractMessages(fqcn); + } + buf.put(b, off, len); + extractMessages(fqcn); + } + } else { + logEnd(fqcn); + } + } + + public void close(final String fqcn) { + synchronized (msg) { + closed = true; + logEnd(fqcn); +// in.close(); + } + } + + private void extractMessages(final String fqcn) throws IOException { + if (closed) { + return; + } + int read = reader.read(msgBuf); + while (read > 0) { + int off = 0; + for (int pos = 0; pos < read; pos++) { + switch (msgBuf[pos]) { + case '\r': + msg.append(msgBuf, off, pos - off); + off = pos + 1; + break; + case '\n': + msg.append(msgBuf, off, pos - off); + off = pos + 1; + log(fqcn); + break; + } + } + msg.append(msgBuf, off, read - off); + read = reader.read(msgBuf); + } + } + + private void logEnd(final String fqcn) { + if (msg.length() > 0) { + log(fqcn); + } + } + + private void log(final String fqcn) { + // convert to string now so async loggers work + logger.logIfEnabled(fqcn, level, marker, msg.toString()); + msg.setLength(0); + } + + private class ByteBufferInputStream extends InputStream { + + @Override + public int read() throws IOException { + buf.flip(); + int result = -1; + if (buf.limit() > 0) { + result = buf.get() & 0xFF; + } + buf.compact(); + return result; + } + + @Override + public int read(final byte[] bytes, final int off, final int len) throws IOException { + buf.flip(); + int result = -1; + if (buf.limit() > 0) { + result = Math.min(len, buf.limit()); + buf.get(bytes, off, result); + } + buf.compact(); + return result; + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/CharStreamLogger.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/CharStreamLogger.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/CharStreamLogger.java new file mode 100644 index 0000000..589bfaf --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/CharStreamLogger.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.nio.CharBuffer; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +public class CharStreamLogger { + private final ExtendedLogger logger; + private final Level level; + private final Marker marker; + private final StringBuilder msg = new StringBuilder(); + private boolean closed = false; + + public CharStreamLogger(final ExtendedLogger logger, final Level level, final Marker marker) { + this.logger = logger; + this.level = level; + this.marker = marker; + } + + public void put(final String fqcn, final int c) { + if (c >= 0) { + synchronized (msg) { + if (closed) { + return; + } + switch (c) { + case '\n': + log(fqcn); + break; + case '\r': + break; + default: + msg.append((char) c); + } + } + } else { + logEnd(fqcn); + } + } + + public void put(final String fqcn, final char[] cbuf, final int off, final int len) { + put(fqcn, CharBuffer.wrap(cbuf), off, len); + } + + public void put(final String fqcn, final CharSequence str, final int off, final int len) { + if (len >= 0) { + synchronized (msg) { + if (closed) { + return; + } + int start = off; + final int end = off + len; + for (int pos = off; pos < end; pos++) { + final char c = str.charAt(pos); + switch (c) { + case '\r': + case '\n': + msg.append(str, start, pos); + start = pos + 1; + if (c == '\n') { + log(fqcn); + } + break; + } + } + msg.append(str, start, end); + } + } else { + logEnd(fqcn); + } + } + + public void close(final String fqcn) { + synchronized (msg) { + closed = true; + logEnd(fqcn); + } + } + + private void logEnd(final String fqcn) { + if (msg.length() > 0) { + log(fqcn); + } + } + + private void log(final String fqcn) { + logger.logIfEnabled(fqcn, level, marker, msg.toString()); // convert to string now so async loggers + // work + msg.setLength(0); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerBufferedInputStream.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerBufferedInputStream.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerBufferedInputStream.java new file mode 100644 index 0000000..c60ee1e --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerBufferedInputStream.java @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +public class LoggerBufferedInputStream extends BufferedInputStream { + private static final String FQCN = LoggerBufferedInputStream.class.getName(); + + public LoggerBufferedInputStream(final InputStream in, final Logger logger, final Level level) { + this(in, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerBufferedInputStream(final InputStream in, final Charset charset, final Logger logger, final Level level) { + this(in, charset, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerBufferedInputStream(final InputStream in, final Logger logger, final Level level, final Marker marker) { + this(in, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerBufferedInputStream(final InputStream in, final Charset charset, final Logger logger, final Level level, final Marker marker) { + this(in, charset, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerBufferedInputStream(final InputStream in, final int sz, final Logger logger, final Level level) { + this(in, sz, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerBufferedInputStream(final InputStream in, final Charset charset, final int sz, final Logger logger, final Level level) { + this(in, charset, sz, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerBufferedInputStream(final InputStream in, final int sz, final Logger logger, final Level level, final Marker marker) { + this(in, sz, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerBufferedInputStream(final InputStream in, final Charset charset, final int sz, final Logger logger, final Level level, final Marker marker) { + this(in, charset, sz, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerBufferedInputStream(final InputStream in, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + this(in, Charset.defaultCharset(), logger, fqcn, level, marker); + } + + public LoggerBufferedInputStream(final InputStream in, final Charset charset, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + super(new LoggerInputStream(in, charset, logger, fqcn, level, marker)); + } + + public LoggerBufferedInputStream(final InputStream in, final int sz, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + this(in, Charset.defaultCharset(), sz, logger, fqcn, level, marker); + } + + public LoggerBufferedInputStream(final InputStream in, final Charset charset, final int sz, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + super(new LoggerInputStream(in, charset, logger, fqcn, level, marker), sz); + } + + @Override + public void close() throws IOException { + super.close(); + } + + @Override + public synchronized int read() throws IOException { + return super.read(); + } + + @Override + public int read(final byte[] b) throws IOException { + return super.read(b, 0, b.length); + } + + @Override + public synchronized int read(final byte[] b, final int off, final int len) throws IOException { + return super.read(b, off, len); + } + + @Override + public String toString() { + return LoggerBufferedInputStream.class.getSimpleName() + "{stream=" + in + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerBufferedReader.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerBufferedReader.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerBufferedReader.java new file mode 100644 index 0000000..16d998c --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerBufferedReader.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; +import java.nio.CharBuffer; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +public class LoggerBufferedReader extends BufferedReader { + private static final String FQCN = LoggerBufferedReader.class.getName(); + + public LoggerBufferedReader(final Reader reader, final Logger logger, final Level level) { + this(reader, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerBufferedReader(final Reader reader, final Logger logger, final Level level, final Marker marker) { + this(reader, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerBufferedReader(final Reader reader, final int sz, final Logger logger, final Level level) { + this(reader, sz, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerBufferedReader(final Reader reader, final int sz, final Logger logger, final Level level, final Marker marker) { + this(reader, sz, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerBufferedReader(final Reader reader, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + super(new LoggerReader(reader, logger, FQCN, level, marker)); + } + + public LoggerBufferedReader(final Reader reader, final int sz, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + super(new LoggerReader(reader, logger, FQCN, level, marker), sz); + } + + @Override + public void close() throws IOException { + super.close(); + } + + @Override + public int read() throws IOException { + return super.read(); + } + + @Override + public int read(final char[] cbuf) throws IOException { + return super.read(cbuf, 0, cbuf.length); + } + + @Override + public int read(final char[] cbuf, final int off, final int len) throws IOException { + return super.read(cbuf, off, len); + } + + @Override + public int read(final CharBuffer target) throws IOException { + final int len = target.remaining(); + final char[] cbuf = new char[len]; + final int charsRead = read(cbuf, 0, len); + if (charsRead > 0) { + target.put(cbuf, 0, charsRead); + } + return charsRead; + } + + @Override + public String readLine() throws IOException { + return super.readLine(); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerInputStream.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerInputStream.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerInputStream.java new file mode 100644 index 0000000..61ed149 --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerInputStream.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.streams.helpers.ByteStreamLogger; + +/** + * Input stream that logs each line read to a pre-defined level. Can also be configured with a + * Marker. + */ +public class LoggerInputStream extends FilterInputStream { + private static final String FQCN = LoggerInputStream.class.getName(); + + private final String fqcn; + private final ByteStreamLogger logger; + + public LoggerInputStream(final InputStream in, final Logger logger, final Level level) { + this(in, Charset.defaultCharset(), (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerInputStream(final InputStream in, final Charset charset, final Logger logger, final Level level) { + this(in, charset, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerInputStream(final InputStream in, final Logger logger, final Level level, final Marker marker) { + this(in, Charset.defaultCharset(), (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerInputStream(final InputStream in, final Charset charset, final Logger logger, final Level level, final Marker marker) { + this(in, charset, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerInputStream(final InputStream in, final Charset charset, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + super(in); + this.logger = new ByteStreamLogger(logger, level, marker, charset); + this.fqcn = fqcn; + } + + @Override + public int read() throws IOException { + final int b = super.read(); + logger.put(fqcn, b); + return b; + } + + @Override + public int read(final byte[] b) throws IOException { + return read(b, 0, b.length); + } + + @Override + public int read(final byte[] b, final int off, final int len) throws IOException { + final int bytesRead = super.read(b, off, len); + logger.put(fqcn, b, off, bytesRead); + return bytesRead; + } + + @Override + public void close() throws IOException { + logger.close(fqcn); + super.close(); + } + + @Override + public String toString() { + return LoggerInputStream.class.getSimpleName() + "{stream=" + in + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerOutputStream.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerOutputStream.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerOutputStream.java new file mode 100644 index 0000000..f6a504b --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerOutputStream.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.streams.helpers.ByteStreamLogger; + +/** + * Output stream that logs each line written to a pre-defined level. Can also be configured with a + * Marker. This class provides an interface that follows the {@link java.io.OutputStream} methods in + * spirit, but doesn't require output to any external stream. This class should <em>not</em> be used + * as a stream for an underlying logger unless it's being used as a bridge. Otherwise, infinite + * loops may occur! + */ +public class LoggerOutputStream extends OutputStream { + private static final String FQCN = LoggerOutputStream.class.getName(); + + private final OutputStream out; + private final ByteStreamLogger logger; + private final String fqcn; + + public LoggerOutputStream(final Logger logger, final Level level) { + this(null, Charset.defaultCharset(), (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerOutputStream(final Charset charset, final Logger logger, final Level level) { + this(null, charset, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerOutputStream(final Logger logger, final Level level, final Marker marker) { + this(null, Charset.defaultCharset(), (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerOutputStream(final Charset charset, final Logger logger, final Level level, final Marker marker) { + this(null, charset, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerOutputStream(final OutputStream out, final Logger logger, final Level level) { + this(out, Charset.defaultCharset(), (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerOutputStream(final OutputStream out, final Charset charset, final Logger logger, final Level level) { + this(out, charset, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerOutputStream(final OutputStream out, final Logger logger, final Level level, final Marker marker) { + this(out, Charset.defaultCharset(), (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerOutputStream(final OutputStream out, final Charset charset, final Logger logger, final Level level, final Marker marker) { + this(out, charset, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerOutputStream(final OutputStream out, final Charset charset, final ExtendedLogger logger, final String fqcn, final Level level, + final Marker marker) { + this.out = out; + this.logger = new ByteStreamLogger(logger, level, marker, charset); + this.fqcn = fqcn; + } + + @Override + public void write(final int b) throws IOException { + if (out != null) { + out.write(b); + } + logger.put(fqcn, (byte) (b & 0xFF)); + } + + @Override + public void write(final byte[] b) throws IOException { + if (out != null) { + out.write(b); + } + logger.put(fqcn, b, 0, b.length); + } + + @Override + public void write(final byte[] b, final int off, final int len) throws IOException { + if (out != null) { + out.write(b, off, len); + } + logger.put(fqcn, b, off, len); + } + + @Override + public void flush() throws IOException { + if (out != null) { + out.flush(); + } + } + + @Override + public void close() throws IOException { + if (out != null) { + out.close(); + } + logger.close(fqcn); + } + + @Override + public String toString() { + return LoggerOutputStream.class.getSimpleName() + "{stream=" + out + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerPrintStream.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerPrintStream.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerPrintStream.java new file mode 100644 index 0000000..5829a8d --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerPrintStream.java @@ -0,0 +1,273 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.util.Locale; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +/** + * Output stream that logs each line written to a pre-defined level. Can also be configured with a + * Marker. This class provides an interface that follows the {@link java.io.PrintStream} methods in + * spirit, but doesn't require output to any external stream. This class should <em>not</em> be used + * as a stream for an underlying logger unless it's being used as a bridge. Otherwise, infinite + * loops may occur! + */ +public class LoggerPrintStream extends PrintStream { + private static final String FQCN = LoggerPrintStream.class.getName(); + + public LoggerPrintStream(final Logger logger, final Level level) { + this(null, false, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerPrintStream(final Charset charset, final Logger logger, final Level level) throws UnsupportedEncodingException { + this(null, false, charset, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerPrintStream(final Logger logger, final Level level, final Marker marker) { + this(null, false, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerPrintStream(final Charset charset, final Logger logger, final Level level, final Marker marker) throws UnsupportedEncodingException { + this(null, false, charset, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerPrintStream(final OutputStream out, final Logger logger, final Level level) { + this(out, false, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerPrintStream(final OutputStream out, final Charset charset, final Logger logger, final Level level) throws UnsupportedEncodingException { + this(out, false, charset, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerPrintStream(final OutputStream out, final Logger logger, final Level level, final Marker marker) { + this(out, false, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerPrintStream(final OutputStream out, final Charset charset, final Logger logger, final Level level, final Marker marker) + throws UnsupportedEncodingException { + this(out, false, charset, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final Logger logger, final Level level) { + this(out, autoFlush, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final Charset charset, final Logger logger, final Level level) + throws UnsupportedEncodingException { + this(out, autoFlush, charset, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final Logger logger, final Level level, final Marker marker) { + this(out, autoFlush, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final Charset charset, final Logger logger, final Level level, final Marker marker) + throws UnsupportedEncodingException { + this(out, autoFlush, charset, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + super(new LoggerOutputStream(out, Charset.defaultCharset(), logger, fqcn, level, marker), autoFlush); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final Charset charset, final ExtendedLogger logger, final String fqcn, + final Level level, final Marker marker) throws UnsupportedEncodingException { + super(new LoggerOutputStream(out, charset, logger, fqcn, level, marker), autoFlush, charset.name()); + } + + @Override + public void write(final int b) { + super.write(b); + } + + @Override + public void write(final byte[] b) throws IOException { + super.write(b); + } + + @Override + public void write(final byte[] b, final int off, final int len) { + super.write(b, off, len); + } + + @Override + public void flush() { + super.flush(); + } + + @Override + public void close() { + super.close(); + } + + @Override + public void print(final boolean b) { + super.print(b); + } + + @Override + public void print(final char c) { + super.print(c); + } + + @Override + public void print(final int i) { + super.print(i); + } + + @Override + public void print(final long l) { + super.print(l); + } + + @Override + public void print(final float f) { + super.print(f); + } + + @Override + public void print(final double d) { + super.print(d); + } + + @Override + public void print(final char[] s) { + super.print(s); + } + + @Override + public void print(final String s) { + super.print(s); + } + + @Override + public void print(final Object obj) { + super.print(obj); + } + + @Override + public void println() { + super.println(); + } + + @Override + public void println(final boolean x) { + super.println(x); + } + + @Override + public void println(final char x) { + super.println(x); + } + + @Override + public void println(final int x) { + super.println(x); + } + + @Override + public void println(final long x) { + super.println(x); + } + + @Override + public void println(final float x) { + super.println(x); + } + + @Override + public void println(final double x) { + super.println(x); + } + + @Override + public void println(final char[] x) { + super.println(x); + } + + @Override + public void println(final String x) { + super.println(x); + } + + @Override + public void println(final Object x) { + super.println(x); + } + + @Override + public LoggerPrintStream printf(final String format, final Object... args) { + super.printf(format, args); + return this; + } + + @Override + public LoggerPrintStream printf(final Locale l, final String format, final Object... args) { + super.printf(l, format, args); + return this; + } + + @Override + public LoggerPrintStream append(final char c) { + super.append(c); + return this; + } + + @Override + public LoggerPrintStream append(final CharSequence csq) { + super.append(csq); + return this; + } + + @Override + public LoggerPrintStream append(final CharSequence csq, final int start, final int end) { + super.append(csq, start, end); + return this; + } + + @Override + public LoggerPrintStream format(final String format, final Object... args) { + super.format(format, args); + return this; + } + + @Override + public LoggerPrintStream format(final Locale l, final String format, final Object... args) { + super.format(l, format, args); + return this; + } + + @Override + public boolean checkError() { + return super.checkError(); + } + + @Override + public String toString() { + return LoggerPrintStream.class.getSimpleName() + "{stream=" + out + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerPrintWriter.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerPrintWriter.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerPrintWriter.java new file mode 100644 index 0000000..f36c911 --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerPrintWriter.java @@ -0,0 +1,247 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.PrintWriter; +import java.io.Writer; +import java.util.Locale; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +/** + * Print Writer that logs each line written to a pre-defined level. Can also be configured with a + * Marker. This class provides an interface that follows the {@link java.io.PrintWriter} methods in + * spirit, but doesn't require output to any external writer. + */ +public class LoggerPrintWriter extends PrintWriter { + private static final String FQCN = LoggerPrintWriter.class.getName(); + + public LoggerPrintWriter(final Logger logger, final Level level) { + this(null, false, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerPrintWriter(final Logger logger, final Level level, final Marker marker) { + this(null, false, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerPrintWriter(final Writer writer, final Logger logger, final Level level) { + this(writer, false, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerPrintWriter(final Writer writer, final Logger logger, final Level level, final Marker marker) { + this(writer, false, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerPrintWriter(final Writer writer, final boolean autoFlush, final Logger logger, final Level level) { + this(writer, autoFlush, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerPrintWriter(final Writer writer, final boolean autoFlush, final Logger logger, final Level level, final Marker marker) { + this(writer, autoFlush, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerPrintWriter(final Writer writer, final boolean autoFlush, final ExtendedLogger logger, final String fqcn, + final Level level, final Marker marker) { + super(new LoggerWriter(writer, logger, fqcn, level, marker), autoFlush); + } + + @Override + public void write(final int c) { + super.write(c); + } + + @Override + public void write(final char[] buf) { + super.write(buf); + } + + @Override + public void write(final char[] buf, final int off, final int len) { + super.write(buf, off, len); + } + + @Override + public void write(final String s) { + super.write(s); + } + + @Override + public void write(final String s, final int off, final int len) { + super.write(s, off, len); + } + + @Override + public void flush() { + super.flush(); + } + + @Override + public void close() { + super.close(); + } + + @Override + public void print(final boolean b) { + super.print(b); + } + + @Override + public void print(final char c) { + super.print(c); + } + + @Override + public void print(final int i) { + super.print(i); + } + + @Override + public void print(final long l) { + super.print(l); + } + + @Override + public void print(final float f) { + super.print(f); + } + + @Override + public void print(final double d) { + super.print(d); + } + + @Override + public void print(final char[] s) { + super.print(s); + } + + @Override + public void print(final String s) { + super.print(s); + } + + @Override + public void print(final Object obj) { + super.print(obj); + } + + @Override + public void println() { + super.println(); + } + + @Override + public void println(final boolean x) { + super.println(x); + } + + @Override + public void println(final char x) { + super.println(x); + } + + @Override + public void println(final int x) { + super.println(x); + } + + @Override + public void println(final long x) { + super.println(x); + } + + @Override + public void println(final float x) { + super.println(x); + } + + @Override + public void println(final double x) { + super.println(x); + } + + @Override + public void println(final char[] x) { + super.println(x); + } + + @Override + public void println(final String x) { + super.println(x); + } + + @Override + public void println(final Object x) { + super.println(x); + } + + @Override + public LoggerPrintWriter printf(final String format, final Object... args) { + super.printf(format, args); + return this; + } + + @Override + public LoggerPrintWriter printf(final Locale l, final String format, final Object... args) { + super.printf(l, format, args); + return this; + } + + @Override + public LoggerPrintWriter append(final char c) { + super.append(c); + return this; + } + + @Override + public LoggerPrintWriter append(final CharSequence csq) { + super.append(csq); + return this; + } + + @Override + public LoggerPrintWriter append(final CharSequence csq, final int start, final int end) { + super.append(csq, start, end); + return this; + } + + @Override + public LoggerPrintWriter format(final String format, final Object... args) { + super.format(format, args); + return this; + } + + @Override + public LoggerPrintWriter format(final Locale l, final String format, final Object... args) { + super.format(l, format, args); + return this; + } + + @Override + public boolean checkError() { + return super.checkError(); + } + + @Override + public String toString() { + return LoggerPrintWriter.class.getSimpleName() + "{stream=" + out + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerReader.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerReader.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerReader.java new file mode 100644 index 0000000..74e7c63 --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerReader.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.FilterReader; +import java.io.IOException; +import java.io.Reader; +import java.nio.CharBuffer; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.streams.helpers.CharStreamLogger; + +/** + * Writer that logs each line written to a pre-defined level. Can also be configured with a Marker. + * This class provides an interface that follows the {@link java.io.Writer} methods in spirit, but + * doesn't require output to any external writer. + */ +public class LoggerReader extends FilterReader { + private static final String FQCN = LoggerReader.class.getName(); + + private final CharStreamLogger logger; + private final String fqcn; + + public LoggerReader(final Reader reader, final Logger logger, final Level level) { + this(reader, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerReader(final Reader reader, final Logger logger, final Level level, final Marker marker) { + this(reader, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerReader(final Reader reader, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + super(reader); + this.logger = new CharStreamLogger(logger, level, marker); + this.fqcn = fqcn; + } + + @Override + public int read() throws IOException { + final int c = super.read(); + logger.put(fqcn, c); + return c; + } + + @Override + public int read(final char[] cbuf) throws IOException { + return read(cbuf, 0, cbuf.length); + } + + @Override + public int read(final char[] cbuf, final int off, final int len) throws IOException { + final int charsRead = super.read(cbuf, off, len); + logger.put(fqcn, cbuf, off, charsRead); + return charsRead; + } + + @Override + public int read(final CharBuffer target) throws IOException { + final int len = target.remaining(); + final char[] cbuf = new char[len]; + final int charsRead = read(cbuf, 0, len); + if (charsRead > 0) { + target.put(cbuf, 0, charsRead); + } + return charsRead; + } + + @Override + public void close() throws IOException { + super.close(); + logger.close(fqcn); + } + + @Override + public String toString() { + return LoggerReader.class.getSimpleName() + "{stream=" + in + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerStreams.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerStreams.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerStreams.java new file mode 100644 index 0000000..f74ce7e --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerStreams.java @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.nio.charset.Charset; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; + +public class LoggerStreams { + + public static Builder trace(final Logger logger) { + return new Builder(logger, Level.TRACE, null); + } + + public static Builder debug(final Logger logger) { + return new Builder(logger, Level.DEBUG, null); + } + + public static Builder info(final Logger logger) { + return new Builder(logger, Level.INFO, null); + } + + public static Builder warn(final Logger logger) { + return new Builder(logger, Level.WARN, null); + } + + public static Builder error(final Logger logger) { + return new Builder(logger, Level.ERROR, null); + } + + public static class Builder { + private final Logger logger; + private final Level level; + private final Marker marker; + + Builder(final Logger logger, final Level level, final Marker marker) { + this.logger = logger; + this.level = level; + this.marker = marker; + } + + public Builder marker(final Marker marker) { + return new Builder(logger, level, marker); + } + + public PrintingBuilder printing() { + return new PrintingBuilder(logger, level, marker, false); + } + + public BufferedBuilder buffered() { + return new BufferedBuilder(logger, level, marker, 0); + } + + public LoggerWriter create(final Writer writer) { + return new LoggerWriter(writer, logger, level, marker); + } + } + + public static class PrintingBuilder { + private final Logger logger; + private final Level level; + private final Marker marker; + private final boolean autoFlush; + + PrintingBuilder(final Logger logger, final Level level, final Marker marker, final boolean autoFlush) { + this.logger = logger; + this.level = level; + this.marker = marker; + this.autoFlush = autoFlush; + } + + public PrintingBuilder marker(final Marker marker) { + return new PrintingBuilder(logger, level, marker, autoFlush); + } + + public PrintingBuilder autoFlush() { + return autoFlush(true); + } + + public PrintingBuilder autoFlush(final boolean autoFlush) { + return new PrintingBuilder(logger, level, marker, autoFlush); + } + + public LoggerPrintWriter create(final Writer writer) { + return new LoggerPrintWriter(writer, autoFlush, logger, level, marker); + } + + public LoggerPrintStream create(final OutputStream out) { + return new LoggerPrintStream(out, autoFlush, logger, level, marker); + } + + public LoggerPrintStream create(final OutputStream out, final Charset charset) { + try { + return new LoggerPrintStream(out, autoFlush, charset, logger, level, marker); + } catch (UnsupportedEncodingException e) { + // Should never occur because the constructor must throw this + throw new IllegalArgumentException("Invalid charset", e); + } + } + } + + public static class BufferedBuilder { + private final Logger logger; + private final Level level; + private final Marker marker; + private final int size; + + BufferedBuilder(final Logger logger, final Level level, final Marker marker, final int size) { + this.logger = logger; + this.level = level; + this.marker = marker; + this.size = size; + } + + public BufferedBuilder marker(final Marker marker) { + return new BufferedBuilder(logger, level, marker, size); + } + + public BufferedBuilder size(final int size) { + return new BufferedBuilder(logger, level, marker, size); + } + + public LoggerBufferedReader create(final Reader reader) { + if (size > 0) { + return new LoggerBufferedReader(reader, size, logger, level, marker); + } + return new LoggerBufferedReader(reader, logger, level, marker); + } + + public LoggerBufferedInputStream create(final InputStream in) { + if (size > 0) { + return new LoggerBufferedInputStream(in, size, logger, level, marker); + } + return new LoggerBufferedInputStream(in, logger, level, marker); + } + + public LoggerBufferedInputStream create(final InputStream in, final Charset charset) { + if (size > 0) { + return new LoggerBufferedInputStream(in, charset, size, logger, level, marker); + } + return new LoggerBufferedInputStream(in, charset, logger, level, marker); + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerWriter.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerWriter.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerWriter.java new file mode 100644 index 0000000..98e6f8e --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/LoggerWriter.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams; + +import java.io.IOException; +import java.io.Writer; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.streams.helpers.CharStreamLogger; + +/** + * Writer that logs each line written to a pre-defined level. Can also be configured with a Marker. + * This class provides an interface that follows the {@link java.io.Writer} methods in spirit, but + * doesn't require output to any external writer. + */ +public class LoggerWriter extends Writer { + private static final String FQCN = LoggerWriter.class.getName(); + + private final Writer writer; + private final CharStreamLogger logger; + private final String fqcn; + + public LoggerWriter(final Logger logger, final Level level) { + this(null, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerWriter(final Logger logger, final Level level, final Marker marker) { + this(null, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerWriter(final Writer writer, final Logger logger, final Level level) { + this(writer, (ExtendedLogger) logger, FQCN, level, null); + } + + public LoggerWriter(final Writer writer, final Logger logger, final Level level, final Marker marker) { + this(writer, (ExtendedLogger) logger, FQCN, level, marker); + } + + public LoggerWriter(final Writer writer, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + this.writer = writer; + this.logger = new CharStreamLogger(logger, level, marker); + this.fqcn = fqcn; + } + + @Override + public void write(final int c) throws IOException { + if (writer != null) { + writer.write(c); + } + logger.put(fqcn, (char) c); + } + + @Override + public void write(final char[] cbuf) throws IOException { + if (writer != null) { + writer.write(cbuf); + } + logger.put(fqcn, cbuf, 0, cbuf.length); + } + + @Override + public void write(final char[] cbuf, final int off, final int len) throws IOException { + if (writer != null) { + writer.write(cbuf, off, len); + } + logger.put(fqcn, cbuf, off, len); + } + + @Override + public void write(final String str) throws IOException { + if (writer != null) { + writer.write(str); + } + logger.put(fqcn, str, 0, str.length()); + } + + @Override + public void write(final String str, final int off, final int len) throws IOException { + if (writer != null) { + writer.write(str, off, len); + } + logger.put(fqcn, str, off, len); + } + + @Override + public void flush() throws IOException { + if (writer != null) { + writer.flush(); + } + } + + @Override + public void close() throws IOException { + if (writer != null) { + writer.close(); + } + logger.close(fqcn); + } + + @Override + public String toString() { + return LoggerWriter.class.getSimpleName() + "{stream=" + writer + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/helpers/ByteStreamLogger.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/helpers/ByteStreamLogger.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/helpers/ByteStreamLogger.java new file mode 100644 index 0000000..e9ee5b8 --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/helpers/ByteStreamLogger.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams.helpers; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +public class ByteStreamLogger { + private static final int BUFFER_SIZE = 1024; + + private final ExtendedLogger logger; + private final Level level; + private final Marker marker; + private final ByteBufferInputStream in; + private final InputStreamReader reader; + private final char[] msgBuf = new char[BUFFER_SIZE]; + private final StringBuilder msg = new StringBuilder(); + private boolean closed; + private final ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE); + + public ByteStreamLogger(final ExtendedLogger logger, final Level level, final Marker marker, final Charset charset) { + this.logger = logger; + this.level = level; + this.marker = marker; + this.in = new ByteBufferInputStream(); + this.reader = new InputStreamReader(in, charset); + } + + public void put(final String fqcn, final int b) throws IOException { + if (b >= 0) { + synchronized (msg) { + buf.put((byte) (b & 0xFF)); + extractMessages(fqcn); + } + } else { + logEnd(fqcn); + } + } + + public void put(final String fqcn, final byte[] b, int off, int len) throws IOException { + if (len >= 0) { + synchronized (msg) { + while (len > buf.remaining()) { + final int remaining = buf.remaining(); + buf.put(b, off, remaining); + len -= remaining; + off += remaining; + extractMessages(fqcn); + } + buf.put(b, off, len); + extractMessages(fqcn); + } + } else { + logEnd(fqcn); + } + } + + public void close(final String fqcn) { + synchronized (msg) { + closed = true; + logEnd(fqcn); +// in.close(); + } + } + + private void extractMessages(final String fqcn) throws IOException { + if (closed) { + return; + } + int read = reader.read(msgBuf); + while (read > 0) { + int off = 0; + for (int pos = 0; pos < read; pos++) { + switch (msgBuf[pos]) { + case '\r': + msg.append(msgBuf, off, pos - off); + off = pos + 1; + break; + case '\n': + msg.append(msgBuf, off, pos - off); + off = pos + 1; + log(fqcn); + break; + } + } + msg.append(msgBuf, off, read - off); + read = reader.read(msgBuf); + } + } + + private void logEnd(final String fqcn) { + if (msg.length() > 0) { + log(fqcn); + } + } + + private void log(final String fqcn) { + // convert to string now so async loggers work + logger.logIfEnabled(fqcn, level, marker, msg.toString()); + msg.setLength(0); + } + + private class ByteBufferInputStream extends InputStream { + + @Override + public int read() throws IOException { + buf.flip(); + int result = -1; + if (buf.limit() > 0) { + result = buf.get() & 0xFF; + } + buf.compact(); + return result; + } + + @Override + public int read(final byte[] bytes, final int off, final int len) throws IOException { + buf.flip(); + int result = -1; + if (buf.limit() > 0) { + result = Math.min(len, buf.limit()); + buf.get(bytes, off, result); + } + buf.compact(); + return result; + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/main/java/org/apache/logging/log4j/streams/helpers/CharStreamLogger.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/main/java/org/apache/logging/log4j/streams/helpers/CharStreamLogger.java b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/helpers/CharStreamLogger.java new file mode 100644 index 0000000..1282e84 --- /dev/null +++ b/log4j-streams/src/main/java/org/apache/logging/log4j/streams/helpers/CharStreamLogger.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.streams.helpers; + +import java.nio.CharBuffer; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +public class CharStreamLogger { + private final ExtendedLogger logger; + private final Level level; + private final Marker marker; + private final StringBuilder msg = new StringBuilder(); + private boolean closed = false; + + public CharStreamLogger(final ExtendedLogger logger, final Level level, final Marker marker) { + this.logger = logger; + this.level = level; + this.marker = marker; + } + + public void put(final String fqcn, final int c) { + if (c >= 0) { + synchronized (msg) { + if (closed) { + return; + } + switch (c) { + case '\n': + log(fqcn); + break; + case '\r': + break; + default: + msg.append((char) c); + } + } + } else { + logEnd(fqcn); + } + } + + public void put(final String fqcn, final char[] cbuf, final int off, final int len) { + put(fqcn, CharBuffer.wrap(cbuf), off, len); + } + + public void put(final String fqcn, final CharSequence str, final int off, final int len) { + if (len >= 0) { + synchronized (msg) { + if (closed) { + return; + } + int start = off; + final int end = off + len; + for (int pos = off; pos < end; pos++) { + final char c = str.charAt(pos); + switch (c) { + case '\r': + case '\n': + msg.append(str, start, pos); + start = pos + 1; + if (c == '\n') { + log(fqcn); + } + break; + } + } + msg.append(str, start, end); + } + } else { + logEnd(fqcn); + } + } + + public void close(final String fqcn) { + synchronized (msg) { + closed = true; + logEnd(fqcn); + } + } + + private void logEnd(final String fqcn) { + if (msg.length() > 0) { + log(fqcn); + } + } + + private void log(final String fqcn) { + logger.logIfEnabled(fqcn, level, marker, msg.toString()); // convert to string now so async loggers + // work + msg.setLength(0); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedInputStreamCallerInfoTest.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedInputStreamCallerInfoTest.java b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedInputStreamCallerInfoTest.java new file mode 100644 index 0000000..b8206c8 --- /dev/null +++ b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedInputStreamCallerInfoTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.logging.log4j.streams; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import org.junit.Before; +import org.junit.Test; + +public class LoggerBufferedInputStreamCallerInfoTest extends LoggerStreamsCallerInfoTesting { + + private LoggerBufferedInputStream logIn; + + @Before + public void setupStreams() { + final InputStream srcInputStream = new ByteArrayInputStream("a\nb\nc\nd".getBytes()); + logIn = new LoggerBufferedInputStream(srcInputStream, getLogger(), LEVEL); + } + + @Test + public void read() throws Exception { + logIn.read(); + + assertMessages("read", 3, "read"); + logIn.close(); + } + + @Test + public void readBytes() throws Exception { + logIn.read(new byte[2]); + + assertMessages("read", 3, "readBytes"); + logIn.close(); + } + + @Test + public void readBytesOffsetLen() throws Exception { + logIn.read(new byte[2], 0, 2); + + assertMessages("read", 3, "readBytesOffsetLen"); + logIn.close(); + } + + @Test + public void close() throws Exception { + logIn.read(); + assertMessages("before close", 3, "close"); + logIn.close(); + assertMessages("after close", 4, "close"); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedInputStreamTest.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedInputStreamTest.java b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedInputStreamTest.java new file mode 100644 index 0000000..e6d60f0 --- /dev/null +++ b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedInputStreamTest.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.logging.log4j.streams; + +import java.io.InputStream; + +import org.apache.logging.log4j.Level; + +public class LoggerBufferedInputStreamTest extends LoggerInputStreamTest { + + @Override + protected InputStream createInputStream() { + return new LoggerBufferedInputStream(wrapped, getLogger(), Level.ERROR); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1167a72d/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedReaderCallerInfoTest.java ---------------------------------------------------------------------- diff --git a/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedReaderCallerInfoTest.java b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedReaderCallerInfoTest.java new file mode 100644 index 0000000..3d3b927 --- /dev/null +++ b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerBufferedReaderCallerInfoTest.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.logging.log4j.streams; + +import java.io.Reader; +import java.io.StringReader; +import java.nio.CharBuffer; + +import org.apache.logging.log4j.Level; +import org.junit.Before; +import org.junit.Test; + +public class LoggerBufferedReaderCallerInfoTest extends LoggerStreamsCallerInfoTesting { + + LoggerBufferedReader logReader; + + @Before + public void setupReader() { + final Reader srcReader = new StringReader("a\nb\nc\nd"); + logReader = new LoggerBufferedReader(srcReader, getLogger(), Level.WARN); + } + + @Test + public void read() throws Exception { + logReader.read(); + + assertMessages("read", 3, "read"); + logReader.close(); + } + + @Test + public void readCbuf() throws Exception { + logReader.read(new char[2]); + + assertMessages("read", 3, "readCbuf"); + logReader.close(); + } + + @Test + public void readCbufOffset() throws Exception { + logReader.read(new char[2], 0, 2); + + assertMessages("read", 3, "readCbufOffset"); + logReader.close(); + } + + @Test + public void readCharBuffer() throws Exception { + logReader.read(CharBuffer.allocate(2)); + + assertMessages("read", 3, "readCharBuffer"); + logReader.close(); + } + + @Test + public void readLine() throws Exception { + logReader.readLine(); + + assertMessages("read", 3, "readLine"); + logReader.close(); + } + + @Test + public void close() throws Exception { + logReader.readLine(); + assertMessages("before close", 3, "close"); + logReader.close(); + assertMessages("after close", 4, "close"); + } +}
