001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender;
018
019import java.io.FileDescriptor;
020import java.io.FileOutputStream;
021import java.io.IOException;
022import java.io.OutputStream;
023import java.io.PrintStream;
024import java.io.Serializable;
025import java.io.UnsupportedEncodingException;
026import java.lang.reflect.Constructor;
027import java.nio.charset.Charset;
028import java.util.concurrent.atomic.AtomicInteger;
029
030import org.apache.logging.log4j.core.Appender;
031import org.apache.logging.log4j.core.Core;
032import org.apache.logging.log4j.core.Filter;
033import org.apache.logging.log4j.core.Layout;
034import org.apache.logging.log4j.core.config.plugins.Plugin;
035import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
036import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
037import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
038import org.apache.logging.log4j.core.layout.PatternLayout;
039import org.apache.logging.log4j.core.util.Booleans;
040import org.apache.logging.log4j.core.util.CloseShieldOutputStream;
041import org.apache.logging.log4j.util.LoaderUtil;
042import org.apache.logging.log4j.util.PropertiesUtil;
043
044/**
045 * Appends log events to <code>System.out</code> or <code>System.err</code> using a layout specified by the user. The
046 * default target is <code>System.out</code>.
047 * <p>
048 * TODO Accessing <code>System.out</code> or <code>System.err</code> as a byte stream instead of a writer bypasses the
049 * JVM's knowledge of the proper encoding. (RG) Encoding is handled within the Layout. Typically, a Layout will generate
050 * a String and then call getBytes which may use a configured encoding or the system default. OTOH, a Writer cannot
051 * print byte streams.
052 * </p>
053 */
054@Plugin(name = ConsoleAppender.PLUGIN_NAME, category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
055public final class ConsoleAppender extends AbstractOutputStreamAppender<OutputStreamManager> {
056
057    public static final String PLUGIN_NAME = "Console";
058    private static final String JANSI_CLASS = "org.fusesource.jansi.WindowsAnsiOutputStream";
059    private static ConsoleManagerFactory factory = new ConsoleManagerFactory();
060    private static final Target DEFAULT_TARGET = Target.SYSTEM_OUT;
061    private static final AtomicInteger COUNT = new AtomicInteger();
062
063    private final Target target;
064
065    /**
066     * Enumeration of console destinations.
067     */
068    public enum Target {
069        
070        /** Standard output. */
071        SYSTEM_OUT {
072            @Override
073            public Charset getDefaultCharset() {
074                return getCharset("sun.stdout.encoding", Charset.defaultCharset());
075            }
076        },
077        
078        /** Standard error output. */
079        SYSTEM_ERR {
080            @Override
081            public Charset getDefaultCharset() {
082                return getCharset("sun.stderr.encoding", Charset.defaultCharset());
083            }
084        };
085        
086        public abstract Charset getDefaultCharset();
087        
088        protected Charset getCharset(final String property, Charset defaultCharset) {
089            return new PropertiesUtil(PropertiesUtil.getSystemProperties()).getCharsetProperty(property, defaultCharset);
090        }
091
092    }
093
094    private ConsoleAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
095            final OutputStreamManager manager, final boolean ignoreExceptions, final Target target) {
096        super(name, layout, filter, ignoreExceptions, true, manager);
097        this.target = target;
098    }
099
100    /**
101     * Creates a Console Appender.
102     *
103     * @param layout The layout to use (required).
104     * @param filter The Filter or null.
105     * @param targetStr The target ("SYSTEM_OUT" or "SYSTEM_ERR"). The default is "SYSTEM_OUT".
106     * @param name The name of the Appender (required).
107     * @param follow If true will follow changes to the underlying output stream.
108     * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise they
109     *            are propagated to the caller.
110     * @return The ConsoleAppender.
111     * @deprecated Deprecated in 2.7; use {@link #newBuilder()}.
112     */
113    @Deprecated
114    public static ConsoleAppender createAppender(Layout<? extends Serializable> layout,
115            final Filter filter,
116            final String targetStr,
117            final String name,
118            final String follow,
119            final String ignore) {
120        if (name == null) {
121            LOGGER.error("No name provided for ConsoleAppender");
122            return null;
123        }
124        if (layout == null) {
125            layout = PatternLayout.createDefaultLayout();
126        }
127        final boolean isFollow = Boolean.parseBoolean(follow);
128        final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
129        final Target target = targetStr == null ? DEFAULT_TARGET : Target.valueOf(targetStr);
130        return new ConsoleAppender(name, layout, filter, getManager(target, isFollow, false, layout), ignoreExceptions, target);
131    }
132
133    /**
134     * Creates a Console Appender.
135     *
136     * @param layout The layout to use (required).
137     * @param filter The Filter or null.
138     * @param target The target (SYSTEM_OUT or SYSTEM_ERR). The default is SYSTEM_OUT.
139     * @param name The name of the Appender (required).
140     * @param follow If true will follow changes to the underlying output stream.
141     * @param direct If true will write directly to {@link java.io.FileDescriptor} and bypass
142     *            {@link System#out}/{@link System#err}.
143     * @param ignoreExceptions If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise they
144     *            are propagated to the caller.
145     * @return The ConsoleAppender.
146     * @deprecated Deprecated in 2.7; use {@link #newBuilder()}.
147     */
148    @Deprecated
149    public static ConsoleAppender createAppender(
150            // @formatter:off
151            Layout<? extends Serializable> layout,
152            final Filter filter,
153            Target target,
154            final String name,
155            final boolean follow,
156            final boolean direct,
157            final boolean ignoreExceptions) {
158            // @formatter:on
159        if (name == null) {
160            LOGGER.error("No name provided for ConsoleAppender");
161            return null;
162        }
163        if (layout == null) {
164            layout = PatternLayout.createDefaultLayout();
165        }
166        target = target == null ? Target.SYSTEM_OUT : target;
167        if (follow && direct) {
168            LOGGER.error("Cannot use both follow and direct on ConsoleAppender");
169            return null;
170        }
171        return new ConsoleAppender(name, layout, filter, getManager(target, follow, direct, layout), ignoreExceptions, target);
172    }
173
174    public static ConsoleAppender createDefaultAppenderForLayout(final Layout<? extends Serializable> layout) {
175        // this method cannot use the builder class without introducing an infinite loop due to DefaultConfiguration
176        return new ConsoleAppender("DefaultConsole-" + COUNT.incrementAndGet(), layout, null,
177                getDefaultManager(DEFAULT_TARGET, false, false, layout), true, DEFAULT_TARGET);
178    }
179
180    @PluginBuilderFactory
181    public static <B extends Builder<B>> B newBuilder() {
182        return new Builder<B>().asBuilder();
183    }
184
185    /**
186     * Builds ConsoleAppender instances.
187     * @param <B> The type to build
188     */
189    public static class Builder<B extends Builder<B>> extends AbstractOutputStreamAppender.Builder<B>
190            implements org.apache.logging.log4j.core.util.Builder<ConsoleAppender> {
191
192        @PluginBuilderAttribute
193        @Required
194        private Target target = DEFAULT_TARGET;
195
196        @PluginBuilderAttribute
197        private boolean follow;
198
199        @PluginBuilderAttribute
200        private boolean direct;
201
202        public B setTarget(final Target aTarget) {
203            this.target = aTarget;
204            return asBuilder();
205        }
206
207        public B setFollow(final boolean shouldFollow) {
208            this.follow = shouldFollow;
209            return asBuilder();
210        }
211
212        public B setDirect(final boolean shouldDirect) {
213            this.direct = shouldDirect;
214            return asBuilder();
215        }
216
217        @Override
218        public ConsoleAppender build() {
219            if (follow && direct) {
220                throw new IllegalArgumentException("Cannot use both follow and direct on ConsoleAppender '" + getName() + "'");
221            }
222            final Layout<? extends Serializable> layout = getOrCreateLayout(target.getDefaultCharset());
223            return new ConsoleAppender(getName(), layout, getFilter(), getManager(target, follow, direct, layout),
224                    isIgnoreExceptions(), target);
225        }
226    }
227
228    private static OutputStreamManager getDefaultManager(final Target target, final boolean follow, final boolean direct,
229            final Layout<? extends Serializable> layout) {
230        final OutputStream os = getOutputStream(follow, direct, target);
231
232        // LOG4J2-1176 DefaultConfiguration should not share OutputStreamManager instances to avoid memory leaks.
233        final String managerName = target.name() + '.' + follow + '.' + direct + "-" + COUNT.get();
234        return OutputStreamManager.getManager(managerName, new FactoryData(os, managerName, layout), factory);
235    }
236
237    private static OutputStreamManager getManager(final Target target, final boolean follow, final boolean direct,
238            final Layout<? extends Serializable> layout) {
239        final OutputStream os = getOutputStream(follow, direct, target);
240        final String managerName = target.name() + '.' + follow + '.' + direct;
241        return OutputStreamManager.getManager(managerName, new FactoryData(os, managerName, layout), factory);
242    }
243
244    private static OutputStream getOutputStream(final boolean follow, final boolean direct, final Target target) {
245        final String enc = Charset.defaultCharset().name();
246        OutputStream outputStream;
247        try {
248            // @formatter:off
249            outputStream = target == Target.SYSTEM_OUT ?
250                direct ? new FileOutputStream(FileDescriptor.out) :
251                    (follow ? new PrintStream(new SystemOutStream(), true, enc) : System.out) :
252                direct ? new FileOutputStream(FileDescriptor.err) :
253                    (follow ? new PrintStream(new SystemErrStream(), true, enc) : System.err);
254            // @formatter:on
255            outputStream = new CloseShieldOutputStream(outputStream);
256        } catch (final UnsupportedEncodingException ex) { // should never happen
257            throw new IllegalStateException("Unsupported default encoding " + enc, ex);
258        }
259        final PropertiesUtil propsUtil = PropertiesUtil.getProperties();
260        if (!propsUtil.isOsWindows() || propsUtil.getBooleanProperty("log4j.skipJansi") || direct) {
261            return outputStream;
262        }
263        try {
264            // We type the parameter as a wildcard to avoid a hard reference to Jansi.
265            final Class<?> clazz = LoaderUtil.loadClass(JANSI_CLASS);
266            final Constructor<?> constructor = clazz.getConstructor(OutputStream.class);
267            return new CloseShieldOutputStream((OutputStream) constructor.newInstance(outputStream));
268        } catch (final ClassNotFoundException cnfe) {
269            LOGGER.debug("Jansi is not installed, cannot find {}", JANSI_CLASS);
270        } catch (final NoSuchMethodException nsme) {
271            LOGGER.warn("{} is missing the proper constructor", JANSI_CLASS);
272        } catch (final Exception ex) {
273            LOGGER.warn("Unable to instantiate {}", JANSI_CLASS);
274        }
275        return outputStream;
276    }
277
278    /**
279     * An implementation of OutputStream that redirects to the current System.err.
280     */
281    private static class SystemErrStream extends OutputStream {
282        public SystemErrStream() {
283        }
284
285        @Override
286        public void close() {
287            // do not close sys err!
288        }
289
290        @Override
291        public void flush() {
292            System.err.flush();
293        }
294
295        @Override
296        public void write(final byte[] b) throws IOException {
297            System.err.write(b);
298        }
299
300        @Override
301        public void write(final byte[] b, final int off, final int len) throws IOException {
302            System.err.write(b, off, len);
303        }
304
305        @Override
306        public void write(final int b) {
307            System.err.write(b);
308        }
309    }
310
311    /**
312     * An implementation of OutputStream that redirects to the current System.out.
313     */
314    private static class SystemOutStream extends OutputStream {
315        public SystemOutStream() {
316        }
317
318        @Override
319        public void close() {
320            // do not close sys out!
321        }
322
323        @Override
324        public void flush() {
325            System.out.flush();
326        }
327
328        @Override
329        public void write(final byte[] b) throws IOException {
330            System.out.write(b);
331        }
332
333        @Override
334        public void write(final byte[] b, final int off, final int len) throws IOException {
335            System.out.write(b, off, len);
336        }
337
338        @Override
339        public void write(final int b) throws IOException {
340            System.out.write(b);
341        }
342    }
343
344    /**
345     * Data to pass to factory method.
346     */
347    private static class FactoryData {
348        private final OutputStream os;
349        private final String name;
350        private final Layout<? extends Serializable> layout;
351
352        /**
353         * Constructor.
354         *
355         * @param os The OutputStream.
356         * @param type The name of the target.
357         * @param layout A Serializable layout
358         */
359        public FactoryData(final OutputStream os, final String type, final Layout<? extends Serializable> layout) {
360            this.os = os;
361            this.name = type;
362            this.layout = layout;
363        }
364    }
365
366    /**
367     * Factory to create the Appender.
368     */
369    private static class ConsoleManagerFactory implements ManagerFactory<OutputStreamManager, FactoryData> {
370
371        /**
372         * Create an OutputStreamManager.
373         *
374         * @param name The name of the entity to manage.
375         * @param data The data required to create the entity.
376         * @return The OutputStreamManager
377         */
378        @Override
379        public OutputStreamManager createManager(final String name, final FactoryData data) {
380            return new OutputStreamManager(data.os, data.name, data.layout, true);
381        }
382    }
383
384    public Target getTarget() {
385        return target;
386    }
387
388}