"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "log4j-core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java" between
apache-log4j-2.11.1-src.tar.gz and apache-log4j-2.11.2-src.tar.gz

About: Apache Log4j 2 is a logging library for Java. Source distribution (Java).

ConsoleAppender.java  (apache-log4j-2.11.1-src):ConsoleAppender.java  (apache-log4j-2.11.2-src)
skipping to change at line 19 skipping to change at line 19
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license for the specific language governing permissions and * See the license for the specific language governing permissions and
* limitations under the license. * limitations under the license.
*/ */
package org.apache.logging.log4j.core.appender; package org.apache.logging.log4j.core.appender;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core; import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Requi red; import org.apache.logging.log4j.core.config.plugins.validation.constraints.Requi red;
import org.apache.logging.log4j.core.layout.PatternLayout; import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.util.Booleans; import org.apache.logging.log4j.core.util.Booleans;
import org.apache.logging.log4j.core.util.CloseShieldOutputStream; import org.apache.logging.log4j.core.util.CloseShieldOutputStream;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.core.util.Throwables; import org.apache.logging.log4j.core.util.Throwables;
import org.apache.logging.log4j.util.Chars; import org.apache.logging.log4j.util.Chars;
import org.apache.logging.log4j.util.LoaderUtil;
import org.apache.logging.log4j.util.PropertiesUtil; import org.apache.logging.log4j.util.PropertiesUtil;
import java.io.*;
import java.lang.reflect.Constructor;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Appends log events to <code>System.out</code> or <code>System.err</code> usin g a layout specified by the user. The * Appends log events to <code>System.out</code> or <code>System.err</code> usin g a layout specified by the user. The
* default target is <code>System.out</code>. * default target is <code>System.out</code>.
* <p> * <p>
* TODO Accessing <code>System.out</code> or <code>System.err</code> as a byte s tream instead of a writer bypasses the * TODO Accessing <code>System.out</code> or <code>System.err</code> as a byte s tream instead of a writer bypasses the
* JVM's knowledge of the proper encoding. (RG) Encoding is handled within the L ayout. Typically, a Layout will generate * JVM's knowledge of the proper encoding. (RG) Encoding is handled within the L ayout. Typically, a Layout will generate
* a String and then call getBytes which may use a configured encoding or the sy stem default. OTOH, a Writer cannot * a String and then call getBytes which may use a configured encoding or the sy stem default. OTOH, a Writer cannot
* print byte streams. * print byte streams.
* </p> * </p>
*/ */
skipping to change at line 92 skipping to change at line 87
SYSTEM_ERR { SYSTEM_ERR {
@Override @Override
public Charset getDefaultCharset() { public Charset getDefaultCharset() {
// "sun.stderr.encoding" is only set when running from the conso le. // "sun.stderr.encoding" is only set when running from the conso le.
return getCharset("sun.stderr.encoding", Charset.defaultCharset( )); return getCharset("sun.stderr.encoding", Charset.defaultCharset( ));
} }
}; };
public abstract Charset getDefaultCharset(); public abstract Charset getDefaultCharset();
protected Charset getCharset(final String property, Charset defaultChars et) { protected Charset getCharset(final String property, final Charset defaul tCharset) {
return new PropertiesUtil(PropertiesUtil.getSystemProperties()).getC harsetProperty(property, defaultCharset); return new PropertiesUtil(PropertiesUtil.getSystemProperties()).getC harsetProperty(property, defaultCharset);
} }
} }
private ConsoleAppender(final String name, final Layout<? extends Serializab le> layout, final Filter filter, private ConsoleAppender(final String name, final Layout<? extends Serializab le> layout, final Filter filter,
final OutputStreamManager manager, final boolean ignoreExceptions, f final OutputStreamManager manager, final boolean ignoreExceptions, f
inal Target target) { inal Target target,
super(name, layout, filter, ignoreExceptions, true, manager); final Property[] properties) {
super(name, layout, filter, ignoreExceptions, true, properties, manager)
;
this.target = target; this.target = target;
} }
/** /**
* Creates a Console Appender. * Creates a Console Appender.
* *
* @param layout The layout to use (required). * @param layout The layout to use (required).
* @param filter The Filter or null. * @param filter The Filter or null.
* @param targetStr The target ("SYSTEM_OUT" or "SYSTEM_ERR"). The default i s "SYSTEM_OUT". * @param targetStr The target ("SYSTEM_OUT" or "SYSTEM_ERR"). The default i s "SYSTEM_OUT".
* @param name The name of the Appender (required). * @param name The name of the Appender (required).
skipping to change at line 134 skipping to change at line 130
if (name == null) { if (name == null) {
LOGGER.error("No name provided for ConsoleAppender"); LOGGER.error("No name provided for ConsoleAppender");
return null; return null;
} }
if (layout == null) { if (layout == null) {
layout = PatternLayout.createDefaultLayout(); layout = PatternLayout.createDefaultLayout();
} }
final boolean isFollow = Boolean.parseBoolean(follow); final boolean isFollow = Boolean.parseBoolean(follow);
final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
final Target target = targetStr == null ? DEFAULT_TARGET : Target.valueO f(targetStr); final Target target = targetStr == null ? DEFAULT_TARGET : Target.valueO f(targetStr);
return new ConsoleAppender(name, layout, filter, getManager(target, isFo llow, false, layout), ignoreExceptions, target); return new ConsoleAppender(name, layout, filter, getManager(target, isFo llow, false, layout), ignoreExceptions, target, null);
} }
/** /**
* Creates a Console Appender. * Creates a Console Appender.
* *
* @param layout The layout to use (required). * @param layout The layout to use (required).
* @param filter The Filter or null. * @param filter The Filter or null.
* @param target The target (SYSTEM_OUT or SYSTEM_ERR). The default is SYSTE M_OUT. * @param target The target (SYSTEM_OUT or SYSTEM_ERR). The default is SYSTE M_OUT.
* @param name The name of the Appender (required). * @param name The name of the Appender (required).
* @param follow If true will follow changes to the underlying output stream . * @param follow If true will follow changes to the underlying output stream .
skipping to change at line 175 skipping to change at line 171
return null; return null;
} }
if (layout == null) { if (layout == null) {
layout = PatternLayout.createDefaultLayout(); layout = PatternLayout.createDefaultLayout();
} }
target = target == null ? Target.SYSTEM_OUT : target; target = target == null ? Target.SYSTEM_OUT : target;
if (follow && direct) { if (follow && direct) {
LOGGER.error("Cannot use both follow and direct on ConsoleAppender") ; LOGGER.error("Cannot use both follow and direct on ConsoleAppender") ;
return null; return null;
} }
return new ConsoleAppender(name, layout, filter, getManager(target, foll ow, direct, layout), ignoreExceptions, target); return new ConsoleAppender(name, layout, filter, getManager(target, foll ow, direct, layout), ignoreExceptions, target, null);
} }
public static ConsoleAppender createDefaultAppenderForLayout(final Layout<? extends Serializable> layout) { public static ConsoleAppender createDefaultAppenderForLayout(final Layout<? extends Serializable> layout) {
// this method cannot use the builder class without introducing an infin ite loop due to DefaultConfiguration // this method cannot use the builder class without introducing an infin ite loop due to DefaultConfiguration
return new ConsoleAppender("DefaultConsole-" + COUNT.incrementAndGet(), layout, null, return new ConsoleAppender("DefaultConsole-" + COUNT.incrementAndGet(), layout, null,
getDefaultManager(DEFAULT_TARGET, false, false, layout), true, D EFAULT_TARGET); getDefaultManager(DEFAULT_TARGET, false, false, layout), true, D EFAULT_TARGET, null);
} }
@PluginBuilderFactory @PluginBuilderFactory
public static <B extends Builder<B>> B newBuilder() { public static <B extends Builder<B>> B newBuilder() {
return new Builder<B>().asBuilder(); return new Builder<B>().asBuilder();
} }
/** /**
* Builds ConsoleAppender instances. * Builds ConsoleAppender instances.
* @param <B> The type to build * @param <B> The type to build
skipping to change at line 228 skipping to change at line 224
return asBuilder(); return asBuilder();
} }
@Override @Override
public ConsoleAppender build() { public ConsoleAppender build() {
if (follow && direct) { if (follow && direct) {
throw new IllegalArgumentException("Cannot use both follow and d irect on ConsoleAppender '" + getName() + "'"); throw new IllegalArgumentException("Cannot use both follow and d irect on ConsoleAppender '" + getName() + "'");
} }
final Layout<? extends Serializable> layout = getOrCreateLayout(targ et.getDefaultCharset()); final Layout<? extends Serializable> layout = getOrCreateLayout(targ et.getDefaultCharset());
return new ConsoleAppender(getName(), layout, getFilter(), getManage r(target, follow, direct, layout), return new ConsoleAppender(getName(), layout, getFilter(), getManage r(target, follow, direct, layout),
isIgnoreExceptions(), target); isIgnoreExceptions(), target, getPropertyArray());
} }
} }
private static OutputStreamManager getDefaultManager(final Target target, fi nal boolean follow, final boolean direct, private static OutputStreamManager getDefaultManager(final Target target, fi nal boolean follow, final boolean direct,
final Layout<? extends Serializable> layout) { final Layout<? extends Serializable> layout) {
final OutputStream os = getOutputStream(follow, direct, target); final OutputStream os = getOutputStream(follow, direct, target);
// LOG4J2-1176 DefaultConfiguration should not share OutputStreamManager instances to avoid memory leaks. // LOG4J2-1176 DefaultConfiguration should not share OutputStreamManager instances to avoid memory leaks.
final String managerName = target.name() + '.' + follow + '.' + direct + "-" + COUNT.get(); final String managerName = target.name() + '.' + follow + '.' + direct + "-" + COUNT.get();
return OutputStreamManager.getManager(managerName, new FactoryData(os, m anagerName, layout), factory); return OutputStreamManager.getManager(managerName, new FactoryData(os, m anagerName, layout), factory);
skipping to change at line 269 skipping to change at line 265
outputStream = new CloseShieldOutputStream(outputStream); outputStream = new CloseShieldOutputStream(outputStream);
} catch (final UnsupportedEncodingException ex) { // should never happen } catch (final UnsupportedEncodingException ex) { // should never happen
throw new IllegalStateException("Unsupported default encoding " + en c, ex); throw new IllegalStateException("Unsupported default encoding " + en c, ex);
} }
final PropertiesUtil propsUtil = PropertiesUtil.getProperties(); final PropertiesUtil propsUtil = PropertiesUtil.getProperties();
if (!propsUtil.isOsWindows() || propsUtil.getBooleanProperty("log4j.skip Jansi", true) || direct) { if (!propsUtil.isOsWindows() || propsUtil.getBooleanProperty("log4j.skip Jansi", true) || direct) {
return outputStream; return outputStream;
} }
try { try {
// We type the parameter as a wildcard to avoid a hard reference to Jansi. // We type the parameter as a wildcard to avoid a hard reference to Jansi.
final Class<?> clazz = LoaderUtil.loadClass(JANSI_CLASS); final Class<?> clazz = Loader.loadClass(JANSI_CLASS);
final Constructor<?> constructor = clazz.getConstructor(OutputStream .class); final Constructor<?> constructor = clazz.getConstructor(OutputStream .class);
return new CloseShieldOutputStream((OutputStream) constructor.newIns tance(outputStream)); return new CloseShieldOutputStream((OutputStream) constructor.newIns tance(outputStream));
} catch (final ClassNotFoundException cnfe) { } catch (final ClassNotFoundException cnfe) {
LOGGER.debug("Jansi is not installed, cannot find {}", JANSI_CLASS); LOGGER.debug("Jansi is not installed, cannot find {}", JANSI_CLASS);
} catch (final NoSuchMethodException nsme) { } catch (final NoSuchMethodException nsme) {
LOGGER.warn("{} is missing the proper constructor", JANSI_CLASS); LOGGER.warn("{} is missing the proper constructor", JANSI_CLASS);
} catch (final Exception ex) { } catch (final Exception ex) {
LOGGER.warn("Unable to instantiate {} due to {}", JANSI_CLASS, clean (Throwables.getRootCause(ex).toString()).trim()); LOGGER.warn("Unable to instantiate {} due to {}", JANSI_CLASS, clean (Throwables.getRootCause(ex).toString()).trim());
} }
return outputStream; return outputStream;
} }
private static String clean(String string) { private static String clean(final String string) {
return string.replace(Chars.NUL, Chars.SPACE); return string.replace(Chars.NUL, Chars.SPACE);
} }
/** /**
* An implementation of OutputStream that redirects to the current System.er r. * An implementation of OutputStream that redirects to the current System.er r.
*/ */
private static class SystemErrStream extends OutputStream { private static class SystemErrStream extends OutputStream {
public SystemErrStream() { public SystemErrStream() {
} }
 End of changes. 13 change blocks. 
22 lines changed or deleted 19 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)