我正在使用logback进行Kotlin项目,我希望能够使用API获取服务器的所有日志。 例如localhost:8088 / logs / recent会返回一个包含所有前小时日志的json。
我尝试使用附加程序将日志存储在MongoDB中,并使用rest API为它们提供服务,但是这会使服务器运行速度变慢,我什至无法打开该应用程序。文件追加程序也有同样的问题。
我的mongodb附加程序:
public class MongoDbAppender extends AppenderBase<LoggingEvent> {
private Mongo _mongo;
private String _dbHost = "localhost";
private int _dbPort = 27017;
private String _dbName = "labeler2";
private String _collectionP = "logs";
private DBCollection _collection;
@Override
public void start() {
try {
_mongo = new Mongo(_dbHost, _dbPort);
DB db = _mongo.getDB(_dbName);
_collection = db.getCollection(_collectionP);
} 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 setDbPort(int dbPort) {
_dbPort = dbPort;
}
public void setCollectionP(String collectionP) {
_collectionP = collectionP;
}
@Override
public void stop() {
_mongo.close();
super.stop();
}
@Override
protected void append(LoggingEvent e) {
BasicDBObjectBuilder objectBuilder = BasicDBObjectBuilder.start().
add("ts", new Date(e.getTimeStamp())).
add("msg", e.getFormattedMessage()).
add("level", e.getLevel().toString()).
add("logger", e.getLoggerName()).
add("thread", e.getThreadName());
if (e.hasCallerData()) {
StackTraceElement st = e.getCallerData()[0];
String callerData = String.format("%s.%s:%d", st.getClassName(), st.getMethodName(), st.getLineNumber());
objectBuilder.add("caller", callerData);
}
Map<String, String> mdc = e.getMdc();
if (mdc != null && !mdc.isEmpty()) {
objectBuilder.add("mdc", new BasicDBObject(mdc));
}
_collection.insert(objectBuilder.get());
}
}
登录声明:
<appender name="MONGODB" class="labeler2.util.MongoDbAppender">
<DbHost>localhost</DbHost>
<DbPort>27017</DbPort>
<DbName>labeler2</DbName>
<CollectionP>logs</CollectionP>
</appender>
<root level="DEBUG">
<appender-ref ref="MONGODB"/>
</root>
您会建议我做什么以使其正常工作? 谢谢