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     */
017    package org.apache.logging.log4j.core.appender;
018    
019    import org.apache.logging.log4j.core.Filter;
020    import org.apache.logging.log4j.core.Layout;
021    import org.apache.logging.log4j.core.config.Configuration;
022    import org.apache.logging.log4j.core.config.plugins.Plugin;
023    import org.apache.logging.log4j.core.config.plugins.PluginAttr;
024    import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
025    import org.apache.logging.log4j.core.config.plugins.PluginElement;
026    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
027    import org.apache.logging.log4j.core.layout.PatternLayout;
028    import org.apache.logging.log4j.core.net.Advertiser;
029    
030    import java.io.Serializable;
031    import java.util.HashMap;
032    import java.util.Map;
033    
034    /**
035     * File Appender.
036     */
037    @Plugin(name = "File", category = "Core", elementType = "appender", printObject = true)
038    public final class FileAppender<T extends Serializable> extends AbstractOutputStreamAppender<T> {
039    
040        private final String fileName;
041        private final Advertiser advertiser;
042        private Object advertisement;
043    
044        private FileAppender(final String name, final Layout<T> layout, final Filter filter, final FileManager manager,
045                             final String filename, final boolean handleException, final boolean immediateFlush,
046                             Advertiser advertiser) {
047            super(name, layout, filter, handleException, immediateFlush, manager);
048            if (advertiser != null)
049            {
050                Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat());
051                configuration.putAll(manager.getContentFormat());
052                configuration.put("contentType", layout.getContentType());
053                configuration.put("name", name);
054                advertisement = advertiser.advertise(configuration);
055            }
056            this.fileName = filename;
057            this.advertiser = advertiser;
058        }
059    
060        @Override
061        public void stop() {
062            super.stop();
063            if (advertiser != null) {
064                advertiser.unadvertise(advertisement);
065            }
066        }
067    
068        /**
069         * Returns the file name this appender is associated with.
070         * @return The File name.
071         */
072        public String getFileName() {
073            return this.fileName;
074        }
075    
076        /**
077         * Create a File Appender.
078         * @param fileName The name and path of the file.
079         * @param append "True" if the file should be appended to, "false" if it should be overwritten.
080         * The default is "true".
081         * @param locking "True" if the file should be locked. The default is "false".
082         * @param name The name of the Appender.
083         * @param immediateFlush "true" if the contents should be flushed on every write, "false" otherwise. The default
084         * is "true".
085         * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
086         * The default is "true".
087         * @param bufferedIO "true" if I/O should be buffered, "false" otherwise. The default is "true".
088         * @param layout The layout to use to format the event. If no layout is provided the default PatternLayout
089         * will be used.
090         * @param filter The filter, if any, to use.
091         * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
092         * @param advertiseURI The advertised URI which can be used to retrieve the file contents.
093         * @param config The Configuration
094         * @return The FileAppender.
095         */
096        @PluginFactory
097        public static <S extends Serializable> FileAppender<S> createAppender(@PluginAttr("fileName") final String fileName,
098                                                  @PluginAttr("append") final String append,
099                                                  @PluginAttr("locking") final String locking,
100                                                  @PluginAttr("name") final String name,
101                                                  @PluginAttr("immediateFlush") final String immediateFlush,
102                                                  @PluginAttr("suppressExceptions") final String suppress,
103                                                  @PluginAttr("bufferedIO") final String bufferedIO,
104                                                  @PluginElement("layout") Layout<S> layout,
105                                                  @PluginElement("filters") final Filter filter,
106                                                  @PluginAttr("advertise") final String advertise,
107                                                  @PluginAttr("advertiseURI") final String advertiseURI,
108                                                  @PluginConfiguration final Configuration config) {
109    
110            final boolean isAppend = append == null ? true : Boolean.valueOf(append);
111            final boolean isLocking = locking == null ? false : Boolean.valueOf(locking);
112            boolean isBuffered = bufferedIO == null ? true : Boolean.valueOf(bufferedIO);
113            boolean isAdvertise = advertise == null ? false : Boolean.valueOf(advertise);
114            if (isLocking && isBuffered) {
115                if (bufferedIO != null) {
116                    LOGGER.warn("Locking and buffering are mutually exclusive. No buffering will occur for " + fileName);
117                }
118                isBuffered = false;
119            }
120            final boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);
121            final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
122    
123            if (name == null) {
124                LOGGER.error("No name provided for FileAppender");
125                return null;
126            }
127    
128            if (fileName == null) {
129                LOGGER.error("No filename provided for FileAppender with name "  + name);
130                return null;
131            }
132            if (layout == null) {
133                @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
134                Layout<S> l = (Layout<S>)PatternLayout.createLayout(null, null, null, null, null);
135                layout = l;
136            }
137    
138            final FileManager manager = FileManager.getFileManager(fileName, isAppend, isLocking, isBuffered, advertiseURI,
139                layout);
140            if (manager == null) {
141                return null;
142            }
143    
144    
145            return new FileAppender<S>(name, layout, filter, manager, fileName, handleExceptions, isFlush,
146                    isAdvertise ? config.getAdvertiser() : null);
147        }
148    }