Java Code Examples for org.apache.logging.log4j.core.Appender#stop()
The following examples show how to use
org.apache.logging.log4j.core.Appender#stop() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Log4J2Appender.java From javamelody with Apache License 2.0 | 5 votes |
void deregister() { if (LogManager.getContext(false) instanceof LoggerContext) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); if (ctx.getConfiguration() instanceof AbstractConfiguration) { final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); final Appender appender = getSingleton(); appender.stop(); config.removeAppender(appender.getName()); final Logger rootLogger = LogManager.getRootLogger(); final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName()); loggerConfig.removeAppender(appender.getName()); ctx.updateLoggers(); } } }
Example 2
Source File: BaseService.java From support-diagnostics with Apache License 2.0 | 5 votes |
protected void createFileAppender(String logDir, String logFile) { logPath = logDir + SystemProperties.fileSeparator + logFile; Layout layout = PatternLayout.newBuilder() .withConfiguration(logConfig) .withPattern("%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n") .build(); FileAppender.Builder builder = FileAppender.newBuilder(); builder.setConfiguration(logConfig); builder.withFileName(logPath); builder.withAppend(false); builder.withLocking(false); builder.setName("packaged"); builder.setIgnoreExceptions(false); builder.withImmediateFlush(true); builder.withBufferedIo(false); builder.withBufferSize(0); builder.setLayout(layout); Appender diagAppender = builder.build(); Appender oldAppender = logConfig.getAppender("packaged"); if(oldAppender != null && oldAppender.isStarted()){ oldAppender.stop(); logConfig.getRootLogger().removeAppender("packaged"); } diagAppender.start(); logConfig.addAppender(diagAppender); AppenderRef.createAppenderRef("packaged", null, null); logConfig.getRootLogger().addAppender(diagAppender, null, null); loggerContext.updateLoggers(); logger.info(Constants.CONSOLE, "Diagnostic logger reconfigured for inclusion into archive"); }
Example 3
Source File: SocketAppenderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
void removeAndStopAppenders() { final Map<String, Appender> map = logger.getAppenders(); for (final Map.Entry<String, Appender> entry : map.entrySet()) { final Appender appender = entry.getValue(); logger.removeAppender(appender); appender.stop(); } }
Example 4
Source File: LogstashIT.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private static void testEvents(final List<LogEvent> logEvents) throws IOException { try (final RestHighLevelClient client = createClient()) { final Appender appender = createStartedAppender( JSON_TEMPLATE_GELF_LAYOUT, MavenHardcodedConstants.LS_GELF_INPUT_PORT); try { // Append events. LOGGER.info("appending events"); logEvents.forEach(appender::append); LOGGER.info("completed appending events"); // Wait all messages to arrive. Awaitility .await() .atMost(Duration.ofSeconds(60)) .pollDelay(Duration.ofSeconds(2)) .until(() -> queryDocumentCount(client) == LOG_EVENT_COUNT); // Verify indexed messages. final Set<String> expectedMessages = logEvents .stream() .map(LogstashIT::expectedLogstashMessageField) .collect(Collectors.toSet()); final Set<String> actualMessages = queryDocuments(client) .stream() .map(source -> (String) source.get(ES_INDEX_MESSAGE_FIELD_NAME)) .filter(Objects::nonNull) .collect(Collectors.toSet()); Assertions .assertThat(actualMessages) .isEqualTo(expectedMessages); } finally { appender.stop(); } } }
Example 5
Source File: LogstashIT.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private static <K> Map<K, Object> appendAndCollect( final List<LogEvent> logEvents, final Layout<?> layout, final int port, final Function<Map<String, Object>, K> keyMapper, final Set<String> excludedKeys) throws IOException { try (final RestHighLevelClient client = createClient()) { final Appender appender = createStartedAppender(layout, port); try { // Append the event. LOGGER.info("appending events"); logEvents.forEach(appender::append); LOGGER.info("completed appending events"); // Wait the message to arrive. Awaitility .await() .atMost(Duration.ofSeconds(60)) .pollDelay(Duration.ofSeconds(2)) .until(() -> queryDocumentCount(client) == LOG_EVENT_COUNT); // Retrieve the persisted messages. return queryDocuments(client) .stream() .collect(Collectors.toMap( keyMapper, (final Map<String, Object> source) -> { excludedKeys.forEach(source::remove); return source; })); } finally { appender.stop(); } } }
Example 6
Source File: FlumeAppenderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void removeAppenders(final Logger logger) { final Map<String, Appender> map = logger.getAppenders(); for (final Map.Entry<String, Appender> entry : map.entrySet()) { final Appender app = entry.getValue(); avroLogger.removeAppender(app); app.stop(); } }
Example 7
Source File: AbstractConfiguration.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Remove an Appender. First removes any associations between LoggerConfigs and the Appender, removes the Appender * from this appender list and then stops the appender. This method is synchronized in case an Appender with the * same name is being added during the removal. * * @param appenderName the name of the appender to remove. */ public synchronized void removeAppender(final String appenderName) { for (final LoggerConfig logger : loggerConfigs.values()) { logger.removeAppender(appenderName); } final Appender app = appenderName != null ? appenders.remove(appenderName) : null; if (app != null) { app.stop(); } }
Example 8
Source File: RoutingAppender.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override public boolean stop(final long timeout, final TimeUnit timeUnit) { setStopping(); super.stop(timeout, timeUnit, false); // Only stop appenders that were created by this RoutingAppender for (final Map.Entry<String, CreatedRouteAppenderControl> entry : createdAppenders.entrySet()) { final Appender appender = entry.getValue().getAppender(); appender.stop(timeout, timeUnit); } setStopped(); return true; }
Example 9
Source File: SyslogAppenderTestBase.java From logging-log4j2 with Apache License 2.0 | 5 votes |
protected void removeAppenders() { final Map<String,Appender> map = root.getAppenders(); for (final Map.Entry<String, Appender> entry : map.entrySet()) { final Appender app = entry.getValue(); root.removeAppender(app); app.stop(); } }
Example 10
Source File: LogstashIT.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Test public void test_newlines() throws IOException { // Create two log events containing new lines. final Level level = Level.DEBUG; final String loggerFqcn = "f.q.c.n"; final String loggerName = "A"; final SimpleMessage message1 = new SimpleMessage("line1\nline2\r\nline3"); final long instantMillis1 = Instant.EPOCH.toEpochMilli(); final LogEvent logEvent1 = Log4jLogEvent .newBuilder() .setLoggerName(loggerName) .setLoggerFqcn(loggerFqcn) .setLevel(level) .setMessage(message1) .setTimeMillis(instantMillis1) .build(); final SimpleMessage message2 = new SimpleMessage("line4\nline5\r\nline6"); final long instantMillis2 = instantMillis1 + Duration.ofDays(1).toMillis(); final LogEvent logEvent2 = Log4jLogEvent .newBuilder() .setLoggerName(loggerName) .setLoggerFqcn(loggerFqcn) .setLevel(level) .setMessage(message2) .setTimeMillis(instantMillis2) .build(); try (final RestHighLevelClient client = createClient()) { final Appender appender = createStartedAppender( JSON_TEMPLATE_GELF_LAYOUT, MavenHardcodedConstants.LS_GELF_INPUT_PORT); try { // Append the event. LOGGER.info("appending events"); appender.append(logEvent1); appender.append(logEvent2); LOGGER.info("completed appending events"); // Wait the message to arrive. Awaitility .await() .atMost(Duration.ofSeconds(60)) .pollDelay(Duration.ofSeconds(2)) .until(() -> queryDocumentCount(client) == 2); // Verify indexed messages. final Set<String> expectedMessages = Stream .of(logEvent1, logEvent2) .map(LogstashIT::expectedLogstashMessageField) .collect(Collectors.toSet()); final Set<String> actualMessages = queryDocuments(client) .stream() .map(source -> (String) source.get(ES_INDEX_MESSAGE_FIELD_NAME)) .filter(Objects::nonNull) .collect(Collectors.toSet()); Assertions .assertThat(actualMessages) .isEqualTo(expectedMessages); } finally { appender.stop(); } } }
Example 11
Source File: KafkaAppenderCloseTimeoutTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Test(timeout = 2000) public void testClose() throws Exception { final Appender appender = ctx.getRequiredAppender("KafkaAppender"); appender.stop(); }
Example 12
Source File: BaseService.java From support-diagnostics with Apache License 2.0 | 3 votes |
protected void closeLogs() { logger.info(Constants.CONSOLE, "Closing loggers."); Appender appndr = logConfig.getAppender("diag"); if(appndr != null && appndr.isStarted()){ appndr.stop(); } logConfig.getRootLogger().removeAppender("File"); }