关闭

MongoDB整合Spring Boot 的logback

标签: mongodblogbacklog
4847人阅读 评论(0) 收藏 举报
分类:

1、pom.xml

       <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.4.2</version>
        </dependency>
       <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.11</version>
        </dependency>
        <dependency>  
            <groupId>ch.qos.logback</groupId>  
            <artifactId>logback-classic</artifactId>  
            <version>1.1.11</version>  
        </dependency> 

2、logback.xml

    <appender name="MONGODB" class="com.lovnx.common.log.appender.MongoDBAppender">
        <DbHost>168.1.1.200</DbHost>
        <DbPort>27017</DbPort>
        <DbName>system_log</DbName>
        <DbCollectionName>log</DbCollectionName>
    </appender>

    <logger name="MONGODB" level="INFO">
        <appender-ref ref="MONGODB"/>
    </logger>  

3、appender

package com.lovnx.common.log.appender;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.status.ErrorStatus;

public class MongoDBAppender extends AppenderBase<LoggingEvent> {

    private MongoClient _mongo;
    private MongoCollection<Document> _collection;
    private String _dbHost = "168.1.1.200";
    private int _dbPort = 27017;
    private String _dbName = "logging";
    private String _dbCollectionName = "logging";


    @Override
    public void start() {
        try {
            _mongo = new MongoClient(_dbHost, _dbPort);
            MongoDatabase db = _mongo.getDatabase(_dbName);
            _collection = db.getCollection(_dbCollectionName);
        } catch (Exception e) {
            addStatus(new ErrorStatus("Failed to initialize MondoDB", this, e));
            return;
        }
        super.start();
    }

    public void setDbHost(String dbHost) {
        _dbHost = dbHost;
    }

    public void setDbName(String dbName) {
        _dbName = dbName;
    }

    public void setDbCollectionName(String dbCollectionName) {
        _dbCollectionName = dbCollectionName;
    }

    public void setDbPort(int dbPort) {
        _dbPort = dbPort;
    }


    @Override
    public void stop() {
        _mongo.close();
        super.stop();
    }

    @Override
    protected void append(LoggingEvent event) {
        try{
            Document document = getDocument(event.getFormattedMessage());
            if(document != null){
                _collection.insertOne(document);
            }
        }catch (Exception e) {
            addStatus(new ErrorStatus("日志写入到MongDB出错", this, e));
        }
    }
//这里指定只有json格式的日志才能输入到mongo
    private Document getDocument(String json) {
        try{
            return Document.parse(json);
        }catch (Exception e) {
            return null;
        }
    }
}

4、调用

//两种打日志方式,会更灵活

//这样是传统方式
  private static final Logger LOGGER = LoggerFactory.getLogger(Result.class);
//这样是输入到mongo的方式
  private static final Logger mongoLog = LoggerFactory.getLogger("MONGODB");
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:524521次
    • 积分:6062
    • 等级:
    • 排名:第4589名
    • 原创:102篇
    • 转载:37篇
    • 译文:10篇
    • 评论:182条
    博客专栏
    个人联系方式
    更多交流
    最新评论