logginglog4j2appenderrollingfileappendergraylog

How can I write log record to graylog server


I have graylog appender in log4j file. And I have a lot of other appender which defined before graylog integration in log4j file.

I want to write log record which defined before to graylog when I run the project. As a result will put to graylog other log records if there is AppenderRef

How my log4j configuration should be

<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="org.graylog2.log4j2" xmlns="http://logging.apache.org/log4j/2.x/config">
  <Properties>
    <Property name="filePath">${sys:thy.appdir}/paymentGateway/log
  </Property>
  </Properties>
  <Appenders>
    <GELF hostname="graylog.test.com" includeThreadContext="true" name="gelfAppender" port="12212" protocol="UDP" server="graylog.test.com">
      <KeyValuePair key="grayloggroup" value="Project_Name"/>
      <KeyValuePair key="application" value="App_Key"/>
      <PatternLayout pattern="%m%n"/>
    </GELF>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </Console>
    <RollingFile fileName="${filePath}/xFileAppender.out" filePattern="${filePath}/xFileAppender-%d{MM-dd-yyyy}-%i.log.gz" name="xFileAppender">
      <PatternLayout>
        <Pattern>%d - %m%n</Pattern>
      </PatternLayout>
      <DefaultRolloverStrategy max="2"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Logger level="info" name="grayLogger">
      <AppenderRef ref="gelfAppender"/>
    </Logger>
    <Logger level="debug" name="xLogger">
      <AppenderRef ref="xFileAppender,gelfAppender"/>
    </Logger>
    <Root level="debug">
      <AppenderRef ref="rootAppender"/>
    </Root>
  </Loggers>
</Configuration>

I assign reference gelfAppender in xFileAppender in order to write log to graylog in the same time

<Logger name="xLogger" level="debug">
  <AppenderRef ref="xFileAppender,gelfAppender" />
</Logger>

Solution

  • Below is the correct way to define multiple appender references in a Logger -

    <Logger name="xLogger" level="debug">
      <AppenderRef ref="xFileAppender" />
      <AppenderRef ref="gelfAppender" />
    </Logger>