.withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN) .withConfiguration(config) .build(); .withFileName(LOG_FILE) .withAppend(false) .withName("File") .withIgnoreExceptions(false) .withBufferSize(4000) .withBufferedIo(false) .withLayout(layout) .build();
/** * 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"); } }
.withFileName(file.getAbsolutePath()) .withName("test") .withImmediateFlush(false) .withIgnoreExceptions(false) .withBufferedIo(false) .withBufferSize(1) .withLayout(layout) .withCreateOnDemand(createOnDemand) .withFilePermissions(filePermissions) .build();
final Configuration config) { return FileAppender.<B>newBuilder() .withAdvertise(Boolean.parseBoolean(advertise)) .withAdvertiseUri(advertiseUri) .withAppend(Booleans.parseBoolean(append, true)) .withBufferedIo(Booleans.parseBoolean(bufferedIo, true)) .withBufferSize(Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE)) .setConfiguration(config) .withFileName(fileName) .withFilter(filter) .withIgnoreExceptions(Booleans.parseBoolean(ignoreExceptions, true)) .withImmediateFlush(Booleans.parseBoolean(immediateFlush, true)) .withLayout(layout) .withLocking(Boolean.parseBoolean(locking)) .withName(name) .build();
FileAppender.Builder appenderBuilder = new FileAppender.Builder(); appenderBuilder.withFileName(filePath); appenderBuilder.withAppend(append); appenderBuilder.withName(fileAppenderName); appenderBuilder.withLayout(layout); FileAppender appender = appenderBuilder.build(); appender.start(); context.getRootLogger().addAppender(appender);
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(); }
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(); } } }
/** * 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(); }
.withFileName(file.getAbsolutePath()) .withName("test") .withImmediateFlush(true) .withIgnoreExceptions(false) .withBufferedIo(false) .withBufferSize(1) .withLayout(layout) .withFilePermissions(filePermissions) .withFileOwner(user) .withFileGroup(group) .build();
.withFileName(FILE_NAME) .withName("test") .withImmediateFlush(false) .withIgnoreExceptions(false) .withBufferedIo(false) .withBufferSize(1) .withLayout(layout) .withCreateOnDemand(createOnDemand) .build();
.withFileName(FILE_NAME) .withName("test") .withImmediateFlush(false) .withIgnoreExceptions(false) .withLocking(locking) .withBufferedIo(false) .withLayout(layout) .withCreateOnDemand(createOnDemand) .build();
@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)); }