kayvee 3.17.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +147 -202
  2. package/dist/index.d.ts +4 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +29 -0
  5. package/dist/kayvee.d.ts +12 -0
  6. package/dist/kayvee.d.ts.map +1 -0
  7. package/dist/kayvee.js +50 -0
  8. package/dist/logger/logger.d.ts +49 -0
  9. package/dist/logger/logger.d.ts.map +1 -0
  10. package/dist/logger/logger.js +237 -0
  11. package/dist/middleware.d.ts +23 -0
  12. package/dist/middleware.d.ts.map +1 -0
  13. package/dist/middleware.js +196 -0
  14. package/dist/package.json +89 -0
  15. package/dist/router/index.d.ts +23 -0
  16. package/dist/router/index.d.ts.map +1 -0
  17. package/dist/router/index.js +184 -0
  18. package/package.json +64 -24
  19. package/.circleci/config.yml +0 -25
  20. package/.eslintrc.yml +0 -44
  21. package/.nvmrc +0 -1
  22. package/.prettierrc.json +0 -1
  23. package/Makefile +0 -56
  24. package/benchmarks/data/.keep +0 -1
  25. package/benchmarks/data/corpus-basic.json +0 -22
  26. package/benchmarks/data/corpus-pathological.json +0 -22
  27. package/benchmarks/data/corpus-realistic.json +0 -22
  28. package/benchmarks/data/kvconfig-basic.yml +0 -7
  29. package/benchmarks/data/kvconfig-pathological.yml +0 -222
  30. package/benchmarks/data/kvconfig-realistic.yml +0 -39
  31. package/benchmarks/routing.js +0 -116
  32. package/build/lib/kayvee.js +0 -67
  33. package/build/lib/logger/helpers.js +0 -0
  34. package/build/lib/logger/logger.js +0 -221
  35. package/build/lib/middleware.js +0 -302
  36. package/build/lib/router/index.js +0 -198
  37. package/build/package.json +0 -49
  38. package/build/test/context_logger.js +0 -77
  39. package/build/test/kayvee.js +0 -36
  40. package/build/test/logger_test.js +0 -334
  41. package/build/test/middleware.js +0 -557
  42. package/build/test/router.js +0 -311
  43. package/index.js +0 -7
  44. package/lib/kayvee.ts +0 -73
  45. package/lib/logger/helpers.ts +0 -0
  46. package/lib/logger/logger.ts +0 -296
  47. package/lib/middleware.ts +0 -317
  48. package/lib/router/index.ts +0 -234
  49. package/lib/router/schema_definitions.json +0 -158
  50. package/test/context_logger.ts +0 -76
  51. package/test/kayvee.ts +0 -50
  52. package/test/kvconfig.yml +0 -14
  53. package/test/logger_test.ts +0 -378
  54. package/test/middleware.ts +0 -632
  55. package/test/router.ts +0 -558
  56. package/test/static/empty.css +0 -0
  57. package/test/static/js/empty.js +0 -0
  58. package/test/tests.json +0 -100
  59. package/tsconfig.json +0 -10
  60. package/tslint.json +0 -134
  61. /package/{build/lib → dist}/router/schema_definitions.json +0 -0
@@ -1,311 +0,0 @@
1
- var assert = require("assert");
2
- var router = require("../lib/router");
3
- describe("router.Router", function () {
4
- describe("constructor", function () {
5
- it("parses well formatted configs", function () {
6
- process.env.SCHOOL = "Hogwarts";
7
- var conf = "\nroutes:\n rule-one:\n matchers:\n title: [\"authorize-app\"]\n output:\n type: \"notifications\"\n channel: \"%{foo.bar}\"\n icon: \":rocket:\"\n message: \"authorized %{foo.bar} in ${SCHOOL}\"\n user: \"@fishman\"\n rule-two:\n matchers:\n foo.bar: [\"multiple\", \"matches\"]\n baz: [\"whatever\"]\n output:\n type: \"alerts\"\n series: \"other-series\"\n dimensions: [\"baz\"]\n stat_type: \"gauge\"\n rule-three:\n matchers:\n foo.bar: [\"multiple\", \"matches\"]\n baz: [\"whatever\"]\n output:\n type: \"alerts\"\n series: \"other-series\"\n dimensions: [\"baz\"]\n stat_type: \"gauge\"\n value_field: \"hello\"\n rule-four:\n matchers:\n foo.bar: [\"multiple\", \"matches\"]\n baz: [\"whatever\"]\n output:\n type: \"alerts\"\n series: \"other-series\"\n dimensions: []\n stat_type: \"gauge\"\n rule-five:\n matchers:\n foo.bar: [true]\n baz: [false]\n output:\n type: \"alerts\"\n series: \"other-series\"\n dimensions: []\n stat_type: \"gauge\"\n";
8
- var expected = [
9
- new router.Rule("rule-one", { title: ["authorize-app"] }, {
10
- type: "notifications",
11
- channel: "%{foo.bar}",
12
- icon: ":rocket:",
13
- message: "authorized %{foo.bar} in Hogwarts",
14
- user: "@fishman",
15
- }),
16
- new router.Rule("rule-two", { "foo.bar": ["multiple", "matches"], baz: ["whatever"] }, {
17
- type: "alerts",
18
- series: "other-series",
19
- dimensions: ["baz"],
20
- stat_type: "gauge",
21
- value_field: "value",
22
- }),
23
- new router.Rule("rule-three", { "foo.bar": ["multiple", "matches"], baz: ["whatever"] }, {
24
- type: "alerts",
25
- series: "other-series",
26
- dimensions: ["baz"],
27
- stat_type: "gauge",
28
- value_field: "hello",
29
- }),
30
- new router.Rule("rule-four", { "foo.bar": ["multiple", "matches"], baz: ["whatever"] }, {
31
- type: "alerts",
32
- series: "other-series",
33
- dimensions: [],
34
- stat_type: "gauge",
35
- value_field: "value",
36
- }),
37
- new router.Rule("rule-five", { "foo.bar": [true], baz: [false] }, {
38
- type: "alerts",
39
- series: "other-series",
40
- dimensions: [],
41
- stat_type: "gauge",
42
- value_field: "value",
43
- }),
44
- ];
45
- var actual = new router.Router();
46
- actual._loadConfigString(conf);
47
- assert.deepEqual(actual.rules, expected);
48
- });
49
- it("rejects specials in matchers", function () {
50
- var confTmpl = function (v) { return "\nroutes:\n non-string-values:\n matchers:\n no-numbers: [" + v + "]\n output:\n type: \"analytics\"\n series: \"fun\"\n"; };
51
- // Make sure the template works
52
- var conf = confTmpl('"valid"');
53
- var actual = new router.Router();
54
- assert.doesNotThrow(function () { return actual._loadConfigString(conf); });
55
- var _loop_1 = function (invalidVal) {
56
- var invalidConf = confTmpl(invalidVal);
57
- assert.throws(function () { return actual._loadConfigString(invalidConf); });
58
- };
59
- for (var _i = 0, _a = ["5", "[]", "{}"]; _i < _a.length; _i++) {
60
- var invalidVal = _a[_i];
61
- _loop_1(invalidVal);
62
- }
63
- assert.throws(function () { return actual._loadConfigString(confTmpl('""')); });
64
- return;
65
- });
66
- it("rejects duplicates in matchers", function () {
67
- var confTmpl = function (v) { return "\nroutes:\n sloppy:\n matchers:\n title: [" + v + "]\n output:\n type: \"analytics\"\n series: \"fun\"\n"; };
68
- var actual = new router.Router();
69
- var validConf = confTmpl('"non-repeated", "name"');
70
- assert.doesNotThrow(function () { return actual._loadConfigString(validConf); });
71
- var invalidConf = confTmpl('"repeated", "repeated", "name"');
72
- assert.throws(function () { return actual._loadConfigString(invalidConf); });
73
- });
74
- it("requires correct types in outputs", function () {
75
- var confTmpl = function (series, dimensions) { return "\nroutes:\n wrong:\n matchers:\n title: [\"test\"]\n output:\n type: \"alerts\"\n series: " + series + "\n dimensions: " + dimensions + "\n value_field: \"hihi\"\n stat_type: \"gauge\"\n"; };
76
- var actual = new router.Router();
77
- var validConf = confTmpl('"my-series"', '["dim1", "dim2"]');
78
- assert.doesNotThrow(function () { return actual._loadConfigString(validConf); });
79
- var invalidConf0 = confTmpl('["my-series"]', '["dim1", "dim2"]');
80
- assert.throws(function () { return actual._loadConfigString(invalidConf0); });
81
- var invalidConf1 = confTmpl('"my-series"', '"dim1"');
82
- assert.throws(function () { return actual._loadConfigString(invalidConf1); });
83
- });
84
- it("requires all keys in outputs", function () {
85
- var confTmpl = function (v) { return "\nroutes:\n wrong:\n matchers:\n title: [\"test\"]\n output:\n type: \"alerts\"" + v + "\n dimensions: [\"dim1\", \"dim2\"]\n stat_type: \"gauge\"\n"; };
86
- var actual = new router.Router();
87
- var validConf = confTmpl("\n series: \"whatever\"");
88
- assert.doesNotThrow(function () { return actual._loadConfigString(validConf); });
89
- var invalidConf = confTmpl("");
90
- assert.throws(function () { return actual._loadConfigString(invalidConf); });
91
- });
92
- it("doesn't allow extra keys", function () {
93
- var confTmpl = function (v) { return "\nroutes:\n wrong:\n matchers:\n title: [\"test\"]\n output:\n type: \"metrics\"" + v + "\n dimensions: [\"dim1\", \"dim2\"]\n"; };
94
- var actual = new router.Router();
95
- var validConf = confTmpl("\n series: \"whatever\"");
96
- assert.doesNotThrow(function () { return actual._loadConfigString(validConf); });
97
- var invalidConf = confTmpl("\n series: \"whatever\"\n something-else: \"hi there\"");
98
- assert.throws(function () { return actual._loadConfigString(invalidConf); });
99
- });
100
- it("errors on type-os", function () {
101
- var actual = new router.Router();
102
- var config;
103
- config = "\nroute: # Should be routes (plural)\n string-values:\n matchers:\n errors: [ \"type-o\" ]\n output:\n type: \"analytics\"\n series: \"fun\"\n";
104
- assert.throws(function () { return actual._loadConfigString(config); });
105
- config = "\nroutes:\n string-values:\n matcher: # Should be matchers (plural)\n errors: [ \"type-o\" ]\n output:\n type: \"analytics\"\n series: \"fun\"\n";
106
- assert.throws(function () { return actual._loadConfigString(config); });
107
- config = "\nroutes:\n string-values:\n matchers:\n errors: [ \"type-o\" ]\n outputs: # Should be output (signular)\n type: \"analytics\"\n series: \"fun\"\n";
108
- assert.throws(function () { return actual._loadConfigString(config); });
109
- config = "\nroutes:\n $invalid-string-values: # Invalid rule name\n matchers:\n errors: [ \"type-o\" ]\n output:\n type: \"analytics\"\n series: \"fun\"\n";
110
- assert.throws(function () { return actual._loadConfigString(config); });
111
- config = "\nroutes:\n string-values:\n matchers:\n errors: [ \"type-o\" ]\n output:\n type: \"analytic\" # Should be analytics (plural)p\n series: \"fun\"\n";
112
- assert.throws(function () { return actual._loadConfigString(config); });
113
- config = "\nroutes:\n string-values:\n matchers:\n errors: [ \"*\", \"type-o\" ] # A wildcard cannot exist with other matchers\n output:\n type: \"analytics\"\n series: \"fun\"\n";
114
- assert.throws(function () { return actual._loadConfigString(config); });
115
- });
116
- });
117
- describe("route", function () {
118
- it("matches one or more rule and returns appropriate outputs", function () {
119
- var r = new router.Router([
120
- new router.Rule("rule-one", { title: ["hello", "hi"], foo: ["bar", "baz"] }, { channel: "#-%{foo}-", dimensions: ["-%{foo}-"] }),
121
- new router.Rule("rule-two", { "bing.bong": ["buzz"] }, { series: "x" }),
122
- ]);
123
- var msg0 = {
124
- title: "hi",
125
- foo: "bar",
126
- };
127
- var expected0 = [
128
- {
129
- rule: "rule-one",
130
- channel: "#-bar-",
131
- dimensions: ["-bar-"],
132
- },
133
- ];
134
- var actual0 = r.route(msg0).routes;
135
- assert.deepEqual(expected0, actual0);
136
- var msg1 = {
137
- title: "hi",
138
- bing: {
139
- bong: "buzz",
140
- },
141
- };
142
- var expected1 = [
143
- {
144
- rule: "rule-two",
145
- series: "x",
146
- },
147
- ];
148
- var actual1 = r.route(msg1).routes;
149
- assert.deepEqual(expected1, actual1);
150
- var msg2 = {
151
- title: "hello",
152
- foo: "baz",
153
- bing: {
154
- bong: "buzz",
155
- },
156
- };
157
- var expected2 = [
158
- {
159
- rule: "rule-one",
160
- channel: "#-baz-",
161
- dimensions: ["-baz-"],
162
- },
163
- {
164
- rule: "rule-two",
165
- series: "x",
166
- },
167
- ];
168
- var actual2 = r.route(msg2).routes;
169
- assert.deepEqual(expected2, actual2);
170
- });
171
- });
172
- });
173
- describe("router.Rule", function () {
174
- describe("matches", function () {
175
- it("works on simple cases", function () {
176
- var r = new router.Rule("test-rule", { title: ["hello", "hi"], foo: ["bar"] }, {});
177
- assert(r.matches({
178
- title: "hello",
179
- foo: "bar",
180
- }));
181
- assert(r.matches({
182
- title: "hi",
183
- foo: "bar",
184
- }));
185
- assert(!r.matches({
186
- title: "hi",
187
- foo: "fighters",
188
- }));
189
- assert(!r.matches({
190
- title: "howdy",
191
- foo: "bar",
192
- }));
193
- assert(!r.matches({
194
- "missing-stuff": "indeed",
195
- }));
196
- });
197
- it("works on nested messages", function () {
198
- var r = new router.Rule("test-rule", { "foo.bar": ["hello", "hi"] }, {});
199
- assert(r.matches({
200
- title: "greeting",
201
- foo: {
202
- bar: "hello",
203
- },
204
- }));
205
- assert(r.matches({
206
- title: "greeting",
207
- foo: {
208
- bar: "hi",
209
- },
210
- }));
211
- assert(!r.matches({
212
- title: "greeting",
213
- foo: {
214
- bar: "howdy",
215
- },
216
- }));
217
- assert(!r.matches({
218
- title: "greeting",
219
- foo: {
220
- baz: "howdy",
221
- },
222
- }));
223
- assert(!r.matches({
224
- title: "greeting",
225
- boo: {
226
- bar: "howdy",
227
- },
228
- }));
229
- assert(!r.matches({
230
- title: "greeting",
231
- foo: "hi",
232
- }));
233
- });
234
- it("wild card matching", function () {
235
- var r = new router.Rule("test-rule", { any: ["*"] }, {});
236
- assert(r.matches({
237
- any: false,
238
- }));
239
- assert(r.matches({
240
- any: 5,
241
- }));
242
- assert(r.matches({
243
- any: "hello",
244
- }));
245
- assert(r.matches({
246
- any: {
247
- bar: "hi",
248
- },
249
- }));
250
- assert(!r.matches({
251
- any: "",
252
- }));
253
- assert(!r.matches({
254
- any: null,
255
- }));
256
- assert(!r.matches({
257
- any: undefined,
258
- }));
259
- assert(!r.matches({
260
- title: "greeting",
261
- foo: {
262
- bar: "howdy",
263
- },
264
- }));
265
- });
266
- it("bool matching", function () {
267
- var r = new router.Rule("test-rule", { bull: [true] }, {});
268
- assert(r.matches({
269
- bull: true,
270
- }));
271
- assert(r.matches({
272
- any: false,
273
- bull: true,
274
- }));
275
- assert(!r.matches({
276
- bull: false,
277
- }));
278
- assert(!r.matches({
279
- bull: "false",
280
- }));
281
- assert(!r.matches({
282
- title: "greeting",
283
- foo: {
284
- bar: "howdy",
285
- },
286
- }));
287
- });
288
- });
289
- describe("outputFor", function () {
290
- it("substitutes kv entries", function () {
291
- var r = new router.Rule("myrule", {}, {
292
- channel: "#-%{foo}-",
293
- dimensions: ["-%{foo}-", "-%{bar.baz}-"],
294
- });
295
- var msg = {
296
- title: "greeting",
297
- foo: "partner",
298
- bar: {
299
- baz: "nest egg",
300
- },
301
- };
302
- var expected = {
303
- rule: "myrule",
304
- channel: "#-partner-",
305
- dimensions: ["-partner-", "-nest egg-"],
306
- };
307
- var actual = r.outputFor(msg);
308
- assert.deepEqual(expected, actual);
309
- });
310
- });
311
- });
package/index.js DELETED
@@ -1,7 +0,0 @@
1
- module.exports = require("./build/lib/kayvee");
2
- // export logger capitalized to fix lint errors, lowercased to support legacy usage
3
- module.exports.logger = module.exports.Logger = require("./build/lib/logger/logger");
4
- module.exports.router = require("./build/lib/router");
5
- module.exports.middleware = require("./build/lib/middleware");
6
- module.exports.setGlobalRouting = module.exports.logger.setGlobalRouting;
7
- module.exports.mockRouting = module.exports.logger.mockRouting;
package/lib/kayvee.ts DELETED
@@ -1,73 +0,0 @@
1
- var _ = require("underscore");
2
-
3
- const deploy_env = process.env._DEPLOY_ENV;
4
- const workflow_id = process.env._EXECUTION_NAME;
5
- const pod_id = process.env._POD_ID;
6
- const pod_shortname = process.env._POD_SHORTNAME;
7
- const pod_region = process.env._POD_REGION;
8
- const pod_account = process.env._POD_ACCOUNT;
9
-
10
- // Encode errors to strings instead of toJSON()
11
- function replaceErrors(key, value) {
12
- if (value instanceof Error) {
13
- return value.toString();
14
- }
15
-
16
- return value;
17
- }
18
-
19
- // Converts a map to a string space-delimited key=val pairs
20
- function format(data) {
21
- if (deploy_env || workflow_id || pod_id || pod_shortname || pod_account || pod_region) {
22
- const extras: any = {};
23
- if (deploy_env) {
24
- extras.deploy_env = deploy_env;
25
- }
26
- if (workflow_id) {
27
- extras.wf_id = workflow_id;
28
- }
29
- if (pod_id) {
30
- extras["pod-id"] = pod_id;
31
- }
32
- if (pod_shortname) {
33
- extras["pod-shortname"] = pod_shortname;
34
- }
35
- if (pod_region) {
36
- extras["pod-region"] = pod_region;
37
- }
38
- if (pod_account) {
39
- extras["pod-account"] = pod_account;
40
- }
41
-
42
- return JSON.stringify(_.extend(extras, data), replaceErrors);
43
- }
44
- return JSON.stringify(data, replaceErrors);
45
- }
46
-
47
- // Similar to format, but takes additional reserved params to promote logging best-practices
48
- function formatLog(source = "", level = "", title = "", data = {}) {
49
- let info = data;
50
- if (!_.isObject(data)) {
51
- info = {};
52
- }
53
- const reserved = { source, level, title };
54
-
55
- // reserved keys overwrite other keys in data
56
- return format(_.extend({}, info, reserved));
57
- }
58
-
59
- module.exports = {
60
- format,
61
- formatLog,
62
- };
63
-
64
- const LOG_LEVELS = {
65
- UNKNOWN: "unknown",
66
- CRITICAL: "critical",
67
- ERROR: "error",
68
- WARNING: "warning",
69
- INFO: "info",
70
- TRACE: "trace",
71
- };
72
-
73
- _.extend(module.exports, LOG_LEVELS);
File without changes
@@ -1,296 +0,0 @@
1
- var _ = require("underscore");
2
- var kv = require("../kayvee");
3
- var router = require("../router");
4
-
5
- var LEVELS = {
6
- Trace: "trace",
7
- Debug: "debug",
8
- Info: "info",
9
- Warning: "warning",
10
- Error: "error",
11
- Critical: "critical",
12
- };
13
-
14
- var LOG_LEVEL_ENUM = {
15
- trace: 0,
16
- debug: 1,
17
- info: 2,
18
- warning: 3,
19
- error: 4,
20
- critical: 5,
21
- };
22
-
23
- const assign = Object.assign || _.assign; // Use the faster Object.assign if possible
24
-
25
- let globalRouter;
26
-
27
- function setGlobalRouting(filename) {
28
- globalRouter = new router.Router();
29
- globalRouter.loadConfig(filename);
30
- }
31
-
32
- function getGlobalRouter() {
33
- return globalRouter;
34
- }
35
-
36
- // This is a port from kayvee-go/logger/logger.go
37
- class Logger {
38
- formatter = null;
39
- logLvl = null;
40
- globals = null;
41
- logWriter = null;
42
- logRouter = null;
43
-
44
- constructor(
45
- source,
46
- logLvl = process.env.KAYVEE_LOG_LEVEL,
47
- formatter = kv.format,
48
- output = console.error,
49
- ) {
50
- this.formatter = formatter;
51
- this.logLvl = this._validateLogLvl(logLvl);
52
- this.globals = {};
53
- this.globals.source = source;
54
- this.logWriter = output;
55
-
56
- if (process.env._TEAM_OWNER) {
57
- this.globals.team = process.env._TEAM_OWNER;
58
- }
59
- if (process.env._DEPLOY_ENV) {
60
- this.globals.deploy_env = process.env._DEPLOY_ENV;
61
- }
62
- if (process.env._EXECUTION_NAME) {
63
- this.globals.wf_id = process.env._EXECUTION_NAME;
64
- }
65
- if (process.env._POD_ID) {
66
- this.globals["pod-id"] = process.env._POD_ID;
67
- }
68
- if (process.env._POD_SHORTNAME) {
69
- this.globals["pod-shortname"] = process.env._POD_SHORTNAME;
70
- }
71
- if (process.env._POD_REGION) {
72
- this.globals["pod-region"] = process.env._POD_REGION;
73
- }
74
- if (process.env._POD_ACCOUNT) {
75
- this.globals["pod-account"] = process.env._POD_ACCOUNT;
76
- }
77
- }
78
-
79
- setRouter(r) {
80
- this.logRouter = r;
81
- }
82
-
83
- setConfig(source, logLvl, formatter, output) {
84
- this.globals.source = source;
85
- this.logLvl = this._validateLogLvl(logLvl);
86
- this.formatter = formatter;
87
- this.logWriter = output;
88
- return this.logWriter;
89
- }
90
-
91
- _validateLogLvl(logLvl) {
92
- if (logLvl == null) {
93
- return LEVELS.Debug;
94
- }
95
- for (var key in LEVELS) {
96
- if (Object.prototype.hasOwnProperty.call(LEVELS, key)) {
97
- var value = LEVELS[key];
98
- if (logLvl.toLowerCase() === value) {
99
- return value;
100
- }
101
- }
102
- }
103
- return LEVELS.Debug;
104
- }
105
-
106
- setLogLevel(logLvl) {
107
- this.logLvl = this._validateLogLvl(logLvl);
108
- return this.logLvl;
109
- }
110
-
111
- setFormatter(formatter) {
112
- this.formatter = formatter;
113
- return this.formatter;
114
- }
115
-
116
- setOutput(output) {
117
- this.logWriter = output;
118
- return this.logWriter;
119
- }
120
-
121
- trace(title) {
122
- this.traceD(title, {});
123
- }
124
-
125
- debug(title) {
126
- this.debugD(title, {});
127
- }
128
-
129
- info(title) {
130
- this.infoD(title, {});
131
- }
132
-
133
- warn(title) {
134
- this.warnD(title, {});
135
- }
136
-
137
- error(title) {
138
- this.errorD(title, {});
139
- }
140
-
141
- critical(title) {
142
- this.criticalD(title, {});
143
- }
144
-
145
- counter(title) {
146
- this.counterD(title, 1, {});
147
- }
148
-
149
- gauge(title, value) {
150
- this.gaugeD(title, value, {});
151
- }
152
-
153
- traceD(title, data) {
154
- this._logWithLevel(
155
- LEVELS.Trace,
156
- {
157
- title,
158
- },
159
- data,
160
- );
161
- }
162
-
163
- debugD(title, data) {
164
- this._logWithLevel(
165
- LEVELS.Debug,
166
- {
167
- title,
168
- },
169
- data,
170
- );
171
- }
172
-
173
- infoD(title, data) {
174
- this._logWithLevel(
175
- LEVELS.Info,
176
- {
177
- title,
178
- },
179
- data,
180
- );
181
- }
182
-
183
- warnD(title, data) {
184
- this._logWithLevel(
185
- LEVELS.Warning,
186
- {
187
- title,
188
- },
189
- data,
190
- );
191
- }
192
-
193
- errorD(title, data) {
194
- this._logWithLevel(
195
- LEVELS.Error,
196
- {
197
- title,
198
- },
199
- data,
200
- );
201
- }
202
-
203
- criticalD(title, data) {
204
- this._logWithLevel(
205
- LEVELS.Critical,
206
- {
207
- title,
208
- },
209
- data,
210
- );
211
- }
212
-
213
- counterD(title, value, data) {
214
- this._logWithLevel(
215
- LEVELS.Info,
216
- {
217
- title,
218
- value,
219
- type: "counter",
220
- },
221
- data,
222
- );
223
- }
224
-
225
- gaugeD(title, value, data) {
226
- this._logWithLevel(
227
- LEVELS.Info,
228
- {
229
- title,
230
- value,
231
- type: "gauge",
232
- },
233
- data,
234
- );
235
- }
236
-
237
- _logWithLevel(logLvl, metadata, userdata) {
238
- if (LOG_LEVEL_ENUM[logLvl] < LOG_LEVEL_ENUM[this.logLvl]) {
239
- return;
240
- }
241
- const data = assign({ level: logLvl }, this.globals, metadata, userdata);
242
- if (this.logRouter) {
243
- data._kvmeta = this.logRouter.route(data);
244
- } else if (globalRouter) {
245
- data._kvmeta = globalRouter.route(data);
246
- }
247
- this.logWriter(this.formatter(data));
248
- }
249
- }
250
-
251
- module.exports = Logger;
252
- module.exports.setGlobalRouting = setGlobalRouting;
253
- module.exports.getGlobalRouter = getGlobalRouter;
254
- module.exports.mockRouting = (cb) => {
255
- const _logWithLevel: any = Logger.prototype._logWithLevel;
256
-
257
- if (_logWithLevel.isMocked) {
258
- throw Error("Nested kv.mockRouting calls are not supported");
259
- }
260
-
261
- const ruleMatches = {};
262
-
263
- Logger.prototype._logWithLevel = function (logLvl, metadata, userdata) {
264
- const formatter = this.formatter;
265
- const logWriter = this.logWriter;
266
-
267
- this.formatter = (msg) => msg;
268
- this.logWriter = (msg) => {
269
- if (!msg._kvmeta) {
270
- return;
271
- }
272
-
273
- msg._kvmeta.routes.forEach((route) => {
274
- ruleMatches[route.rule] = (ruleMatches[route.rule] || []).concat(route);
275
- });
276
- };
277
-
278
- _logWithLevel.call(this, logLvl, metadata, userdata);
279
-
280
- this.formatter = formatter;
281
- this.logWriter = logWriter;
282
- };
283
-
284
- const stfuTypeScript: any = Logger.prototype._logWithLevel;
285
- stfuTypeScript.isMocked = true;
286
-
287
- const done = () => {
288
- Logger.prototype._logWithLevel = _logWithLevel;
289
- return ruleMatches;
290
- };
291
-
292
- cb(done);
293
- };
294
- _.extend(module.exports, LEVELS);
295
- module.exports.LEVELS = ["trace", "debug", "info", "warn", "error", "critical"];
296
- module.exports.METRICS = ["counter", "gauge"];