import org.apache.log4j.spi.LoggingEvent;
import org.log4mongo.LoggingEventBsonifierImpl;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
* 自定义log4mongo文档格式
*
* @ClassName: ConciseLoggingEventBsonifier
* @Description: TODO(这里用一句话描述这个类的作用)
* @author juluo
* @date 2017年9月25日 下午4:00:19
*
*/
public class ConciseLoggingEventBsonifier extends LoggingEventBsonifierImpl {
@Override
public DBObject bsonify(LoggingEvent loggingEvent) {
BasicDBObject result = null;
if (loggingEvent != null) {
result = new BasicDBObject();
result.put("timestamp", new Date(loggingEvent.getTimeStamp()));
this.nullSafePut(result, "level", loggingEvent.getLevel().toString());
this.nullSafePut(result, "thread", loggingEvent.getThreadName());
this.nullSafePut(result, "message", loggingEvent.getMessage());
this.nullSafePut(result, "loggerName", loggingEvent.getLoggerName());
this.addHostnameInformation(result);
// this.addMDCInformation(result, loggingEvent.getProperties());
// this.addLocationInformation(result,
// loggingEvent.getLocationInformation());
// this.addThrowableInformation(result,
// loggingEvent.getThrowableInformation());
}
return result;
}
}
============================================================================
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.spi.LoggingEvent;
import org.log4mongo.LoggingEventBsonifier;
import org.log4mongo.MongoDbAppender;
import com.mongodb.DBObject;
/**
* 重写MongoDbAppender编辑文档类型
*
* @ClassName: ConciseMongoDbAppender
* @Description: TODO(这里用一句话描述这个类的作用)
* @author juluo
* @date 2017年9月25日 下午4:00:59
*
*/
public class ConciseMongoDbAppender extends MongoDbAppender {
private LoggingEventBsonifier bsonifier = new ConciseLoggingEventBsonifier();
@Override
protected void append(LoggingEvent loggingEvent) {
if (StringUtils.isNotBlank(loggingEvent.getLoggerName())
&& loggingEvent.getLoggerName().contains("com.choice.core")) {
DBObject bson = this.bsonifier.bsonify(loggingEvent);
this.append(bson);
}
}
}
==========================================================i==========================
配置文件:
mongodb.properties:
mongo.hostport=127.0.0.1:27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#连接超时时间
mongo.connectTimeout=1000
#等待时间
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
#Socket超时时间
mongo.socketTimeout=1500
mongo.slaveOk=true
=====================================================================================
spring-mongodb.xml:
<?xml version="1.0" encoding="UTF-8"?>
-<beans xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 加载mongodb的属性配置文件 -->
<context:property-placeholder location="classpath:mongodb.properties"/>
<!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
-<mongo:mongo replica-set="${mongo.hostport}" id="mongo">
<!-- 一些连接属性的设置 -->
<mongo:options write-fsync="true" write-timeout="0" write-number="1" slave-ok="${mongo.slaveOk}" socket-timeout="${mongo.socketTimeout}" socket-keep-alive="${mongo.socketKeepAlive}" auto-connect-retry="${mongo.autoConnectRetry}" max-wait-time="${mongo.maxWaitTime}" connect-timeout="${mongo.connectTimeout}" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" connections-per-host="${mongo.connectionsPerHost}"/>
</mongo:mongo>
<mongo:db-factory mongo-ref="mongo" dbname="test"/>
-<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg value="logs" name="databaseName"/>
</bean>
</beans>
=====================================================================================
log4j.properties:
#设置输入日志到MongoDB
#log4j.logger.MongoDB=MongoDB
log4j.appender.MongoDB=com.choice.core.appender.ConciseMongoDbAppender
log4j.appender.MongoDB.Threshold=INFO
log4j.appender.MongoDB.databaseName=logs
log4j.appender.MongoDB.collectionName=log
log4j.appender.MongoDB.hostname=127.0.0.1
log4j.appender.MongoDB.port=27017
#传入jsonObject出错使用下面配置即可(此种配置将把所有类型value值以字符型插入)
log4j.appender.MongoDB.layout=org.apache.log4j.PatternLayout