public void closeOutputStreamAppender ( String appenderName ) throws IOException { if ( null == appenderName ) { return ; } final LoggerContext ctx = ( LoggerContext ) LogManager . getContext ( false ) ; final Configuration config = ctx . getConfiguration ( ) ; AdminToolLog4j2OutputStream baos = outputStreams . get ( appenderName ) ; if ( null != config && null != config . getAppenders ( ) ) { OutputStreamAppender appender = config . getAppender ( appenderName ) ; if ( null != appender ) { appender . stop ( ) ; Collection < String > parentLoggerNames = getParentLoggerNames ( ) ; for ( String configuredLoggerName : getAllLoggerNames ( ) ) { LoggerConfig loggerConfig = config . getLoggerConfig ( configuredLoggerName ) ; loggerConfig . removeAppender ( appender . getName ( ) ) ; if ( null != baos . getOriginalLevel ( configuredLoggerName ) ) { changeLogger ( configuredLoggerName , baos . getOriginalLevel ( configuredLoggerName ) , parentLoggerNames . contains ( configuredLoggerName ) ) ; } } //unsure about, if removing the appender from logger config if it gets also removed from logger instance too... removeAppender ( appender , getParentLoggers ( ) ) ; removeAppender ( appender , getLoggers ( ) ) ; appender . getManager ( ) . getByteBuffer ( ) . clear ( ) ; ctx . updateLoggers ( ) ; } } if ( null != baos ) { try { baos . close ( ) ; baos . clearOriginalLevels ( ) ; } catch ( Exception ignore ) { } finally { outputStreams . remove ( appenderName ) ; } } }
closes output stream and removes appender from loggers