Rename log4j-streams to log4j-iostreams.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/ffc1a38a Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/ffc1a38a Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/ffc1a38a Branch: refs/heads/master Commit: ffc1a38a695edf08812e37d26dee78ee8a7dbf29 Parents: bb5d55b Author: Matt Sicker <[email protected]> Authored: Wed Sep 3 00:19:19 2014 -0500 Committer: Matt Sicker <[email protected]> Committed: Wed Sep 3 00:19:19 2014 -0500 ---------------------------------------------------------------------- log4j-iostreams/.gitignore | Bin 0 -> 69 bytes log4j-iostreams/pom.xml | 217 ++++++++++++++ .../log4j/io/LoggerBufferedInputStream.java | 104 +++++++ .../logging/log4j/io/LoggerBufferedReader.java | 91 ++++++ .../log4j/io/LoggerFilterOutputStream.java | 101 +++++++ .../logging/log4j/io/LoggerFilterWriter.java | 99 +++++++ .../logging/log4j/io/LoggerInputStream.java | 92 ++++++ .../logging/log4j/io/LoggerOutputStream.java | 87 ++++++ .../logging/log4j/io/LoggerPrintStream.java | 284 +++++++++++++++++++ .../logging/log4j/io/LoggerPrintWriter.java | 258 +++++++++++++++++ .../apache/logging/log4j/io/LoggerReader.java | 95 +++++++ .../apache/logging/log4j/io/LoggerStreams.java | 170 +++++++++++ .../apache/logging/log4j/io/LoggerWriter.java | 90 ++++++ .../logging/log4j/io/util/ByteStreamLogger.java | 152 ++++++++++ .../logging/log4j/io/util/CharStreamLogger.java | 110 +++++++ .../io/AbstractLoggerOutputStreamTest.java | 138 +++++++++ .../log4j/io/AbstractLoggerWriterTest.java | 138 +++++++++ .../logging/log4j/io/AbstractStreamTest.java | 66 +++++ ...LoggerBufferedInputStreamCallerInfoTest.java | 66 +++++ .../log4j/io/LoggerBufferedInputStreamTest.java | 29 ++ .../io/LoggerBufferedReaderCallerInfoTest.java | 84 ++++++ .../log4j/io/LoggerBufferedReaderTest.java | 41 +++ .../log4j/io/LoggerFilterOutputStreamTest.java | 36 +++ .../log4j/io/LoggerFilterWriterTest.java | 18 ++ .../io/LoggerInputStreamCallerInfoTest.java | 54 ++++ .../logging/log4j/io/LoggerInputStreamTest.java | 127 +++++++++ .../io/LoggerOutputStreamCallerInfoTest.java | 50 ++++ .../log4j/io/LoggerOutputStreamTest.java | 36 +++ .../io/LoggerPrintStreamCallerInfoTest.java | 146 ++++++++++ .../logging/log4j/io/LoggerPrintStreamTest.java | 122 ++++++++ .../io/LoggerPrintWriterCallerInfoTest.java | 145 ++++++++++ .../logging/log4j/io/LoggerPrintWriterTest.java | 124 ++++++++ .../log4j/io/LoggerReaderCallerInfoTest.java | 57 ++++ .../logging/log4j/io/LoggerReaderTest.java | 141 +++++++++ .../io/LoggerStreamsCallerInfoTesting.java | 55 ++++ .../logging/log4j/io/LoggerWriterTest.java | 34 +++ .../resources/log4j2-streams-calling-info.xml | 30 ++ .../test/resources/log4j2-streams-unit-test.xml | 30 ++ log4j-streams/.gitignore | Bin 69 -> 0 bytes log4j-streams/pom.xml | 212 -------------- .../streams/LoggerBufferedInputStream.java | 104 ------- .../log4j/streams/LoggerBufferedReader.java | 91 ------ .../log4j/streams/LoggerFilterOutputStream.java | 101 ------- .../log4j/streams/LoggerFilterWriter.java | 99 ------- .../log4j/streams/LoggerInputStream.java | 92 ------ .../log4j/streams/LoggerOutputStream.java | 87 ------ .../log4j/streams/LoggerPrintStream.java | 284 ------------------- .../log4j/streams/LoggerPrintWriter.java | 258 ----------------- .../logging/log4j/streams/LoggerReader.java | 95 ------- .../logging/log4j/streams/LoggerStreams.java | 170 ----------- .../logging/log4j/streams/LoggerWriter.java | 90 ------ .../log4j/streams/util/ByteStreamLogger.java | 152 ---------- .../log4j/streams/util/CharStreamLogger.java | 110 ------- .../streams/AbstractLoggerOutputStreamTest.java | 138 --------- .../log4j/streams/AbstractLoggerWriterTest.java | 138 --------- .../log4j/streams/AbstractStreamTest.java | 66 ----- ...LoggerBufferedInputStreamCallerInfoTest.java | 66 ----- .../streams/LoggerBufferedInputStreamTest.java | 29 -- .../LoggerBufferedReaderCallerInfoTest.java | 84 ------ .../log4j/streams/LoggerBufferedReaderTest.java | 41 --- .../streams/LoggerFilterOutputStreamTest.java | 36 --- .../log4j/streams/LoggerFilterWriterTest.java | 18 -- .../LoggerInputStreamCallerInfoTest.java | 54 ---- .../log4j/streams/LoggerInputStreamTest.java | 127 --------- .../LoggerOutputStreamCallerInfoTest.java | 50 ---- .../log4j/streams/LoggerOutputStreamTest.java | 36 --- .../LoggerPrintStreamCallerInfoTest.java | 146 ---------- .../log4j/streams/LoggerPrintStreamTest.java | 122 -------- .../LoggerPrintWriterCallerInfoTest.java | 145 ---------- .../log4j/streams/LoggerPrintWriterTest.java | 124 -------- .../streams/LoggerReaderCallerInfoTest.java | 57 ---- .../logging/log4j/streams/LoggerReaderTest.java | 141 --------- .../streams/LoggerStreamsCallerInfoTesting.java | 55 ---- .../logging/log4j/streams/LoggerWriterTest.java | 34 --- .../resources/log4j2-streams-calling-info.xml | 30 -- .../test/resources/log4j2-streams-unit-test.xml | 30 -- pom.xml | 2 +- 77 files changed, 3718 insertions(+), 3713 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/.gitignore ---------------------------------------------------------------------- diff --git a/log4j-iostreams/.gitignore b/log4j-iostreams/.gitignore new file mode 100644 index 0000000..b79adc7 Binary files /dev/null and b/log4j-iostreams/.gitignore differ http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/pom.xml ---------------------------------------------------------------------- diff --git a/log4j-iostreams/pom.xml b/log4j-iostreams/pom.xml new file mode 100644 index 0000000..979494d --- /dev/null +++ b/log4j-iostreams/pom.xml @@ -0,0 +1,217 @@ +<?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-iostreams</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> + <configuration> + <instructions> + <Export-Package>org.apache.logging.log4j.io.*</Export-Package> + </instructions> + </configuration> + </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/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerBufferedInputStream.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerBufferedInputStream.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerBufferedInputStream.java new file mode 100644 index 0000000..75dbb2f --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerBufferedInputStream.java @@ -0,0 +1,104 @@ +/* + * 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.io; + +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.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 Charset charset, final ExtendedLogger logger, final Level level) { + this(in, charset, logger, FQCN, level, null); + } + + public LoggerBufferedInputStream(final InputStream in, final Charset charset, final ExtendedLogger logger, final Level level, final Marker marker) { + this(in, charset, 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 Charset charset, final int size, final ExtendedLogger logger, final Level level) { + this(in, charset, size, logger, FQCN, level, null); + } + + public LoggerBufferedInputStream(final InputStream in, final Charset charset, final int size, final ExtendedLogger logger, final Level level, final Marker marker) { + this(in, charset, size, logger, FQCN, level, marker); + } + + public LoggerBufferedInputStream(final InputStream in, final Charset charset, final int size, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + super(new LoggerInputStream(in, charset, logger, fqcn, level, marker), size); + } + + public LoggerBufferedInputStream(final InputStream in, final ExtendedLogger logger, final Level level) { + this(in, logger, FQCN, level, null); + } + + public LoggerBufferedInputStream(final InputStream in, final ExtendedLogger logger, final Level level, final Marker marker) { + this(in, 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 int size, final ExtendedLogger logger, final Level level) { + this(in, size, logger, FQCN, level, null); + } + + public LoggerBufferedInputStream(final InputStream in, final int size, final ExtendedLogger logger, final Level level, final Marker marker) { + this(in, size, logger, FQCN, level, marker); + } + + public LoggerBufferedInputStream(final InputStream in, final int size, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + this(in, Charset.defaultCharset(), size, logger, fqcn, level, marker); + } + + @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=" + this.in + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerBufferedReader.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerBufferedReader.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerBufferedReader.java new file mode 100644 index 0000000..756f41a --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerBufferedReader.java @@ -0,0 +1,91 @@ +/* + * 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.io; + +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.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 ExtendedLogger logger, final Level level) { + this(reader, logger, FQCN, level, null); + } + + public LoggerBufferedReader(final Reader reader, final ExtendedLogger logger, final Level level, final Marker marker) { + this(reader, 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 size, final ExtendedLogger logger, final Level level) { + this(reader, size, logger, FQCN, level, null); + } + + public LoggerBufferedReader(final Reader reader, final int size, final ExtendedLogger logger, final Level level, final Marker marker) { + this(reader, size, logger, FQCN, level, marker); + } + + public LoggerBufferedReader(final Reader reader, final int size, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + super(new LoggerReader(reader, logger, FQCN, level, marker), size); + } + + @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/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerFilterOutputStream.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerFilterOutputStream.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerFilterOutputStream.java new file mode 100644 index 0000000..b9e6d78 --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerFilterOutputStream.java @@ -0,0 +1,101 @@ +/* + * 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.io; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.io.util.ByteStreamLogger; + +/** + * 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 LoggerFilterOutputStream extends FilterOutputStream { + private static final String FQCN = LoggerFilterOutputStream.class.getName(); + + private final ByteStreamLogger logger; + private final String fqcn; + + public LoggerFilterOutputStream(final OutputStream out, final Charset charset, final ExtendedLogger logger, + final Level level) { + this(out, charset, logger, FQCN, level, null); + } + + public LoggerFilterOutputStream(final OutputStream out, final Charset charset, final ExtendedLogger logger, + final Level level, final Marker marker) { + this(out, charset, logger, FQCN, level, marker); + } + + public LoggerFilterOutputStream(final OutputStream out, final Charset charset, final ExtendedLogger logger, + final String fqcn, final Level level, final Marker marker) { + super(out); + this.logger = new ByteStreamLogger(logger, level, marker, charset); + this.fqcn = fqcn; + } + + public LoggerFilterOutputStream(final OutputStream out, final ExtendedLogger logger, final Level level) { + this(out, Charset.defaultCharset(), logger, FQCN, level, null); + } + + public LoggerFilterOutputStream(final OutputStream out, final ExtendedLogger logger, final Level level, + final Marker marker) { + this(out, Charset.defaultCharset(), logger, FQCN, level, marker); + } + + @Override + public void close() throws IOException { + this.out.close(); + this.logger.close(this.fqcn); + } + + @Override + public void flush() throws IOException { + this.out.flush(); + } + + @Override + public String toString() { + return LoggerFilterOutputStream.class.getSimpleName() + "{stream=" + this.out + '}'; + } + + @Override + public void write(final byte[] b) throws IOException { + this.out.write(b); + this.logger.put(this.fqcn, b, 0, b.length); + } + + @Override + public void write(final byte[] b, final int off, final int len) throws IOException { + this.out.write(b, off, len); + this.logger.put(this.fqcn, b, off, len); + } + + @Override + public void write(final int b) throws IOException { + this.out.write(b); + this.logger.put(this.fqcn, (byte) (b & 0xFF)); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerFilterWriter.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerFilterWriter.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerFilterWriter.java new file mode 100644 index 0000000..a700f92 --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerFilterWriter.java @@ -0,0 +1,99 @@ +/* + * 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.io; + +import java.io.FilterWriter; +import java.io.IOException; +import java.io.Writer; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.io.util.CharStreamLogger; + +/** + * 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 out. + */ +public class LoggerFilterWriter extends FilterWriter { + private static final String FQCN = LoggerFilterWriter.class.getName(); + + private final CharStreamLogger logger; + private final String fqcn; + + public LoggerFilterWriter(final Writer out, final ExtendedLogger logger, final Level level) { + this(out, logger, FQCN, level, null); + } + + public LoggerFilterWriter(final Writer out, final ExtendedLogger logger, final Level level, final Marker marker) { + this(out, logger, FQCN, level, marker); + } + + public LoggerFilterWriter(final Writer out, final ExtendedLogger logger, final String fqcn, final Level level, + final Marker marker) { + super(out); + this.logger = new CharStreamLogger(logger, level, marker); + this.fqcn = fqcn; + } + + @Override + public void close() throws IOException { + this.out.close(); + this.logger.close(this.fqcn); + } + + @Override + public void flush() throws IOException { + this.out.flush(); + } + + @Override + public String toString() { + return LoggerFilterWriter.class.getSimpleName() + "{writer=" + this.out + '}'; + } + + @Override + public void write(final char[] cbuf) throws IOException { + this.out.write(cbuf); + this.logger.put(this.fqcn, cbuf, 0, cbuf.length); + } + + @Override + public void write(final char[] cbuf, final int off, final int len) throws IOException { + this.out.write(cbuf, off, len); + this.logger.put(this.fqcn, cbuf, off, len); + } + + @Override + public void write(final int c) throws IOException { + this.out.write(c); + this.logger.put(this.fqcn, (char) c); + } + + @Override + public void write(final String str) throws IOException { + this.out.write(str); + this.logger.put(this.fqcn, str, 0, str.length()); + } + + @Override + public void write(final String str, final int off, final int len) throws IOException { + this.out.write(str, off, len); + this.logger.put(this.fqcn, str, off, len); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerInputStream.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerInputStream.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerInputStream.java new file mode 100644 index 0000000..873723f --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/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.io; + +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.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.io.util.ByteStreamLogger; + +/** + * 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 Charset charset, final ExtendedLogger logger, final Level level) { + this(in, charset, logger, FQCN, level, null); + } + + public LoggerInputStream(final InputStream in, final Charset charset, final ExtendedLogger logger, final Level level, + final Marker marker) { + this(in, charset, 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; + } + + public LoggerInputStream(final InputStream in, final ExtendedLogger logger, final Level level) { + this(in, Charset.defaultCharset(), logger, FQCN, level, null); + } + + public LoggerInputStream(final InputStream in, final ExtendedLogger logger, final Level level, final Marker marker) { + this(in, Charset.defaultCharset(), logger, FQCN, level, marker); + } + + @Override + public void close() throws IOException { + this.logger.close(this.fqcn); + super.close(); + } + + @Override + public int read() throws IOException { + final int b = super.read(); + this.logger.put(this.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); + this.logger.put(this.fqcn, b, off, bytesRead); + return bytesRead; + } + + @Override + public String toString() { + return LoggerInputStream.class.getSimpleName() + "{stream=" + this.in + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerOutputStream.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerOutputStream.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerOutputStream.java new file mode 100644 index 0000000..f0ab698 --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerOutputStream.java @@ -0,0 +1,87 @@ +/* + * 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.io; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.io.util.ByteStreamLogger; + +/** + * 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 ByteStreamLogger logger; + private final String fqcn; + + public LoggerOutputStream(final ExtendedLogger logger, final Level level) { + this(logger, level, null, Charset.defaultCharset(), FQCN); + } + + public LoggerOutputStream(final ExtendedLogger logger, final Level level, final Charset charset) { + this(logger, level, null, charset, FQCN); + } + + public LoggerOutputStream(final ExtendedLogger logger, final Level level, final Marker marker) { + this(logger, level, marker, Charset.defaultCharset(), FQCN); + } + + public LoggerOutputStream(final ExtendedLogger logger, final Level level, final Marker marker, final Charset charset) { + this(logger, level, marker, charset, FQCN); + } + + public LoggerOutputStream(final ExtendedLogger logger, final Level level, final Marker marker, + final Charset charset, final String fqcn) { + this.logger = new ByteStreamLogger(logger, level, marker, charset); + this.fqcn = fqcn; + } + + @Override + public void close() throws IOException { + this.logger.close(this.fqcn); + } + + @Override + public void flush() throws IOException { + // do nothing + } + + @Override + public void write(final byte[] b) throws IOException { + this.logger.put(this.fqcn, b, 0, b.length); + } + + @Override + public void write(final byte[] b, final int off, final int len) throws IOException { + this.logger.put(this.fqcn, b, off, len); + } + + @Override + public void write(final int b) throws IOException { + this.logger.put(this.fqcn, (byte) (b & 0xFF)); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerPrintStream.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerPrintStream.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerPrintStream.java new file mode 100644 index 0000000..f5d51a5 --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerPrintStream.java @@ -0,0 +1,284 @@ +/* + * 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.io; + +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.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +/** + * 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 ExtendedLogger logger, final boolean autoFlush, final Charset charset, + final String fqcn, final Level level, final Marker marker) throws UnsupportedEncodingException { + super(new LoggerOutputStream(logger, level, marker, charset, fqcn), autoFlush, charset.name()); + } + + public LoggerPrintStream(final ExtendedLogger logger, final Level level) throws UnsupportedEncodingException { + this(logger, false, Charset.defaultCharset(), FQCN, level, null); + } + + public LoggerPrintStream(final ExtendedLogger logger, final Level level, final Charset charset) + throws UnsupportedEncodingException { + this(logger, false, charset, FQCN, level, null); + } + + public LoggerPrintStream(final ExtendedLogger logger, final Level level, final Marker marker) + throws UnsupportedEncodingException { + this(logger, false, Charset.defaultCharset(), FQCN, level, marker); + } + + public LoggerPrintStream(final ExtendedLogger logger, final Level level, final Marker marker, final Charset charset) + throws UnsupportedEncodingException { + this(logger, false, charset, FQCN, level, marker); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final Charset charset, + final ExtendedLogger logger, final Level level) throws UnsupportedEncodingException { + this(out, autoFlush, charset, logger, FQCN, level, null); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final Charset charset, + final ExtendedLogger logger, final Level level, final Marker marker) throws UnsupportedEncodingException { + this(out, autoFlush, charset, logger, FQCN, level, marker); + } + + 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 LoggerFilterOutputStream(out, charset, logger, fqcn, level, marker), autoFlush, charset.name()); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final ExtendedLogger logger, + final Level level) { + this(out, autoFlush, logger, FQCN, level, null); + } + + public LoggerPrintStream(final OutputStream out, final boolean autoFlush, final ExtendedLogger logger, + final Level level, final Marker marker) { + this(out, autoFlush, 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 LoggerFilterOutputStream(out, Charset.defaultCharset(), logger, fqcn, level, marker), autoFlush); + } + + public LoggerPrintStream(final OutputStream out, final Charset charset, final ExtendedLogger logger, + final Level level) throws UnsupportedEncodingException { + this(out, false, charset, logger, FQCN, level, null); + } + + public LoggerPrintStream(final OutputStream out, final Charset charset, final ExtendedLogger logger, + final Level level, final Marker marker) throws UnsupportedEncodingException { + this(out, false, charset, logger, FQCN, level, marker); + } + + public LoggerPrintStream(final OutputStream out, final ExtendedLogger logger, final Level level) { + this(out, false, logger, FQCN, level, null); + } + + public LoggerPrintStream(final OutputStream out, final ExtendedLogger logger, final Level level, final Marker marker) { + this(out, false, logger, FQCN, level, marker); + } + + @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 boolean checkError() { + return super.checkError(); + } + + @Override + public void close() { + super.close(); + } + + @Override + public void flush() { + super.flush(); + } + + @Override + public LoggerPrintStream format(final Locale l, final String format, final Object... args) { + super.format(l, format, args); + return this; + } + + @Override + public LoggerPrintStream format(final String format, final Object... args) { + super.format(format, args); + return this; + } + + @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 char[] s) { + super.print(s); + } + + @Override + public void print(final double d) { + super.print(d); + } + + @Override + public void print(final float f) { + super.print(f); + } + + @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 Object obj) { + super.print(obj); + } + + @Override + public void print(final String s) { + super.print(s); + } + + @Override + public LoggerPrintStream printf(final Locale l, final String format, final Object... args) { + super.printf(l, format, args); + return this; + } + + @Override + public LoggerPrintStream printf(final String format, final Object... args) { + super.printf(format, args); + return this; + } + + @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 char[] x) { + super.println(x); + } + + @Override + public void println(final double x) { + super.println(x); + } + + @Override + public void println(final float 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 Object x) { + super.println(x); + } + + @Override + public void println(final String x) { + super.println(x); + } + + @Override + public String toString() { + return LoggerPrintStream.class.getSimpleName() + "{stream=" + this.out + '}'; + } + + @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 write(final int b) { + super.write(b); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerPrintWriter.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerPrintWriter.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerPrintWriter.java new file mode 100644 index 0000000..902c586 --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerPrintWriter.java @@ -0,0 +1,258 @@ +/* + * 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.io; + +import java.io.PrintWriter; +import java.io.Writer; +import java.util.Locale; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +/** + * 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. + * <p> + * Integration with JDBC logging can be as simple as: + * </p> + * <pre>DriverManager.setLogWriter(new LoggerPrintWriter((ExtendedLogger) LogManager.getLogger(), Level.DEBUG)); + * </pre> + */ +public class LoggerPrintWriter extends PrintWriter { + private static final String FQCN = LoggerPrintWriter.class.getName(); + + @SuppressWarnings("resource") + public LoggerPrintWriter(final ExtendedLogger logger, final boolean autoFlush, final String fqcn, + final Level level, final Marker marker) { + super(new LoggerWriter(logger, fqcn, level, marker), autoFlush); + } + + public LoggerPrintWriter(final ExtendedLogger logger, final Level level) { + this(logger, false, FQCN, level, null); + } + + public LoggerPrintWriter(final ExtendedLogger logger, final Level level, final Marker marker) { + this(logger, false, FQCN, level, marker); + } + + public LoggerPrintWriter(final Writer writer, final boolean autoFlush, final ExtendedLogger logger, final Level level) { + this(writer, autoFlush, logger, FQCN, level, null); + } + + public LoggerPrintWriter(final Writer writer, final boolean autoFlush, final ExtendedLogger logger, final Level level, + final Marker marker) { + this(writer, autoFlush, logger, FQCN, level, marker); + } + + @SuppressWarnings("resource") + public LoggerPrintWriter(final Writer writer, final boolean autoFlush, final ExtendedLogger logger, + final String fqcn, final Level level, final Marker marker) { + super(new LoggerFilterWriter(writer, logger, fqcn, level, marker), autoFlush); + } + + public LoggerPrintWriter(final Writer writer, final ExtendedLogger logger, final Level level) { + this(writer, false, logger, FQCN, level, null); + } + + public LoggerPrintWriter(final Writer writer, final ExtendedLogger logger, final Level level, final Marker marker) { + this(writer, false, logger, FQCN, level, marker); + } + + @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 boolean checkError() { + return super.checkError(); + } + + @Override + public void close() { + super.close(); + } + + @Override + public void flush() { + super.flush(); + } + + @Override + public LoggerPrintWriter format(final Locale l, final String format, final Object... args) { + super.format(l, format, args); + return this; + } + + @Override + public LoggerPrintWriter format(final String format, final Object... args) { + super.format(format, args); + return this; + } + + @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 char[] s) { + super.print(s); + } + + @Override + public void print(final double d) { + super.print(d); + } + + @Override + public void print(final float f) { + super.print(f); + } + + @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 Object obj) { + super.print(obj); + } + + @Override + public void print(final String s) { + super.print(s); + } + + @Override + public LoggerPrintWriter printf(final Locale l, final String format, final Object... args) { + super.printf(l, format, args); + return this; + } + + @Override + public LoggerPrintWriter printf(final String format, final Object... args) { + super.printf(format, args); + return this; + } + + @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 char[] x) { + super.println(x); + } + + @Override + public void println(final double x) { + super.println(x); + } + + @Override + public void println(final float 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 Object x) { + super.println(x); + } + + @Override + public void println(final String x) { + super.println(x); + } + + @Override + public String toString() { + return LoggerPrintWriter.class.getSimpleName() + "{stream=" + this.out + '}'; + } + + @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 int c) { + super.write(c); + } + + @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); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerReader.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerReader.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerReader.java new file mode 100644 index 0000000..658a988 --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerReader.java @@ -0,0 +1,95 @@ +/* + * 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.io; + +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.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.io.util.CharStreamLogger; + +/** + * 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 ExtendedLogger logger, final Level level) { + this(reader, logger, FQCN, level, null); + } + + public LoggerReader(final Reader reader, final ExtendedLogger logger, final Level level, final Marker marker) { + this(reader, 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 void close() throws IOException { + super.close(); + this.logger.close(this.fqcn); + } + + @Override + public int read() throws IOException { + final int c = super.read(); + this.logger.put(this.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); + this.logger.put(this.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 String toString() { + return LoggerReader.class.getSimpleName() + "{stream=" + this.in + '}'; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerStreams.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerStreams.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerStreams.java new file mode 100644 index 0000000..6378d19 --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerStreams.java @@ -0,0 +1,170 @@ +/* + * 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.io; + +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.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; + +public class LoggerStreams { + + public static class BufferedBuilder { + private final ExtendedLogger logger; + private final Level level; + private final Marker marker; + private final int size; + + BufferedBuilder(final ExtendedLogger logger, final Level level, final Marker marker, final int size) { + this.logger = logger; + this.level = level; + this.marker = marker; + this.size = size; + } + + public LoggerBufferedInputStream create(final InputStream in) { + if (this.size > 0) { + return new LoggerBufferedInputStream(in, this.size, this.logger, this.level, this.marker); + } + return new LoggerBufferedInputStream(in, this.logger, this.level, this.marker); + } + + public LoggerBufferedInputStream create(final InputStream in, final Charset charset) { + if (this.size > 0) { + return new LoggerBufferedInputStream(in, charset, this.size, this.logger, this.level, this.marker); + } + return new LoggerBufferedInputStream(in, charset, this.logger, this.level, this.marker); + } + + public LoggerBufferedReader create(final Reader reader) { + if (this.size > 0) { + return new LoggerBufferedReader(reader, this.size, this.logger, this.level, this.marker); + } + return new LoggerBufferedReader(reader, this.logger, this.level, this.marker); + } + + public BufferedBuilder marker(final Marker marker) { + return new BufferedBuilder(this.logger, this.level, marker, this.size); + } + + public BufferedBuilder size(final int size) { + return new BufferedBuilder(this.logger, this.level, this.marker, size); + } + } + + public static class Builder { + private final ExtendedLogger logger; + private final Level level; + private final Marker marker; + + Builder(final ExtendedLogger logger, final Level level, final Marker marker) { + this.logger = logger; + this.level = level; + this.marker = marker; + } + + public BufferedBuilder buffered() { + return new BufferedBuilder(this.logger, this.level, this.marker, 0); + } + + public LoggerFilterWriter create(final Writer writer) { + return new LoggerFilterWriter(writer, this.logger, this.level, this.marker); + } + + public Builder marker(final Marker marker) { + return new Builder(this.logger, this.level, marker); + } + + public PrintingBuilder printing() { + return new PrintingBuilder(this.logger, this.level, this.marker, false); + } + } + + public static class PrintingBuilder { + private final ExtendedLogger logger; + private final Level level; + private final Marker marker; + private final boolean autoFlush; + + PrintingBuilder(final ExtendedLogger logger, final Level level, final Marker marker, final boolean autoFlush) { + this.logger = logger; + this.level = level; + this.marker = marker; + this.autoFlush = autoFlush; + } + + public PrintingBuilder autoFlush() { + return autoFlush(true); + } + + public PrintingBuilder autoFlush(final boolean autoFlush) { + return new PrintingBuilder(this.logger, this.level, this.marker, autoFlush); + } + + public LoggerPrintStream create(final OutputStream out) { + return new LoggerPrintStream(out, this.autoFlush, this.logger, this.level, this.marker); + } + + public LoggerPrintStream create(final OutputStream out, final Charset charset) { + try { + return new LoggerPrintStream(out, this.autoFlush, charset, this.logger, this.level, this.marker); + } catch (final UnsupportedEncodingException e) { + // Should never occur because the constructor must throw this + throw new IllegalArgumentException("Invalid charset", e); + } + } + + public LoggerPrintWriter create(final Writer writer) { + return new LoggerPrintWriter(writer, this.autoFlush, this.logger, this.level, this.marker); + } + + public PrintingBuilder marker(final Marker marker) { + return new PrintingBuilder(this.logger, this.level, marker, this.autoFlush); + } + } + + public static Builder debug(final ExtendedLogger logger) { + return new Builder(logger, Level.DEBUG, null); + } + + public static Builder error(final ExtendedLogger logger) { + return new Builder(logger, Level.ERROR, null); + } + + public static Builder fatal(final ExtendedLogger logger) { + return new Builder(logger, Level.FATAL, null); + } + + public static Builder info(final ExtendedLogger logger) { + return new Builder(logger, Level.INFO, null); + } + + public static Builder trace(final ExtendedLogger logger) { + return new Builder(logger, Level.TRACE, null); + } + + public static Builder warn(final ExtendedLogger logger) { + return new Builder(logger, Level.WARN, null); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerWriter.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerWriter.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerWriter.java new file mode 100644 index 0000000..70118e8 --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/LoggerWriter.java @@ -0,0 +1,90 @@ +/* + * 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.io; + +import java.io.IOException; +import java.io.Writer; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.io.util.CharStreamLogger; + +/** + * 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 CharStreamLogger logger; + private final String fqcn; + + public LoggerWriter(final ExtendedLogger logger, final Level level) { + this(logger, FQCN, level, null); + } + + public LoggerWriter(final ExtendedLogger logger, final Level level, final Marker marker) { + this(logger, FQCN, level, marker); + } + + public LoggerWriter(final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { + this.logger = new CharStreamLogger(logger, level, marker); + this.fqcn = fqcn; + } + + @Override + public void close() throws IOException { + this.logger.close(this.fqcn); + } + + @Override + public void flush() throws IOException { + // do nothing + } + + @Override + public String toString() { + return this.getClass().getSimpleName() + "[fqcn=" + this.fqcn + ", logger=" + this.logger + "]"; + } + + @Override + public void write(final char[] cbuf) throws IOException { + this.logger.put(this.fqcn, cbuf, 0, cbuf.length); + } + + @Override + public void write(final char[] cbuf, final int off, final int len) throws IOException { + this.logger.put(this.fqcn, cbuf, off, len); + } + + @Override + public void write(final int c) throws IOException { + this.logger.put(this.fqcn, (char) c); + } + + @Override + public void write(final String str) throws IOException { + this.logger.put(this.fqcn, str, 0, str.length()); + } + + @Override + public void write(final String str, final int off, final int len) throws IOException { + this.logger.put(this.fqcn, str, off, len); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/util/ByteStreamLogger.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/util/ByteStreamLogger.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/util/ByteStreamLogger.java new file mode 100644 index 0000000..bbba82e --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/util/ByteStreamLogger.java @@ -0,0 +1,152 @@ +/* + * 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.io.util; + +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 class ByteBufferInputStream extends InputStream { + + @Override + public int read() throws IOException { + ByteStreamLogger.this.buf.flip(); + int result = -1; + if (ByteStreamLogger.this.buf.limit() > 0) { + result = ByteStreamLogger.this.buf.get() & 0xFF; + } + ByteStreamLogger.this.buf.compact(); + return result; + } + + @Override + public int read(final byte[] bytes, final int off, final int len) throws IOException { + ByteStreamLogger.this.buf.flip(); + int result = -1; + if (ByteStreamLogger.this.buf.limit() > 0) { + result = Math.min(len, ByteStreamLogger.this.buf.limit()); + ByteStreamLogger.this.buf.get(bytes, off, result); + } + ByteStreamLogger.this.buf.compact(); + return result; + } + } + + private static final int BUFFER_SIZE = 1024; + private final ExtendedLogger logger; + private final Level level; + private final Marker marker; + private final ByteBufferInputStream inputStream; + 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.inputStream = new ByteBufferInputStream(); + this.reader = new InputStreamReader(this.inputStream, charset); + } + + public void close(final String fqcn) { + synchronized (this.msg) { + this.closed = true; + logEnd(fqcn); +// in.close(); + } + } + + private void extractMessages(final String fqcn) throws IOException { + if (this.closed) { + return; + } + int read = this.reader.read(this.msgBuf); + while (read > 0) { + int off = 0; + for (int pos = 0; pos < read; pos++) { + switch (this.msgBuf[pos]) { + case '\r': + this.msg.append(this.msgBuf, off, pos - off); + off = pos + 1; + break; + case '\n': + this.msg.append(this.msgBuf, off, pos - off); + off = pos + 1; + log(fqcn); + break; + } + } + this.msg.append(this.msgBuf, off, read - off); + read = this.reader.read(this.msgBuf); + } + } + + private void log(final String fqcn) { + // convert to string now so async loggers work + this.logger.logIfEnabled(fqcn, this.level, this.marker, this.msg.toString()); + this.msg.setLength(0); + } + + private void logEnd(final String fqcn) { + if (this.msg.length() > 0) { + log(fqcn); + } + } + + public void put(final String fqcn, final byte[] b, final int off, final int len) throws IOException { + int curOff = off; + int curLen = len; + if (curLen >= 0) { + synchronized (this.msg) { + while (curLen > this.buf.remaining()) { + final int remaining = this.buf.remaining(); + this.buf.put(b, curOff, remaining); + curLen -= remaining; + curOff += remaining; + extractMessages(fqcn); + } + this.buf.put(b, curOff, curLen); + extractMessages(fqcn); + } + } else { + logEnd(fqcn); + } + } + + public void put(final String fqcn, final int b) throws IOException { + if (b >= 0) { + synchronized (this.msg) { + this.buf.put((byte) (b & 0xFF)); + extractMessages(fqcn); + } + } else { + logEnd(fqcn); + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/util/CharStreamLogger.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/util/CharStreamLogger.java b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/util/CharStreamLogger.java new file mode 100644 index 0000000..6107796 --- /dev/null +++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/util/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.io.util; + +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 close(final String fqcn) { + synchronized (this.msg) { + this.closed = true; + logEnd(fqcn); + } + } + + private void log(final String fqcn) { + // convert to string now so async loggers work + this.logger.logIfEnabled(fqcn, this.level, this.marker, this.msg.toString()); + this.msg.setLength(0); + } + + private void logEnd(final String fqcn) { + if (this.msg.length() > 0) { + log(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 (this.msg) { + if (this.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': + this.msg.append(str, start, pos); + start = pos + 1; + if (c == '\n') { + log(fqcn); + } + break; + } + } + this.msg.append(str, start, end); + } + } else { + logEnd(fqcn); + } + } + + public void put(final String fqcn, final int c) { + if (c >= 0) { + synchronized (this.msg) { + if (this.closed) { + return; + } + switch (c) { + case '\n': + log(fqcn); + break; + case '\r': + break; + default: + this.msg.append((char) c); + } + } + } else { + logEnd(fqcn); + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ffc1a38a/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/AbstractLoggerOutputStreamTest.java ---------------------------------------------------------------------- diff --git a/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/AbstractLoggerOutputStreamTest.java b/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/AbstractLoggerOutputStreamTest.java new file mode 100644 index 0000000..a41b6dd --- /dev/null +++ b/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/AbstractLoggerOutputStreamTest.java @@ -0,0 +1,138 @@ +/* + * 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.io; + +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; + +public abstract class AbstractLoggerOutputStreamTest extends AbstractStreamTest { + protected OutputStream out; + protected ByteArrayOutputStream wrapped; + + protected abstract ByteArrayOutputStream createOutputStream(); + + protected abstract OutputStream createOutputStreamWrapper(); + + @Before + public void createStream() { + this.wrapped = createOutputStream(); + this.out = createOutputStreamWrapper(); + } + + @Test + public void testClose_HasRemainingData() throws IOException { + this.out.write(FIRST.getBytes()); + assertMessages(); + this.out.close(); + assertMessages(FIRST); + if (this.wrapped != null) { + assertEquals(FIRST, this.wrapped.toString()); + } + } + + @Test + public void testClose_NoRemainingData() throws IOException { + this.out.close(); + assertMessages(); + if (this.wrapped != null) { + assertEquals("", this.wrapped.toString()); + } + } + + @Test + public void testFlush() throws IOException { + final OutputStream out = EasyMock.createMock("out", OutputStream.class); + out.flush(); // expect the flush to come through to the mocked OutputStream + out.close(); + replay(out); + + final LoggerFilterOutputStream los = new LoggerFilterOutputStream(out, getExtendedLogger(), LEVEL); + los.flush(); + los.close(); + verify(out); + } + + @Test + public void testWrite_ByteArray() throws Exception { + final byte[] bytes = "byte[]".getBytes(); + this.out.write(bytes); + assertMessages(); + this.out.write('\n'); + assertMessages("byte[]"); + if (this.wrapped != null) { + assertEquals("byte[]\n", this.wrapped.toString()); + } + } + + @Test + public void testWrite_ByteArray_Offset_Length() throws Exception { + final byte[] bytes = "byte[]".getBytes(); + final int middle = bytes.length / 2; + final int length = bytes.length - middle; + final String right = new String(bytes, middle, length); + this.out.write(bytes, middle, length); + assertMessages(); + this.out.write('\n'); + assertMessages(right); + if (this.wrapped != null) { + assertEquals("byte[]".substring(middle, bytes.length) + '\n', this.wrapped.toString()); + } + } + + @Test + public void testWrite_IgnoresWindowsNewline() throws IOException { + this.out.write(FIRST.getBytes()); + this.out.write("\r\n".getBytes()); + this.out.write(LAST.getBytes()); + this.out.close(); + assertMessages(FIRST, LAST); + if (this.wrapped != null) { + assertEquals(FIRST + "\r\n" + LAST, this.wrapped.toString()); + } + } + + @Test + public void testWrite_Int() throws Exception { + for (final byte b : "int".getBytes()) { + this.out.write(b); + assertMessages(); + } + this.out.write('\n'); + assertMessages("int"); + if (this.wrapped != null) { + assertEquals("int" + '\n', this.wrapped.toString()); + } + } + + @Test + public void testWrite_MultipleLines() throws IOException { + this.out.write((FIRST + '\n' + LAST + '\n').getBytes()); + assertMessages(FIRST, LAST); + if (this.wrapped != null) { + assertEquals(FIRST + '\n' + LAST + '\n', this.wrapped.toString()); + } + } +}
