private void addAppender ( ) { final LoggerContext context = LoggerContext . getContext ( false ) ; final Configuration defaultConfig = context . getConfiguration ( ) ; String gelfInputUrl = EnvUtil . isLan ( ) ? XianConfig . get ( "gelfInputLanUrl" ) : XianConfig . get ( "gelfInputInternetUrl" ) ; System . out . println ( "gelfInputUrl=" + gelfInputUrl ) ; int gelfInputPort = XianConfig . getIntValue ( "gelfInputPort" ) ; if ( StringUtil . isEmpty ( gelfInputUrl ) || gelfInputPort <= 0 ) { System . out . println ( "Gelf input url or port is not properly configured. No log will be sent to gelf logger server." ) ; return ; } final GelfLogAppender appender = /* WriterAppender.createAppender(layout, null, writer, writerName, false, true)*/ GelfLogAppender . createAppender ( defaultConfig , "gelf" , LevelRangeFilter . createFilter ( Level . OFF , Level . INFO , Filter . Result . ACCEPT , null ) , new GelfLogField [ ] { new GelfLogField ( "environment" , EnvUtil . getEnv ( ) , null , null ) , new GelfLogField ( "application" , EnvUtil . getApplication ( ) , null , null ) , new GelfLogField ( "pid" , JavaPIDUtil . getProcessName ( ) , null , null ) , new GelfLogField ( "nodeId" , LocalNodeManager . LOCAL_NODE_ID , null , null ) , new GelfLogField ( "thread" , null , null , PatternLayout . newBuilder ( ) . withAlwaysWriteExceptions ( false ) . withPattern ( "%t" ) . build ( ) ) , new GelfLogField ( "logger" , null , null , PatternLayout . newBuilder ( ) . withAlwaysWriteExceptions ( false ) . withPattern ( "%c" ) . build ( ) ) , new GelfLogField ( "msgId" , null , "msgId" , null ) , } , null , null , gelfInputUrl , null , gelfInputPort + "" , null , "false" , null , null , "gelf-log4j2" , "true" , //only works when extractStackTrace is true null , null , null , true , "%p %m%n" , MessageFormatEnum . json ) ; if ( appender == null ) throw new RuntimeException ( "gelf-log4j2 fails to initialize." ) ; appender . start ( ) ; defaultConfig . addAppender ( appender ) ; updateLoggers ( appender , defaultConfig ) ; }
add the gelf - log4j2 appender into log4j2 context .