-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ModuleConfig.cfc
170 lines (148 loc) · 6.85 KB
/
ModuleConfig.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
* www.ortussolutions.com
* ---
*/
component {
// Module Properties
this.title = "logstash";
this.author = "Ortus Solutions";
this.webURL = "https://github.com/coldbox-modules/logstash";
this.description = "A logstash module for the Coldbox Platform";
// Model Namespace
this.modelNamespace = "logstash";
this.entrypoint = "logstash";
// CF Mapping
this.cfmapping = "logstash";
// Dependencies
this.dependencies = [ "cbelasticsearch" ];
/**
* Configure Module
*/
function configure(){
var applicationName = server.coldfusion.productname == "Lucee" ? getApplicationSettings().name : getApplicationMetadata().name;
settings = {
// The name of this application, which will allow for grouping of logs
"applicationName" : getSystemSetting( "LOGSTASH_APPLICATION_NAME", server.coldfusion.productname eq "Lucee" ? getApplicationSettings().name : getApplicationMetadata().name ),
// Whether to enable the API endpoints to receive log messages
"enableAPI" : getSystemSetting( "LOGSTASH_ENABLE_API", true ),
// Whether to automatically enable the logstash logging appender
"enableAppenders" : getSystemSetting( "LOGSTASH_ENABLE_APPENDERS", true ),
// The type of transmission mode for this module - `direct` or `api`
"transmission" : getSystemSetting( "LOGSTASH_TRANSMISSION_METHOD", "direct" ),
// only used if the transmission setting is `api`
"apiUrl" : getSystemSetting( "LOGSTASH_API_URL", "" ),
// Regex used to whitelist remote addresses allowed to transmit to the API - by default only 127.0.0.1 is allowed to transmit messages to the API
"apiWhitelist" : getSystemSetting( "LOGSTASH_API_WHITELIST", "127.0.0.1" ),
// a user-provided API token - which must match the token configured on the remote API microservice leave empty if using IP whitelisting
"apiAuthToken" : getSystemSetting( "LOGSTASH_API_TOKEN", "" ),
// Min/Max levels for the appender
"levelMin" : getSystemSetting( "LOGSTASH_LEVEL_MIN", "FATAL" ),
"levelMax" : getSystemSetting( "LOGSTASH_LEVEL_MAX", "ERROR" ),
// A closure, which may be used in the configuration to provide custom information. Will be stored in the `userinfo` key in your logstash logs
"userInfoUDF" : function(){ return {}; },
// The name of the data stream to use for the appender
"dataStream" : getSystemSetting( "LOGSTASH_DATASTREAM", "logs-coldbox-logstash-appender" ),
// The data stream pattern to use for index templates
"dataStreamPattern" : getSystemSetting( "LOGSTASH_DATASTREAMPATTERN", "logs-coldbox-*" ),
// The name of the ILM policy for log rotation
"ILMPolicyName" : getSystemSetting( "LOGSTASH_ILMPOLICY", "cbelasticsearch-logs" ),
// The name of the component template to apply
"componentTemplateName" : getSystemSetting( "LOGSTASH_COMPONENT_TEMPLATE", "cbelasticsearch-logs-mappings" ),
// The name of the index template to apply
"indexTemplateName" : getSystemSetting( "LOGSTASH_INDEX_TEMPLATE", "cbelasticsearch-logs" ),
// Retention of logs in number of days
"retentionDays" : getSystemSetting( "LOGSTASH_RETENTION_DAYS", 365 ),
// The number of shards to use for new logstash indices
"indexShards" : getSystemSetting( "LOGSTASH_INDEX_SHARDS", 1 ),
// The number of replicas to use for new logstash indexes
"indexReplicas" : getSystemSetting( "LOGSTASH_INDEX_REPLICAS", 0 ),
// Backward compatiblility keys for migrating old rotational indices
"indexPrefix" : getSystemSetting( "LOGSTASH_INDEX_PREFIX", "" ),
"migrateIndices" : getSystemSetting( "LOGSTASH_MIGRATE_V2", false ),
// Whether to throw an error when a log document fails to save
"throwOnError" : true,
// An array of detached appenders which can be used with the `writeToAppender` interception point or directly through the elasticsearch module AppenderService
"detachedAppenders" : []
};
// Try to look up the release based on a box.json
if( !isNull( appmapping ) ) {
var boxJSONPath = expandPath( '/' & appmapping & '/box.json' );
if( fileExists( boxJSONPath ) ) {
var boxJSONRaw = fileRead( boxJSONPath );
if( isJSON( boxJSONRaw ) ) {
var boxJSON = deserializeJSON( boxJSONRaw );
if( boxJSON.keyExists( 'version' ) ) {
settings.release = boxJSON.version;
if( boxJSON.keyExists( 'slug' ) ) {
settings.release = boxJSON.slug & '@' & settings.release;
} else if( boxJSON.keyExists( 'name' ) ) {
settings.release = boxJSON.name & '@' & settings.release;
}
}
}
}
}
interceptors = [
//API Security Interceptor
{ class="logstash.interceptors.APISecurity" },
{ class="logstash.interceptors.LogEvents" }
];
interceptorSettings = {
customInterceptionPoints : [
"writeToAppender"
]
};
}
/**
* Fired when the module is registered and activated.
*/
function onLoad(){
loadAppenders();
}
/**
* Fired when the module is unregistered and unloaded
*/
function onUnload(){}
function afterConfigurationLoad(){}
/**
* Load LogBox Appenders
*/
private function loadAppenders(){
var appenderProperties = duplicate( settings );
if( len( appenderProperties.indexPrefix ) ){
appenderProperties.index = settings.indexPrefix;
}
if( settings.enableAppenders ){
logBox.registerAppender(
name = 'logstash_appender',
class = settings.transmission == "direct" ? "cbelasticsearch.models.logging.LogstashAppender" : "logstash.models.logging.APIAppender",
properties = appenderProperties,
levelMin = logBox.logLevels[ settings.levelMin ],
levelMax = logBox.logLevels[ settings.levelMax ]
);
var appenders = logBox.getAppendersMap( 'logstash_appender' );
// Register the appender with the root loggger, and turn the logger on.
var root = logBox.getRootLogger();
root.addAppender( appenders[ 'logstash_appender' ] );
}
settings.detachedAppenders.each( ( appender) => {
wirebox.getInstance( "AppenderService@cbelasticsearch" )
.createDetachedAppender(
appender.name,
appender.properties ?: {},
appender.class ?: "cbelasticsearch.models.logging.LogstashAppender"
);
} );
// If the api
if( settings.enableAPI ){
binder.map( "EventAppender@logstash" )
.to( '#this.cfmapping#.models.logging.APIEventAppender' )
.initWith(
name="logstash_api_event_appender",
properties=appenderProperties
)
.asSingleton();
}
}
}