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