${filePattern} ERROR ACCEPT DENY ${filePattern} ERROR cloudbhislog/error/${projectName}/errorlog.txt cloudbhislog/error/${projectName}/error-${projectName}-%d{yyyy-MM-dd}.%i.zip 100MB 30 1GB ${filePattern} cloudbhislog/business/${projectName}/businesslog.txt cloudbhislog/business/${projectName}/business-${projectName}-%d{yyyy-MM-dd}.%i.zip 100MB 30 1GB ${filePattern} 192.168.135.129:9527 ${filePattern} ${filePattern} bhis business 192.168.135.129 27017 ${filePattern} ERROR bhis error 192.168.135.129 27017 0 512
package com.gsww.bhis.admin.config;import org.bson.Document;import com.mongodb.MongoClient;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoDatabase;import ch.qos.logback.classic.spi.ILoggingEvent;import ch.qos.logback.core.Layout;import ch.qos.logback.core.UnsynchronizedAppenderBase;/** * @version 1.0.0 * @author fenglanglang */public class LogbackMongodbAppender extends UnsynchronizedAppenderBase{ Layout layout; //自定义配置 private String database; private String collection; private String host; private int port; private MongoCollection document; private MongoClient client; @Override public void start() { //这里可以做些初始化判断 比如layout不能为null , if (layout == null) { addWarn("Layout was not defined"); } //或者写入数据库 或者redis时 初始化连接等等 client = new MongoClient(host, port); MongoDatabase db = client.getDatabase(database); document = db.getCollection(collection); super.start(); } @Override public void stop() { //释放相关资源,如数据库连接,redis线程池等等 if (!isStarted()) { return; } client.close(); super.stop(); } @Override public void append(ILoggingEvent event) { if (event == null || !isStarted()) { return; } // 此处自定义实现输出 // 获取输出值:event.getFormattedMessage() //System.out.print(printString +":" + layout.doLayout(event)); Document doc = new Document(); doc.append("message", layout.doLayout(event)); document.insertOne(doc); } public Layout getLayout() { return layout; } public void setLayout(Layout layout) { this.layout = layout; } public String getDatabase() { return database; } public void setDatabase(String database) { this.database = database; } public String getCollection() { return collection; } public void setCollection(String collection) { this.collection = collection; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; }}
net.logstash.logback logstash-logback-encoder 4.6 org.springframework.data spring-data-mongodb org.mongodb mongo-java-driver