Search in sources :

Example 1 with CloseShieldOutputStream

use of org.apache.logging.log4j.core.util.CloseShieldOutputStream in project logging-log4j2 by apache.

the class ConsoleAppender method getOutputStream.

private static OutputStream getOutputStream(final boolean follow, final boolean direct, final Target target) {
    final String enc = Charset.defaultCharset().name();
    OutputStream outputStream;
    try {
        // @formatter:off
        outputStream = target == Target.SYSTEM_OUT ? direct ? new FileOutputStream(FileDescriptor.out) : (follow ? new PrintStream(new SystemOutStream(), true, enc) : System.out) : direct ? new FileOutputStream(FileDescriptor.err) : (follow ? new PrintStream(new SystemErrStream(), true, enc) : System.err);
        // @formatter:on
        outputStream = new CloseShieldOutputStream(outputStream);
    } catch (final UnsupportedEncodingException ex) {
        // should never happen
        throw new IllegalStateException("Unsupported default encoding " + enc, ex);
    }
    final PropertiesUtil propsUtil = PropertiesUtil.getProperties();
    if (!propsUtil.isOsWindows() || propsUtil.getBooleanProperty("log4j.skipJansi") || direct) {
        return outputStream;
    }
    try {
        // We type the parameter as a wildcard to avoid a hard reference to Jansi.
        final Class<?> clazz = LoaderUtil.loadClass(JANSI_CLASS);
        final Constructor<?> constructor = clazz.getConstructor(OutputStream.class);
        return new CloseShieldOutputStream((OutputStream) constructor.newInstance(outputStream));
    } catch (final ClassNotFoundException cnfe) {
        LOGGER.debug("Jansi is not installed, cannot find {}", JANSI_CLASS);
    } catch (final NoSuchMethodException nsme) {
        LOGGER.warn("{} is missing the proper constructor", JANSI_CLASS);
    } catch (final Exception ex) {
        LOGGER.warn("Unable to instantiate {}", JANSI_CLASS);
    }
    return outputStream;
}
Also used : PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CloseShieldOutputStream(org.apache.logging.log4j.core.util.CloseShieldOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileOutputStream(java.io.FileOutputStream) CloseShieldOutputStream(org.apache.logging.log4j.core.util.CloseShieldOutputStream)

Example 2 with CloseShieldOutputStream

use of org.apache.logging.log4j.core.util.CloseShieldOutputStream in project logging-log4j2 by apache.

the class OutputStreamAppender method getManager.

private static OutputStreamManager getManager(final OutputStream target, final boolean follow, final Layout<? extends Serializable> layout) {
    final OutputStream os = new CloseShieldOutputStream(target);
    final String managerName = target.getClass().getName() + "@" + Integer.toHexString(target.hashCode()) + '.' + follow;
    return OutputStreamManager.getManager(managerName, new FactoryData(os, managerName, layout), factory);
}
Also used : OutputStream(java.io.OutputStream) CloseShieldOutputStream(org.apache.logging.log4j.core.util.CloseShieldOutputStream) CloseShieldOutputStream(org.apache.logging.log4j.core.util.CloseShieldOutputStream)

Aggregations

OutputStream (java.io.OutputStream)2 CloseShieldOutputStream (org.apache.logging.log4j.core.util.CloseShieldOutputStream)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)1