This repository has been archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.js
86 lines (74 loc) · 2.79 KB
/
config.js
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
/*
***** BEGIN LICENSE BLOCK *****
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* The Initial Developer of the Original Code is the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2012
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Rob Miller ([email protected])
* Victor Ng ([email protected])
*
***** END LICENSE BLOCK *****
*/
"use strict";
var clientModule = require('./client');
var resolver = require('./resolver');
var resolveName = resolver.resolveName;
var getattr = function(obj, attr, defaultValue) {
defaultValue = typeof(defaultValue) !== 'undefined' ? defaultValue : {};
var value = typeof(obj[attr]) !== 'undefined' ? obj[attr] : defaultValue;
return value;
};
var streamFromConfig = function(config) {
if (typeof(config.factory) == 'undefined')
{
throw new Error("factory attribute is missing from config");
}
var streamFactory = resolveName(config.factory);
if (streamFactory === undefined) {
throw new Error("Unable to resolve the streamFactory: ["+config.factory+"]")
}
var stream = streamFactory(config);
return stream;
};
var createClient = function(config, client) {
var streamConfig = getattr(config, 'stream');
var stream = streamFromConfig(streamConfig);
var logger = getattr(config, 'logger', '');
var severity = getattr(config, 'severity', 6);
var disabledTimers = getattr(config, 'disabledTimers', []);
var plugins = getattr(config, 'plugins');
var filterNames = getattr(config, 'filters', []);
var filters = [];
for (var i=0; i<filterNames.length; i++) {
var provider = resolveName(filterNames[i][0]);
var filterfn = provider(filterNames[i][1]);
filters.push(filterfn);
};
if (typeof(client) === 'undefined') {
client = new clientModule.HekaClient(stream, logger, severity,
disabledTimers, filters);
} else {
client.setup(stream, logger, severity, disabledTimers, filters);
};
for (var name in plugins) {
if (plugins.hasOwnProperty(name)) {
var pluginConfig = plugins[name];
var provider = resolveName(pluginConfig.provider);
var pluginMethod = provider(pluginConfig);
client.addMethod(name, pluginMethod, pluginConfig.override);
};
};
return client;
};
var clientFromJsonConfig = function(config, client) {
config = JSON.parse(config);
return createClient(config, client);
};
exports.resolveName = resolveName;
exports.clientFromJsonConfig = clientFromJsonConfig;
exports.createClient = createClient;