-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathMongoDbPatternLayoutAppender.java
More file actions
114 lines (105 loc) · 4.23 KB
/
MongoDbPatternLayoutAppender.java
File metadata and controls
114 lines (105 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (C) 2010 Robert Stewart (robert@wombatnation.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.log4mongo;
import com.mongodb.DBObject;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;
import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;
import org.bson.Document;
/**
* A Log4J Appender that uses a PatternLayout to write log events into a MongoDB database.
* <p>
* The conversion pattern specifies the format of a JSON document. The document can contain
* sub-documents and the elements can be strings or arrays.
* <p>
* For some Log4J appenders (especially file appenders) blank space padding is often used to get
* fields in adjacent rows to line up. For example, %-5p is often used to make all log levels the
* same width in characters. Since each value is stored in a separate property in the document, it
* usually doesn't make sense to use blank space padding with MongoDbPatternLayoutAppender.
* <p>
* The appender does <u>not</u> create any indexes on the data that's stored. If query performance
* is required, indexes must be created externally (e.g., in the mongo shell or an external
* reporting application).
*
* @author Robert Stewart (robert@wombatnation.com)
* @see <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html">Log4J
* Appender Interface</a>
* @see <a href="http://www.mongodb.org/">MongoDB</a>
*/
public class MongoDbPatternLayoutAppender extends MongoDbAppender {
@Override
public boolean requiresLayout() {
return (true);
}
/**
* Inserts a BSON representation of a LoggingEvent into a MongoDB collection. A PatternLayout is
* used to format a JSON document containing data available in the LoggingEvent and, optionally,
* additional data returned by custom PatternConverters.
* <p>
* The format of the JSON document is specified in the .layout.ConversionPattern property.
*
* @param loggingEvent
* The LoggingEvent that will be formatted and stored in MongoDB
*/
@Override
protected void append(final LoggingEvent loggingEvent) {
if (isInitialized()) {
DBObject bson = null;
String json = layout.format(loggingEvent);
if (json.length() > 0) {
Object obj = JSON.parse(json);
if (obj instanceof DBObject) {
bson = (DBObject) obj;
String dateKey = getDateKey(layout);
if(dateKey != null) {
//saving time stamp as a date instead of string
bson.put(dateKey, new Date(loggingEvent.getTimeStamp()));
}
}
}
if (bson != null) {
try {
getCollection().insertOne(new Document(bson.toMap()));
} catch (MongoException e) {
errorHandler.error("Failed to insert document to MongoDB", e,
ErrorCode.WRITE_FAILURE);
}
}
}
}
/**
* this method returns the key of date which is mentioned in layout pattern
* @param layout
* @return
*/
private String getDateKey(Layout layout){
String key = null;
String conversionPattern =((MongoDbPatternLayout)layout).getConversionPattern();
String[] splitPattern= conversionPattern.split(",");
for(String pattern : splitPattern) {
if(pattern.contains("%d")) {
key = pattern.split(":")[0];
}
}
if(key != null) {
key = key.replaceAll("\"|\"$", "");
key = key.replaceAll("\\{", "");
}
return key;
}
}