Sunday, August 16, 2020

Log4j log to MongoDB

 Log4j 2 writes log to MongoDB

1. org.springframework.data.document.mongodb.log4j.MongoLog4jAppender is deprecated and removed

2. In log4j2.xml configuration, should use MongoDb, instead of MongoDb2, MongoDB3, or may get "ERROR NoSql contains an invalid element or attribute "MongoDb2"" or "ERROR NoSql contains an invalid element or attribute "MongoDb3""

    <NoSql name="mongoAppender">

        <MongoDb databaseName="auspix" collectionName="log"

            server="localhost" port="27017" username="" password="" />

    </NoSql>    

    <Async name="mongoAppenderAsync">

        <AppenderRef ref="mongoAppender" />

    </Async>

3. In the pom.xml configuration, must provide the below dependency and the log4j-core version must be 2.9.1, same with log4j-nosql. or will get "ERROR appender NoSql has no parameter that matches element MongoDb"

    <dependency>

      <groupId>org.mongodb</groupId>

      <artifactId>mongo-java-driver</artifactId>

      </dependency>     

        <dependency>

            <groupId>org.apache.logging.log4j</groupId>

            <artifactId>log4j-core</artifactId>

            <version>2.9.1</version>

        </dependency>    

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-nosql</artifactId>

<version>2.9.1</version>

</dependency>


The project is on my githug: https://github.com/auspix/log4j2mongo

Reference:

https://logging.apache.org/log4j/log4j-2.9.1/log4j-nosql/index.html

https://logging.apache.org/log4j/log4j-2.9.1/manual/appenders.html#NoSQLAppender

https://www.codeleading.com/article/53808223/

https://yezhwi.github.io/2018/10/26/%E5%88%A9%E7%94%A8Log4j2%E5%BC%82%E6%AD%A5%E4%BF%9D%E5%AD%98%E6%97%A5%E5%BF%97%E5%88%B0MongoDB%E4%B8%AD/

No comments:

Post a Comment