/** * Tests https://issues.apache.org/jira/browse/LOG4J2-1620 */ @Test public void testDefaultImmediateFlush() { Assert.assertTrue(FileAppender.newBuilder().isImmediateFlush()); } }
/** * Validates that the code pattern we use to add an appender on the fly * works with a basic appender that is not the new OutputStream appender or * new Writer appender. */ @Test public void testUpdatePatternWithFileAppender() { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Configuration config = ctx.getConfiguration(); // @formatter:off final Appender appender = FileAppender.newBuilder() .withFileName("target/" + getClass().getName() + ".log") .withAppend(false) .withName("File") .withIgnoreExceptions(false) .withBufferedIo(false) .withBufferSize(4000) .setConfiguration(config) .build(); // @formatter:on appender.start(); config.addAppender(appender); ConfigurationTestUtils.updateLoggers(appender, config); LogManager.getLogger().error("FOO MSG"); } }
@Test public void testLazyCreate() throws Exception { final Layout<String> layout = createPatternLayout(); // @formatter:off final FileAppender appender = FileAppender.newBuilder() .withFileName(FILE_NAME) .withName("test") .withImmediateFlush(false) .withIgnoreExceptions(false) .withBufferedIo(false) .withBufferSize(1) .withLayout(layout) .withCreateOnDemand(createOnDemand) .build(); // @formatter:on Assert.assertEquals(createOnDemand, appender.getManager().isCreateOnDemand()); try { Assert.assertNotEquals(createOnDemand, Files.exists(PATH)); appender.start(); Assert.assertNotEquals(createOnDemand, Files.exists(PATH)); } finally { appender.stop(); } Assert.assertNotEquals(createOnDemand, Files.exists(PATH)); }
final Layout<String> layout = createPatternLayout(); final FileAppender appender = FileAppender.newBuilder() .withFileName(FILE_NAME) .withName("test")
.build(); final FileAppender appender = FileAppender.newBuilder() .withFileName(file.getAbsolutePath()) .withName("test")
.build(); final FileAppender appender = FileAppender.newBuilder() .withFileName(file.getAbsolutePath()) .withName("test")
final Layout<String> layout = createPatternLayout(); final FileAppender appender = FileAppender.newBuilder() .withFileName(FILE_NAME) .withName("test")
.build(); final FileAppender appender = FileAppender.newBuilder() .withFileName(LOG_FILE) .withAppend(false)
/** * Makes a FileAppender with the most common options * * @param filename * @param layout * @param name * @return */ public static FileAppender makeFileAppender(String filename, PatternLayout layout, String name) { return FileAppender.newBuilder() .withFileName(filename) .withBufferedIo(true) .withLayout(layout) .withName(name) .build(); }
private static void addFileAppender() throws IOException { String tempDir = System.getProperty("java.io.tmpdir"); File logFile = new File(tempDir, "meghanada_server.log"); Object ctx = LogManager.getContext(false); if (ctx instanceof LoggerContext) { try (LoggerContext context = (LoggerContext) ctx) { Configuration configuration = context.getConfiguration(); LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); FileAppender fileAppender = FileAppender.newBuilder() .withName("file") .withLayout( PatternLayout.newBuilder() .withPattern("[%d][%-5.-5p][%-14.-14c{1}:%4L] %-22.-22M - %m%n") .build()) .withFileName(logFile.getCanonicalPath()) .build(); configuration.addAppender(fileAppender); loggerConfig.addAppender(fileAppender, Level.ERROR, null); context.updateLoggers(); } } }
final String advertiseUri, final Configuration config) { return FileAppender.<B>newBuilder() .withAdvertise(Boolean.parseBoolean(advertise)) .withAdvertiseUri(advertiseUri)
public static void createFileAppender(String logDir, String logFile) { logDir = logDir + SystemProperties.fileSeparator + logFile; final LoggerContext context = (LoggerContext) LogManager.getContext(false); final Configuration config = context.getConfiguration(); /*Layout layout = PatternLayout.createLayout("%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", null, config, null, null,true, true, null, null );*/ Layout layout = PatternLayout.newBuilder() .withConfiguration(config) .withPattern("%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n") .build(); FileAppender.Builder builder = FileAppender.newBuilder(); builder.setConfiguration(config); builder.withFileName(logDir); builder.withAppend(false); builder.withLocking(false); builder.withName("File"); builder.withIgnoreExceptions(false); builder.withImmediateFlush(true); builder.withBufferedIo(false); builder.withBufferSize(0); builder.withLayout(layout); builder.withAdvertise(false); Appender appender = builder.build(); appender.start(); config.addAppender(appender); AppenderRef.createAppenderRef("File", null, null); config.getRootLogger().addAppender(appender, Level.DEBUG, null); context.updateLoggers(); }