/** * Create a RollingRandomAccessFileAppender. * * @param fileName The name of the file that is actively written to. (required). * @param filePattern The pattern of the file name to use on rollover. (required). * @param append If true, events are appended to the file. If false, the file is overwritten when * opened. Defaults to "true" * @param name The name of the Appender (required). * @param immediateFlush When true, events are immediately flushed. Defaults to "true". * @param bufferSizeStr The buffer size, defaults to {@value * RollingRandomAccessFileManager#DEFAULT_BUFFER_SIZE}. * @param policy The triggering policy. (required). * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy. * @param layout The layout to use (defaults to the default PatternLayout). * @param filter The Filter or null. * @param ignore If {@code "true"} (default) exceptions encountered when appending events are * logged; otherwise they are propagated to the caller. * @param advertise "true" if the appender configuration should be advertised, "false" otherwise. * @param advertiseURI The advertised URI which can be used to retrieve the file contents. * @param config The Configuration. * @return A RollingRandomAccessFileAppender. */ @PluginFactory public static RollingRandomAccessFileAppender createAppender( @PluginAttribute("fileName") final String fileName, @PluginAttribute("filePattern") final String filePattern, @PluginAttribute("append") final String append, @PluginAttribute("name") final String name, @PluginAttribute("immediateFlush") final String immediateFlush, @PluginAttribute("bufferSize") final String bufferSizeStr, @PluginElement("Policy") final TriggeringPolicy policy, @PluginElement("Strategy") RolloverStrategy strategy, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter, @PluginAttribute("ignoreExceptions") final String ignore, @PluginAttribute("advertise") final String advertise, @PluginAttribute("advertiseURI") final String advertiseURI, @PluginConfiguration final Configuration config) { final boolean isAppend = Booleans.parseBoolean(append, true); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); final boolean isFlush = Booleans.parseBoolean(immediateFlush, true); final boolean isAdvertise = Boolean.parseBoolean(advertise); final int bufferSize = Integers.parseInt(bufferSizeStr, RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE); if (name == null) { LOGGER.error("No name provided for FileAppender"); return null; } if (fileName == null) { LOGGER.error("No filename was provided for FileAppender with name " + name); return null; } if (filePattern == null) { LOGGER.error("No filename pattern provided for FileAppender with name " + name); return null; } if (policy == null) { LOGGER.error("A TriggeringPolicy must be provided"); return null; } if (strategy == null) { strategy = DefaultRolloverStrategy.createStrategy( null, null, null, String.valueOf(Deflater.DEFAULT_COMPRESSION), config); } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager.getRollingRandomAccessFileManager( fileName, filePattern, isAppend, isFlush, bufferSize, policy, strategy, advertiseURI, layout); if (manager == null) { return null; } return new RollingRandomAccessFileAppender( name, layout, filter, manager, fileName, filePattern, ignoreExceptions, isFlush, bufferSize, isAdvertise ? config.getAdvertiser() : null); }
public org.slf4j.Logger getLogger(String name) { name = StringUtil2.replace(name.trim(), ' ', "_"); org.slf4j.Logger result = map.get(name); if (result != null) return result; try { String fileName = dir + "/" + name + ".log"; String fileNamePattern = dir + "/" + name + "%i.log"; // create logger in log4j2 // TODO: There are Builders that make this logger creation less awkward. Configuration config = new NullConfiguration(); // LOOK: Why are we using this? Why not DefaultConfiguration? PatternLayout layout = PatternLayout.createLayout( "%d{yyyy-MM-dd'T'HH:mm:ss.SSS Z} %-5p - %m%n", // String pattern config, // Configuration config null, // RegexReplacement replace null, // Charset charset true, // boolean alwaysWriteExceptions false, // boolean noConsoleNoAnsi null, // String header null // String footer ); RollingFileAppender app = RollingFileAppender.createAppender( fileName, // String fileName fileNamePattern, // String filePattern "true", // String append name, // String name "true", // String bufferedIO null, // String bufferSizeStr "true", // String immediateFlush SizeBasedTriggeringPolicy.createPolicy( Long.toString(maxSize)), // TriggeringPolicy policy DefaultRolloverStrategy.createStrategy( // RolloverStrategy strategy Integer.toString(maxBackups), // String max "1", // String min "max", // String fileIndex null, // String compressionLevelStr config // Configuration config ), layout, // Layout<? extends Serializable> layout null, // Filter filter "true", // String ignore "false", // String advertise null, // String advertiseURI config); // Configuration config app.start(); /*LoggerContext ctx = (LoggerContext) LogManager.getContext(false); Configuration conf = ctx.getConfiguration(); LoggerConfig lconf = conf.getLoggerConfig(name); lconf.setAdditive(false); // otherwise, it also gets sent to root logger (threddsServlet.log) lconf.setLevel(level); lconf.addAppender(app, level, null); ctx.updateLoggers(conf); */ org.apache.logging.log4j.core.Logger log4j = (org.apache.logging.log4j.core.Logger) LogManager.getLogger(name); log4j.addAppender(app); log4j.setLevel(level); log4j.setAdditive(false); // otherwise, it also gets sent to root logger (threddsServlet.log) startupLog.info("LoggerFactorySpecial add logger= {} file= {}", name, fileName); result = org.slf4j.LoggerFactory.getLogger(name); // get wrapper in slf4j map.put(name, result); return result; } catch (Throwable ioe) { startupLog.error("LoggerFactorySpecial failed on " + name, ioe); // standard slf4j - rely on external configuration return org.slf4j.LoggerFactory.getLogger(name); } }