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