@weave-js/core 0.11.0 → 0.12.1
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.
- package/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +7 -7
- package/lib/broker/context.js +49 -49
- package/lib/broker/defaultOptions.js +9 -9
- package/lib/broker/index.js +161 -165
- package/lib/buildRuntime.js +34 -34
- package/lib/cache/adapters/base.js +119 -0
- package/lib/cache/adapters/inMemory.js +154 -0
- package/lib/cache/adapters/inMemoryLru.js +140 -0
- package/lib/cache/adapters/index.js +10 -0
- package/lib/cache/getCacheKeyByObject.js +31 -0
- package/lib/cache/getPropertyFromDataOrMetadata.js +20 -0
- package/lib/cache/lock.js +30 -24
- package/lib/constants.js +5 -5
- package/lib/errorHandler.js +8 -8
- package/lib/errors.js +35 -35
- package/lib/helper/defineAction.js +2 -2
- package/lib/helper/defineBrokerOptions.js +2 -2
- package/lib/helper/defineService.js +2 -2
- package/lib/index.js +21 -22
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -15
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +24 -24
- package/lib/logger/levels.js +21 -21
- package/lib/logger/tools.js +15 -15
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +75 -65
- package/lib/metrics/common.js +47 -47
- package/lib/metrics/constants.js +44 -44
- package/lib/metrics/exporter/base.js +8 -8
- package/lib/metrics/exporter/event.js +20 -20
- package/lib/metrics/exporter/index.js +15 -15
- package/lib/metrics/index.js +2 -2
- package/lib/metrics/types/base.js +20 -20
- package/lib/metrics/types/counter.js +6 -6
- package/lib/metrics/types/gauge.js +24 -24
- package/lib/metrics/types/histogram.js +26 -26
- package/lib/metrics/types/index.js +6 -6
- package/lib/metrics/types/info.js +17 -17
- package/lib/middlewares/action-hooks/index.js +38 -38
- package/lib/middlewares/bulkhead/index.js +34 -34
- package/lib/middlewares/cache/index.js +63 -37
- package/lib/middlewares/circuit-breaker/index.js +62 -62
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +47 -15
- package/lib/middlewares/index.js +2 -2
- package/lib/middlewares/metrics/getMiddlewareWrapper.js +17 -17
- package/lib/middlewares/metrics/index.js +29 -29
- package/lib/middlewares/retry/index.js +15 -15
- package/lib/middlewares/timeout/index.js +16 -16
- package/lib/middlewares/tracing/index.js +37 -37
- package/lib/middlewares/tracing/tags.js +6 -6
- package/lib/middlewares/validator/index.js +31 -31
- package/lib/registry/actionEndpoint.js +8 -8
- package/lib/registry/collections/actionCollection.js +39 -35
- package/lib/registry/collections/endpointCollection.js +63 -63
- package/lib/registry/collections/eventCollection.js +62 -62
- package/lib/registry/collections/nodeCollection.js +45 -45
- package/lib/registry/collections/serviceCollection.js +61 -55
- package/lib/registry/eventEndpoint.js +14 -14
- package/lib/registry/load-balancer/base.js +3 -3
- package/lib/registry/load-balancer/index.js +7 -7
- package/lib/registry/node.js +27 -27
- package/lib/registry/registry.js +145 -148
- package/lib/registry/service/parseAction.js +16 -16
- package/lib/registry/service/parseEvent.js +18 -18
- package/lib/registry/service/reduceMixins.js +10 -10
- package/lib/registry/service/service.js +72 -72
- package/lib/registry/serviceItem.js +21 -21
- package/lib/runtime/initActionInvoker.js +21 -21
- package/lib/runtime/initCache.js +14 -5
- package/lib/runtime/initContextFactory.js +22 -22
- package/lib/runtime/initEventbus.js +49 -49
- package/lib/runtime/initLogger.js +11 -11
- package/lib/runtime/initMetrics.js +54 -54
- package/lib/runtime/initMiddlewareManager.js +21 -21
- package/lib/runtime/initRegistry.js +6 -6
- package/lib/runtime/initServiceManager.js +85 -68
- package/lib/runtime/initTracing.js +23 -23
- package/lib/runtime/initTransport.js +5 -5
- package/lib/runtime/initUuidFactory.js +4 -4
- package/lib/runtime/initValidator.js +5 -5
- package/lib/tracing/collectors/base.js +22 -22
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +15 -15
- package/lib/tracing/span.js +26 -26
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +26 -26
- package/lib/transport/adapters/adapteBase.js +30 -30
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +18 -18
- package/lib/transport/adapters/fromURI.js +13 -13
- package/lib/transport/adapters/getAdapterByName.js +5 -5
- package/lib/transport/adapters/index.js +12 -12
- package/lib/transport/adapters/tcp/discovery/codec.js +4 -4
- package/lib/transport/adapters/tcp/discovery/index.js +62 -62
- package/lib/transport/adapters/tcp/index.js +169 -171
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -39
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +57 -57
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +4 -4
- package/lib/transport/createTransport.js +254 -254
- package/lib/transport/errorPayloadFactory.js +14 -17
- package/lib/transport/messageHandlers.js +200 -200
- package/lib/transport/messageTypes.js +19 -19
- package/lib/types.js +49 -25
- package/lib/utils/index.js +2 -2
- package/lib/utils/options.js +44 -44
- package/lib/utils/restoreError.js +4 -4
- package/package.json +4 -4
- package/lib/cache/base.js +0 -147
- package/lib/cache/inMemory.js +0 -140
- package/lib/cache/index.js +0 -48
package/lib/errors.js
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { ExtendableError } = require('./ExtendableError')
|
|
7
|
+
const { ExtendableError } = require('./ExtendableError');
|
|
8
8
|
|
|
9
9
|
class WeaveError extends ExtendableError {
|
|
10
10
|
constructor (message, code, type, data) {
|
|
11
|
-
super(message)
|
|
12
|
-
this.name = this.constructor.name
|
|
13
|
-
this.code = code || 500
|
|
14
|
-
this.type = type
|
|
15
|
-
this.data = data
|
|
16
|
-
this.retryable = false
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = this.constructor.name;
|
|
13
|
+
this.code = code || 500;
|
|
14
|
+
this.type = type;
|
|
15
|
+
this.data = data;
|
|
16
|
+
this.retryable = false;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
class WeaveRetryableError extends WeaveError {
|
|
21
21
|
constructor (message, code, type, data) {
|
|
22
|
-
super(message)
|
|
23
|
-
this.code = code || 500
|
|
24
|
-
this.type = type
|
|
25
|
-
this.data = data
|
|
26
|
-
this.retryable = true
|
|
22
|
+
super(message);
|
|
23
|
+
this.code = code || 500;
|
|
24
|
+
this.type = type;
|
|
25
|
+
this.data = data;
|
|
26
|
+
this.retryable = true;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
class WeaveServiceNotFoundError extends WeaveRetryableError {
|
|
31
31
|
constructor (data = {}) {
|
|
32
|
-
let message
|
|
32
|
+
let message;
|
|
33
33
|
|
|
34
34
|
if (data.actionName && data.nodeId) {
|
|
35
|
-
message = `Service "${data.actionName}" not found on node "${data.nodeId}"
|
|
35
|
+
message = `Service "${data.actionName}" not found on node "${data.nodeId}".`;
|
|
36
36
|
} else if (data.actionName) {
|
|
37
|
-
message = `Service "${data.actionName}" not found
|
|
37
|
+
message = `Service "${data.actionName}" not found.`;
|
|
38
38
|
} else {
|
|
39
|
-
message = 'Service not found.'
|
|
39
|
+
message = 'Service not found.';
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
super(message, 404, 'WEAVE_SERVICE_NOT_FOUND_ERROR', data)
|
|
42
|
+
super(message, 404, 'WEAVE_SERVICE_NOT_FOUND_ERROR', data);
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
class WeaveServiceNotAvailableError extends WeaveRetryableError {
|
|
47
47
|
constructor (data = {}) {
|
|
48
|
-
let message
|
|
48
|
+
let message;
|
|
49
49
|
if (data.nodeId) {
|
|
50
|
-
message = `Service "${data.actionName}" not available on node "${data.nodeId}"
|
|
50
|
+
message = `Service "${data.actionName}" not available on node "${data.nodeId}".`;
|
|
51
51
|
} else if (data.actionName) {
|
|
52
|
-
message = `Service "${data.actionName}" not available
|
|
52
|
+
message = `Service "${data.actionName}" not available.`;
|
|
53
53
|
} else {
|
|
54
|
-
message = 'Service not available.'
|
|
54
|
+
message = 'Service not available.';
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
super(message, 503, 'WEAVE_SERVICE_NOT_AVAILABLE_ERROR', data)
|
|
57
|
+
super(message, 503, 'WEAVE_SERVICE_NOT_AVAILABLE_ERROR', data);
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
class WeaveRequestTimeoutError extends WeaveRetryableError {
|
|
62
62
|
constructor (actionName, nodeId, timeout) {
|
|
63
|
-
const message = `Action ${actionName} timed out node ${nodeId || '<local>'}
|
|
63
|
+
const message = `Action ${actionName} timed out node ${nodeId || '<local>'}.`;
|
|
64
64
|
super(message, 504, 'WEAVE_REQUEST_TIMEOUT_ERROR', {
|
|
65
65
|
actionName,
|
|
66
66
|
nodeId,
|
|
67
67
|
timeout
|
|
68
|
-
})
|
|
69
|
-
this.retryable = true
|
|
68
|
+
});
|
|
69
|
+
this.retryable = true;
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
class WeaveParameterValidationError extends WeaveError {
|
|
74
74
|
constructor (message, data) {
|
|
75
|
-
super(message, 422, 'WEAVE_PARAMETER_VALIDATION_ERROR', data)
|
|
75
|
+
super(message, 422, 'WEAVE_PARAMETER_VALIDATION_ERROR', data);
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
class WeaveBrokerOptionsError extends WeaveError {
|
|
80
80
|
constructor (message, data) {
|
|
81
|
-
super(message, 500, 'WEAVE_BROKER_OPTIONS_ERROR', data)
|
|
81
|
+
super(message, 500, 'WEAVE_BROKER_OPTIONS_ERROR', data);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
class WeaveQueueSizeExceededError extends WeaveError {
|
|
86
86
|
constructor (data) {
|
|
87
|
-
super('Queue size limit was exceeded. Request rejected.', 429, 'WEAVE_QUEUE_SIZE_EXCEEDED_ERROR', data)
|
|
87
|
+
super('Queue size limit was exceeded. Request rejected.', 429, 'WEAVE_QUEUE_SIZE_EXCEEDED_ERROR', data);
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
class WeaveMaxCallLevelError extends WeaveError {
|
|
92
92
|
constructor (data) {
|
|
93
|
-
super(`Request level has reached the limit (${data.maxCallLevel}) on node "${data.nodeId}".`, 500, 'WEAVE_MAX_CALL_LEVEL_ERROR', data)
|
|
93
|
+
super(`Request level has reached the limit (${data.maxCallLevel}) on node "${data.nodeId}".`, 500, 'WEAVE_MAX_CALL_LEVEL_ERROR', data);
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|
|
@@ -99,22 +99,22 @@ class WeaveGracefulStopTimeoutError extends WeaveError {
|
|
|
99
99
|
super(`Unable to stop service "${service.name}"`, 500, 'GRACEFUL_STOP_TIMEOUT', {
|
|
100
100
|
name: service.name,
|
|
101
101
|
version: service.version
|
|
102
|
-
})
|
|
102
|
+
});
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
const restoreError = error => {
|
|
107
|
-
const ErrorClass = module.exports[error.name]
|
|
107
|
+
const ErrorClass = module.exports[error.name];
|
|
108
108
|
|
|
109
109
|
if (ErrorClass) {
|
|
110
110
|
switch (error.name) {
|
|
111
111
|
case 'WeaveError':
|
|
112
|
-
return new ErrorClass(error.message, error.code, error.type, error.data)
|
|
112
|
+
return new ErrorClass(error.message, error.code, error.type, error.data);
|
|
113
113
|
// case 'WeaveParameterValidationError':
|
|
114
114
|
// return new ErrorClass(error.message, error.data)
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
-
}
|
|
117
|
+
};
|
|
118
118
|
|
|
119
119
|
module.exports = {
|
|
120
120
|
WeaveBrokerOptionsError,
|
|
@@ -128,4 +128,4 @@ module.exports = {
|
|
|
128
128
|
WeaveServiceNotFoundError,
|
|
129
129
|
WeaveGracefulStopTimeoutError,
|
|
130
130
|
restoreError
|
|
131
|
-
}
|
|
131
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -4,15 +4,15 @@
|
|
|
4
4
|
* @typedef {import('./types.js').Broker} Broker
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { getDefaultOptions } = require('./broker/defaultOptions')
|
|
8
|
-
const { defaultsDeep } = require('@weave-js/utils')
|
|
9
|
-
const { initRuntime } = require('./buildRuntime')
|
|
10
|
-
const { createBrokerInstance } = require('./broker')
|
|
7
|
+
const { getDefaultOptions } = require('./broker/defaultOptions');
|
|
8
|
+
const { defaultsDeep } = require('@weave-js/utils');
|
|
9
|
+
const { initRuntime } = require('./buildRuntime');
|
|
10
|
+
const { createBrokerInstance } = require('./broker');
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @type {BrokerOptions} options Broker options
|
|
14
14
|
*/
|
|
15
|
-
exports.defaultOptions = getDefaultOptions()
|
|
15
|
+
exports.defaultOptions = getDefaultOptions();
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Build runtime object
|
|
@@ -21,43 +21,42 @@ exports.defaultOptions = getDefaultOptions()
|
|
|
21
21
|
*/
|
|
22
22
|
exports.createBroker = (options) => {
|
|
23
23
|
// get default options
|
|
24
|
-
const defaultOptions = getDefaultOptions()
|
|
24
|
+
const defaultOptions = getDefaultOptions();
|
|
25
25
|
|
|
26
26
|
// merge options with default options
|
|
27
|
-
options = defaultsDeep(options, defaultOptions)
|
|
27
|
+
options = defaultsDeep(options, defaultOptions);
|
|
28
28
|
|
|
29
29
|
// Init runtime
|
|
30
|
-
const runtime = initRuntime(options)
|
|
30
|
+
const runtime = initRuntime(options);
|
|
31
31
|
|
|
32
32
|
// Create broker instance
|
|
33
|
-
return createBrokerInstance(runtime)
|
|
34
|
-
}
|
|
33
|
+
return createBrokerInstance(runtime);
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* @deprecated since version 0.9.0
|
|
38
38
|
* @param {import('./types.js').BrokerOptions} options Broker options.
|
|
39
39
|
* @returns {import('./types.js').Broker} Broker instance
|
|
40
40
|
*/
|
|
41
|
-
exports.Weave = exports.createBroker
|
|
41
|
+
exports.Weave = exports.createBroker;
|
|
42
42
|
|
|
43
43
|
// Errors
|
|
44
|
-
exports.Errors = require('./errors')
|
|
44
|
+
exports.Errors = require('./errors');
|
|
45
45
|
|
|
46
|
-
exports.Constants = require('./constants')
|
|
47
|
-
|
|
48
|
-
// Transport
|
|
49
|
-
exports.TransportAdapters = require('./transport/adapters')
|
|
46
|
+
exports.Constants = require('./constants');
|
|
50
47
|
|
|
51
48
|
// Caching
|
|
52
|
-
exports.Cache = require('./cache')
|
|
49
|
+
exports.Cache = require('./cache/adapters');
|
|
53
50
|
|
|
54
51
|
/**
|
|
55
52
|
* @deprecated since version 0.10.0
|
|
56
53
|
*/
|
|
57
|
-
exports.createBaseTracingCollector = require('./tracing/collectors/base').createBaseTracingCollector
|
|
58
|
-
exports.
|
|
54
|
+
exports.createBaseTracingCollector = require('./tracing/collectors/base').createBaseTracingCollector;
|
|
55
|
+
exports.TransportAdapters = require('./transport/adapters');
|
|
56
|
+
exports.TracingAdapters = require('./tracing/collectors');
|
|
57
|
+
exports.CacheAdapters = require('./cache/adapters');
|
|
59
58
|
|
|
60
59
|
// Helper
|
|
61
|
-
exports.defineBrokerOptions = require('./helper/defineBrokerOptions')
|
|
62
|
-
exports.defineService = require('./helper/defineService')
|
|
63
|
-
exports.defineAction = require('./helper/defineAction')
|
|
60
|
+
exports.defineBrokerOptions = require('./helper/defineBrokerOptions');
|
|
61
|
+
exports.defineService = require('./helper/defineService');
|
|
62
|
+
exports.defineAction = require('./helper/defineAction');
|
package/lib/logger/base.js
CHANGED
|
@@ -1,54 +1,63 @@
|
|
|
1
|
-
const {
|
|
2
|
-
const {
|
|
1
|
+
const { WeaveError } = require('../errors');
|
|
2
|
+
const { isStandardLevel, levelMethods } = require('./levels');
|
|
3
|
+
const { noop, generateLogMethod } = require('./tools');
|
|
3
4
|
|
|
4
5
|
exports.initBase = (runtime) => {
|
|
5
6
|
runtime.setLevel = (level) => {
|
|
6
|
-
const { labels, values } = runtime.levels
|
|
7
|
+
const { labels, values } = runtime.levels;
|
|
8
|
+
|
|
9
|
+
// Handle number values for level
|
|
7
10
|
if (typeof level === 'number') {
|
|
8
|
-
if (labels[level] === undefined)
|
|
9
|
-
|
|
11
|
+
if (labels[level] === undefined) {
|
|
12
|
+
throw new WeaveError(`Unknown level value: "${level}"`);
|
|
13
|
+
}
|
|
14
|
+
level = labels[level];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Handle unknown log levels
|
|
18
|
+
if (values[level] === undefined) {
|
|
19
|
+
throw new WeaveError(`Unknown level: "${level}"`);
|
|
10
20
|
}
|
|
11
21
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
const useOnlyCustomLevelsVal = runtime.options.useOnlyCustomLevelsSym
|
|
16
|
-
const hook = runtime.options.hooks.logMethod
|
|
22
|
+
const levelVal = runtime.levelValue = values[level];
|
|
23
|
+
const useOnlyCustomLevelsVal = runtime.options.useOnlyCustomLevelsSym;
|
|
24
|
+
const hook = runtime.options.hooks.logMethod;
|
|
17
25
|
|
|
18
26
|
for (const key in values) {
|
|
19
27
|
if (levelVal < values[key]) {
|
|
20
|
-
runtime.logMethods[key] = noop
|
|
21
|
-
continue
|
|
28
|
+
runtime.logMethods[key] = noop;
|
|
29
|
+
continue;
|
|
22
30
|
}
|
|
23
|
-
runtime.logMethods[key] = isStandardLevel(key, useOnlyCustomLevelsVal) ? levelMethods[key](runtime, hook) : generateLogMethod(runtime, values[key], hook)
|
|
31
|
+
runtime.logMethods[key] = isStandardLevel(key, useOnlyCustomLevelsVal) ? levelMethods[key](runtime, hook) : generateLogMethod(runtime, values[key], hook);
|
|
24
32
|
}
|
|
25
|
-
}
|
|
33
|
+
};
|
|
26
34
|
|
|
27
|
-
runtime.write = (
|
|
28
|
-
const isErrorObject =
|
|
29
|
-
const mixin = runtime.mixin
|
|
30
|
-
const time = Date.now()
|
|
31
|
-
let object
|
|
35
|
+
runtime.write = (originObject, message, number) => {
|
|
36
|
+
const isErrorObject = originObject instanceof Error;
|
|
37
|
+
const mixin = runtime.mixin;
|
|
38
|
+
const time = Date.now();
|
|
39
|
+
let object;
|
|
32
40
|
|
|
33
|
-
if (
|
|
34
|
-
object = mixin ? mixin({}) : {}
|
|
41
|
+
if (originObject === undefined || originObject === null) {
|
|
42
|
+
object = mixin ? mixin({}) : {};
|
|
35
43
|
} else {
|
|
36
|
-
object = Object.assign(mixin ? mixin(
|
|
44
|
+
object = Object.assign(mixin ? mixin(originObject) : {}, originObject);
|
|
37
45
|
|
|
46
|
+
// If the object is an error object, we set the message to the error message.
|
|
38
47
|
if (!message && isErrorObject) {
|
|
39
|
-
message =
|
|
48
|
+
message = originObject.message;
|
|
40
49
|
}
|
|
41
50
|
|
|
42
51
|
if (isErrorObject) {
|
|
43
|
-
object.stack =
|
|
52
|
+
object.stack = originObject.stack;
|
|
44
53
|
if (!object.type) {
|
|
45
|
-
object.type = 'Error'
|
|
54
|
+
object.type = 'Error';
|
|
46
55
|
}
|
|
47
56
|
}
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
const logString = runtime.formatter(runtime, object, message, number, time)
|
|
59
|
+
const logString = runtime.formatter(runtime, object, message, number, time);
|
|
51
60
|
|
|
52
|
-
runtime.destination.write(logString)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
61
|
+
runtime.destination.write(logString);
|
|
62
|
+
};
|
|
63
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const { green, magenta, red, yellow, gray, cyan } = require('../utils/colorize')
|
|
2
|
-
const os = require('os')
|
|
1
|
+
const { green, magenta, red, yellow, gray, cyan } = require('../utils/colorize');
|
|
2
|
+
const os = require('os');
|
|
3
3
|
|
|
4
4
|
exports.asHumanReadable = ({ levels, options }, originObj, message, number, time) => {
|
|
5
|
-
let logResult = ''
|
|
5
|
+
let logResult = '';
|
|
6
6
|
|
|
7
7
|
const logLevelColors = {
|
|
8
8
|
fatal: magenta,
|
|
@@ -11,32 +11,32 @@ exports.asHumanReadable = ({ levels, options }, originObj, message, number, time
|
|
|
11
11
|
info: green,
|
|
12
12
|
debug: cyan,
|
|
13
13
|
verbose: gray
|
|
14
|
-
}
|
|
14
|
+
};
|
|
15
15
|
|
|
16
|
-
const currentLabel = levels.labels[number]
|
|
16
|
+
const currentLabel = levels.labels[number];
|
|
17
17
|
|
|
18
|
-
const color = logLevelColors[currentLabel] || yellow
|
|
18
|
+
const color = logLevelColors[currentLabel] || yellow;
|
|
19
19
|
// Log level label
|
|
20
|
-
logResult += color(currentLabel.toUpperCase())
|
|
20
|
+
logResult += color(currentLabel.toUpperCase());
|
|
21
21
|
|
|
22
22
|
// date time
|
|
23
|
-
logResult += ' [' + new Date(time).toISOString() + '] '
|
|
23
|
+
logResult += ' [' + new Date(time).toISOString() + '] ';
|
|
24
24
|
|
|
25
25
|
if (options.base.pid && options.base.hostname) {
|
|
26
|
-
logResult += ` (${options.base.pid} on ${options.base.hostname})
|
|
26
|
+
logResult += ` (${options.base.pid} on ${options.base.hostname})`;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
if (message) {
|
|
30
|
-
logResult += ' ' + color(message)
|
|
30
|
+
logResult += ' ' + color(message);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
if (Object.keys(originObj).length > 0) {
|
|
34
34
|
// logResult += gray(' Json:')
|
|
35
|
-
logResult += os.EOL
|
|
36
|
-
logResult += gray(JSON.stringify(originObj, null, 2))
|
|
35
|
+
logResult += os.EOL;
|
|
36
|
+
logResult += gray(JSON.stringify(originObj, null, 2));
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
logResult += os.EOL
|
|
39
|
+
logResult += os.EOL;
|
|
40
40
|
|
|
41
|
-
return logResult
|
|
42
|
-
}
|
|
41
|
+
return logResult;
|
|
42
|
+
};
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
const os = require('os')
|
|
1
|
+
const os = require('os');
|
|
2
2
|
|
|
3
3
|
exports.asJson = (runtime, originObj, message, number, time) => {
|
|
4
4
|
const data = {
|
|
5
5
|
level: number,
|
|
6
6
|
time,
|
|
7
7
|
...runtime.fixtures
|
|
8
|
-
}
|
|
8
|
+
};
|
|
9
9
|
|
|
10
10
|
if (message !== undefined) {
|
|
11
|
-
data[runtime.options.messageKey] = message
|
|
11
|
+
data[runtime.options.messageKey] = message;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const doesNotHaveOwnProperty = originObj.hasOwnProperty === undefined;
|
|
15
15
|
|
|
16
|
-
let value
|
|
16
|
+
let value;
|
|
17
17
|
for (const key in originObj) {
|
|
18
|
-
value = originObj[key]
|
|
19
|
-
if ((
|
|
20
|
-
data[key] = value
|
|
18
|
+
value = originObj[key];
|
|
19
|
+
if ((doesNotHaveOwnProperty || originObj.hasOwnProperty(key)) && value !== undefined) {
|
|
20
|
+
data[key] = value;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
return JSON.stringify(data) + os.EOL
|
|
25
|
-
}
|
|
24
|
+
return JSON.stringify(data) + os.EOL;
|
|
25
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { asJson } = require('./asJson')
|
|
2
|
-
const { asHumanReadable } = require('./asHumanReadable')
|
|
1
|
+
const { asJson } = require('./asJson');
|
|
2
|
+
const { asHumanReadable } = require('./asHumanReadable');
|
|
3
3
|
|
|
4
|
-
module.exports = { asJson, asHumanReadable }
|
|
4
|
+
module.exports = { asJson, asHumanReadable };
|
package/lib/logger/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2019 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const os = require('os')
|
|
8
|
-
const { initBase } = require('./base')
|
|
9
|
-
const { asJson, asHumanReadable } = require('./format')
|
|
10
|
-
const { mappings } = require('./levels')
|
|
11
|
-
const { coreFixtures } = require('./tools')
|
|
7
|
+
const os = require('os');
|
|
8
|
+
const { initBase } = require('./base');
|
|
9
|
+
const { asJson, asHumanReadable } = require('./format');
|
|
10
|
+
const { mappings } = require('./levels');
|
|
11
|
+
const { coreFixtures } = require('./tools');
|
|
12
12
|
|
|
13
|
-
const { pid } = process
|
|
14
|
-
const hostname = os.hostname()
|
|
13
|
+
const { pid } = process;
|
|
14
|
+
const hostname = os.hostname();
|
|
15
15
|
|
|
16
16
|
const defaultOptions = {
|
|
17
17
|
enabled: true,
|
|
@@ -29,50 +29,50 @@ const defaultOptions = {
|
|
|
29
29
|
messageFormat: false
|
|
30
30
|
},
|
|
31
31
|
destination: process.stdout
|
|
32
|
-
}
|
|
32
|
+
};
|
|
33
33
|
|
|
34
34
|
exports.createLogger = (options) => {
|
|
35
|
-
options = Object.assign(defaultOptions, options)
|
|
35
|
+
options = Object.assign(defaultOptions, options);
|
|
36
36
|
|
|
37
|
-
const instance = {}
|
|
37
|
+
const instance = {};
|
|
38
38
|
const runtime = {
|
|
39
39
|
options,
|
|
40
40
|
logMethods: {},
|
|
41
41
|
destination: options.destination,
|
|
42
42
|
formatter: process.stdout.isTTY ? asHumanReadable : asJson
|
|
43
|
-
}
|
|
43
|
+
};
|
|
44
44
|
|
|
45
45
|
if (options.enabled === false) {
|
|
46
|
-
options.level = 'silent'
|
|
46
|
+
options.level = 'silent';
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
if (options.base !== null) {
|
|
50
50
|
if (options.name === undefined) {
|
|
51
|
-
runtime.fixtures = coreFixtures(options.base)
|
|
51
|
+
runtime.fixtures = coreFixtures(options.base);
|
|
52
52
|
} else {
|
|
53
|
-
runtime.fixtures = coreFixtures(Object.assign({}, options.base, { name: options.name }))
|
|
53
|
+
runtime.fixtures = coreFixtures(Object.assign({}, options.base, { name: options.name }));
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
if (options.mixin && typeof options.mixin !== 'function') {
|
|
58
|
-
throw Error(`Unknown mixin type "${typeof options.mixin}" - expected "function"`)
|
|
58
|
+
throw Error(`Unknown mixin type "${typeof options.mixin}" - expected "function"`);
|
|
59
59
|
} else if (options.mixin) {
|
|
60
|
-
runtime.mixin = options.mixin
|
|
60
|
+
runtime.mixin = options.mixin;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
const levels = mappings(options.customLevels)
|
|
63
|
+
const levels = mappings(options.customLevels);
|
|
64
64
|
|
|
65
65
|
// merge levels in logger runtime
|
|
66
|
-
Object.assign(runtime, { levels })
|
|
66
|
+
Object.assign(runtime, { levels });
|
|
67
67
|
|
|
68
|
-
initBase(runtime)
|
|
68
|
+
initBase(runtime);
|
|
69
69
|
|
|
70
|
-
runtime.setLevel(options.level)
|
|
70
|
+
runtime.setLevel(options.level);
|
|
71
71
|
|
|
72
72
|
Object.assign(instance, {
|
|
73
73
|
levels,
|
|
74
74
|
...runtime.logMethods
|
|
75
|
-
})
|
|
75
|
+
});
|
|
76
76
|
|
|
77
|
-
return instance
|
|
78
|
-
}
|
|
77
|
+
return instance;
|
|
78
|
+
};
|
package/lib/logger/levels.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { generateLogMethod } = require('./tools')
|
|
1
|
+
const { generateLogMethod } = require('./tools');
|
|
2
2
|
|
|
3
3
|
const levels = {
|
|
4
4
|
verbose: 60,
|
|
@@ -7,14 +7,14 @@ const levels = {
|
|
|
7
7
|
warn: 30,
|
|
8
8
|
error: 20,
|
|
9
9
|
fatal: 10
|
|
10
|
-
}
|
|
10
|
+
};
|
|
11
11
|
|
|
12
12
|
// wrap log methods
|
|
13
13
|
const levelMethods = {
|
|
14
14
|
fatal: (runtime, hook) => {
|
|
15
|
-
const logFatal = generateLogMethod(runtime, levels.fatal, hook)
|
|
15
|
+
const logFatal = generateLogMethod(runtime, levels.fatal, hook);
|
|
16
16
|
return function (...args) {
|
|
17
|
-
logFatal.call(runtime, ...args)
|
|
17
|
+
logFatal.call(runtime, ...args);
|
|
18
18
|
// if (typeof stream.flushSync === 'function') {
|
|
19
19
|
// try {
|
|
20
20
|
// stream.flushSync()
|
|
@@ -22,48 +22,48 @@ const levelMethods = {
|
|
|
22
22
|
// // https://github.com/pinojs/pino/pull/740#discussion_r346788313
|
|
23
23
|
// }
|
|
24
24
|
// }
|
|
25
|
-
}
|
|
25
|
+
};
|
|
26
26
|
},
|
|
27
27
|
error: (runtime, hook) => generateLogMethod(runtime, levels.error, hook),
|
|
28
28
|
warn: (runtime, hook) => generateLogMethod(runtime, levels.warn, hook),
|
|
29
29
|
info: (runtime, hook) => generateLogMethod(runtime, levels.info, hook),
|
|
30
30
|
debug: (runtime, hook) => generateLogMethod(runtime, levels.debug, hook),
|
|
31
31
|
verbose: (runtime, hook) => generateLogMethod(runtime, levels.verbose, hook)
|
|
32
|
-
}
|
|
32
|
+
};
|
|
33
33
|
|
|
34
|
-
exports.levelMethods = levelMethods
|
|
34
|
+
exports.levelMethods = levelMethods;
|
|
35
35
|
|
|
36
36
|
const numbers = Object.keys(levels).reduce((o, k) => {
|
|
37
|
-
o[levels[k]] = k
|
|
38
|
-
return o
|
|
39
|
-
}, {})
|
|
37
|
+
o[levels[k]] = k;
|
|
38
|
+
return o;
|
|
39
|
+
}, {});
|
|
40
40
|
|
|
41
41
|
exports.mappings = (customLevels = null, useOnlyCustomLevels = false) => {
|
|
42
42
|
const customNums = customLevels ? Object.keys(customLevels).reduce((o, k) => {
|
|
43
|
-
o[customLevels[k]] = k
|
|
44
|
-
return o
|
|
43
|
+
o[customLevels[k]] = k;
|
|
44
|
+
return o;
|
|
45
45
|
}, {})
|
|
46
|
-
: null
|
|
46
|
+
: null;
|
|
47
47
|
|
|
48
48
|
const labels = Object.assign(
|
|
49
49
|
Object.create(Object.prototype, { Infinity: { value: 'silent' }}),
|
|
50
50
|
useOnlyCustomLevels ? null : numbers,
|
|
51
51
|
customNums
|
|
52
|
-
)
|
|
52
|
+
);
|
|
53
53
|
|
|
54
54
|
// Merge log levels with "silent" log level.
|
|
55
55
|
const values = Object.assign(
|
|
56
56
|
Object.create(Object.prototype, { silent: { value: 0 }}),
|
|
57
57
|
useOnlyCustomLevels ? null : levels,
|
|
58
58
|
customLevels
|
|
59
|
-
)
|
|
59
|
+
);
|
|
60
60
|
|
|
61
|
-
return { labels, values }
|
|
62
|
-
}
|
|
61
|
+
return { labels, values };
|
|
62
|
+
};
|
|
63
63
|
|
|
64
64
|
exports.isStandardLevel = (level, useOnlyCustomLevels) => {
|
|
65
65
|
if (useOnlyCustomLevels) {
|
|
66
|
-
return false
|
|
66
|
+
return false;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
switch (level) {
|
|
@@ -73,8 +73,8 @@ exports.isStandardLevel = (level, useOnlyCustomLevels) => {
|
|
|
73
73
|
case 'info':
|
|
74
74
|
case 'debug':
|
|
75
75
|
case 'verbose':
|
|
76
|
-
return true
|
|
76
|
+
return true;
|
|
77
77
|
default:
|
|
78
|
-
return false
|
|
78
|
+
return false;
|
|
79
79
|
}
|
|
80
|
-
}
|
|
80
|
+
};
|