-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoAppender.cfc
154 lines (131 loc) · 3.66 KB
/
MongoAppender.cfc
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
component output="false" extends="logbox.system.logging.AbstractAppender"
{
property name="mongoClient" getter="true" setter="false" ;
property name="mongoCollection" getter="true" setter="false";
public MongoAppender function init(
required string name,
struct properties,
string layout,
numeric levelMin,
numeric levelMax
){
if( structKeyExists( arguments, 'properties' ) ){
if( structKeyExists( arguments.properties, 'target' ) ){
var defConfiguration = getDefaultConfiguration().target;
for( var k in defConfiguration ){
if( ! structKeyExists(arguments.properties.target, local.k) ){
structInsert(arguments.properties.target, local.k , local.defConfiguration[ local.k ] );
}
}
structAppend( arguments.properties , getDefaultConfiguration(), false );
}else{
arguments.properties.target = getDefaultConfiguration();
}
}else{
arguments.properties = getDefaultConfiguration();
}
super.init( argumentCollection = arguments );
return this;
}
public void function logMessage(
required logbox.system.logging.LogEvent logEvent
){
/*
timestamp
category
message
severity
extraInfo
*/
var doc = createObject("java", "org.bson.Document").init();
doc.put('timestamp', arguments.logEvent.getTimeStamp() );
doc.put('category', arguments.logEvent.getCategory() );
doc.put('message', arguments.logEvent.getMessage() );
doc.put('severity', javacast("int",arguments.logEvent.getSeverity()) );
doc.put('extraInfo', arguments.logEvent.getExtraInfo() );
try{
getMongoConnection()
.getDatabase( getProperty('target').database)
.getCollection( getProperty('target').collection )
.insertOne( local.doc );
}catch(any e){}
}
private struct function getDefaultConfiguration(){
return {
"target": {
"host": "127.0.0.1",
"port": javacast('int',27017),
"async": false,
"database": 'logboxmongo',
"collection": 'logs',
"scope": 'application',
"scopeKey": 'logboxmongo_connection'
}
};
}
public function getTargetConfiguration(){
return getProperty( "target" );
}
private string function getScopeName()
hint="Gets the name of scope to use, probably application or server"
{
return getProperty( "target" ).scope;
}
private struct function getScopeObject()
hint="Gets the scope object"
{
return evaluate( getScopeName() );
}
private string function getScopeKey()
hint="Gets the key in which mongodb connection is stored"
{
return getProperty( "target" ).scopeKey;
}
private any function getMongoConnection()
hint="Gets the java mongodb connection object"
{
return getScopeObject()[ getScopeKey() ];
}
private boolean function hasMongoConnection()
hint="Tells if the mongodb connection has already been created"
{
return structKeyExists( getScopeObject(), getScopeKey() );
}
private boolean function createMongoConnection()
hint="Internal method to create and store mongodb connection"
{
if( hasMongoConnection() ){
return true;
}
try{
getScopeObject()[ getScopeKey() ] = createObject( "java", "com.mongodb.MongoClient" ).init(
getProperty('target').host,
getProperty('target').port
);
}catch( any e ){
return false;
}
return true;
}
private boolean function closeMongoConnection()
hint="internal method called from logbox onUnregistration"
{
var success = true;
try{
getMongoConnection().close()
}catch( any e ){
local.success=false;
}
return local.success;
}
public void function onRegistration()
hint="Called from logbox"
{
createMongoConnection();
}
public void function onUnRegistration()
hint="Called from logbox"
{
closeMongoConnection();
}
}