@weave-js/core 0.10.0 → 0.12.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.
- package/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +33 -0
- package/lib/broker/context.js +58 -46
- package/lib/broker/{default-options.js → defaultOptions.js} +31 -13
- package/lib/broker/index.js +273 -251
- package/lib/buildRuntime.js +70 -0
- 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 +34 -11
- package/lib/constants.js +11 -8
- package/lib/{error-handler.js → errorHandler.js} +8 -8
- package/lib/errors.js +46 -39
- package/lib/helper/{define-action.js → defineAction.js} +5 -3
- package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +4 -2
- package/lib/helper/{define-service.js → defineService.js} +6 -3
- package/lib/index.js +25 -22
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -14
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +28 -25
- package/lib/logger/levels.js +23 -22
- package/lib/logger/tools.js +17 -13
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +89 -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 +82 -34
- package/lib/middlewares/bulkhead/index.js +34 -33
- package/lib/middlewares/cache/index.js +106 -0
- package/lib/middlewares/circuit-breaker/index.js +85 -60
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +14 -14
- package/lib/middlewares/index.js +3 -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 +18 -18
- package/lib/middlewares/tracing/index.js +50 -37
- package/lib/middlewares/tracing/tags.js +43 -0
- package/lib/middlewares/validator/index.js +32 -32
- package/lib/registry/{action-endpoint.js → actionEndpoint.js} +8 -8
- package/lib/registry/collections/{action-collection.js → actionCollection.js} +39 -35
- package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +64 -65
- package/lib/registry/collections/{event-collection.js → eventCollection.js} +65 -69
- package/lib/registry/collections/{node-collection.js → nodeCollection.js} +45 -45
- package/lib/registry/collections/{service-collection.js → serviceCollection.js} +61 -55
- package/lib/registry/eventEndpoint.js +26 -0
- 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 +147 -163
- 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/{service-item.js → serviceItem.js} +21 -21
- package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +21 -21
- package/lib/runtime/initCache.js +19 -0
- package/lib/runtime/{init-context-factory.js → initContextFactory.js} +22 -22
- package/lib/runtime/{init-eventbus.js → initEventbus.js} +54 -53
- package/lib/runtime/{init-logger.js → initLogger.js} +11 -11
- package/lib/runtime/{init-metrics.js → initMetrics.js} +55 -54
- package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +21 -21
- package/lib/runtime/{init-registry.js → initRegistry.js} +6 -6
- package/lib/runtime/initServiceManager.js +145 -0
- package/lib/runtime/{init-tracing.js → initTracing.js} +25 -24
- package/lib/runtime/{init-transport.js → initTransport.js} +5 -5
- package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +4 -4
- package/lib/runtime/{init-validator.js → initValidator.js} +5 -5
- package/lib/tracing/collectors/base.js +29 -37
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +16 -16
- package/lib/tracing/span.js +34 -32
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +75 -0
- package/lib/transport/adapters/adapteBase.js +107 -0
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +19 -19
- 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 +90 -76
- package/lib/transport/adapters/tcp/index.js +180 -175
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -38
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +58 -58
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +11 -0
- package/lib/transport/createTransport.js +377 -334
- package/lib/transport/errorPayloadFactory.js +15 -0
- package/lib/transport/messageHandlers.js +517 -0
- package/lib/transport/messageTypes.js +23 -0
- package/lib/types.js +95 -71
- package/lib/utils/index.js +2 -2
- package/lib/utils/options.js +47 -45
- package/lib/utils/{restore-error.js → restoreError.js} +4 -4
- package/package.json +6 -12
- package/CHANGELOG.md +0 -1334
- package/lib/build-runtime.js +0 -70
- package/lib/cache/base.js +0 -142
- package/lib/cache/index.js +0 -48
- package/lib/cache/memory.js +0 -121
- package/lib/middlewares/tracing/buildTags.js +0 -17
- package/lib/new-errors.js +0 -25
- package/lib/registry/event-endpoint.js +0 -26
- package/lib/runtime/init-cache.js +0 -10
- package/lib/runtime/init-service-manager.js +0 -128
- package/lib/tracing/collectors/console.js +0 -34
- package/lib/tracing/index.js +0 -3
- package/lib/tracing/tracer.js +0 -64
- package/lib/transport/adapters/adapter-base.js +0 -92
- package/lib/transport/message-handlers.js +0 -448
- package/lib/transport/message-types.js +0 -59
- package/lib/transport/message.js +0 -16
package/lib/errors.js
CHANGED
|
@@ -1,89 +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
|
-
|
|
7
|
+
const { ExtendableError } = require('./ExtendableError');
|
|
8
|
+
|
|
9
|
+
class WeaveError extends ExtendableError {
|
|
8
10
|
constructor (message, code, type, data) {
|
|
9
|
-
super(message)
|
|
10
|
-
this.name = this.constructor.name
|
|
11
|
-
this.code = code || 500
|
|
12
|
-
this.type = type
|
|
13
|
-
this.data = data
|
|
14
|
-
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;
|
|
15
17
|
}
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
class
|
|
20
|
+
class WeaveRetryableError extends WeaveError {
|
|
19
21
|
constructor (message, code, type, data) {
|
|
20
|
-
super(message)
|
|
21
|
-
this.code = code || 500
|
|
22
|
-
this.type = type
|
|
23
|
-
this.data = data
|
|
24
|
-
this.retryable = true
|
|
22
|
+
super(message);
|
|
23
|
+
this.code = code || 500;
|
|
24
|
+
this.type = type;
|
|
25
|
+
this.data = data;
|
|
26
|
+
this.retryable = true;
|
|
25
27
|
}
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
class WeaveServiceNotFoundError extends
|
|
30
|
+
class WeaveServiceNotFoundError extends WeaveRetryableError {
|
|
29
31
|
constructor (data = {}) {
|
|
30
|
-
let message
|
|
32
|
+
let message;
|
|
31
33
|
|
|
32
34
|
if (data.actionName && data.nodeId) {
|
|
33
|
-
message = `Service "${data.actionName}" not found on node "${data.nodeId}"
|
|
35
|
+
message = `Service "${data.actionName}" not found on node "${data.nodeId}".`;
|
|
34
36
|
} else if (data.actionName) {
|
|
35
|
-
message = `Service "${data.actionName}" not found
|
|
37
|
+
message = `Service "${data.actionName}" not found.`;
|
|
38
|
+
} else {
|
|
39
|
+
message = 'Service not found.';
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
super(message, 404, 'WEAVE_SERVICE_NOT_FOUND_ERROR', data)
|
|
42
|
+
super(message, 404, 'WEAVE_SERVICE_NOT_FOUND_ERROR', data);
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
|
|
42
|
-
class WeaveServiceNotAvailableError extends
|
|
46
|
+
class WeaveServiceNotAvailableError extends WeaveRetryableError {
|
|
43
47
|
constructor (data = {}) {
|
|
44
|
-
let message
|
|
48
|
+
let message;
|
|
45
49
|
if (data.nodeId) {
|
|
46
|
-
message = `Service "${data.actionName}" not available on node "${data.nodeId}"
|
|
50
|
+
message = `Service "${data.actionName}" not available on node "${data.nodeId}".`;
|
|
51
|
+
} else if (data.actionName) {
|
|
52
|
+
message = `Service "${data.actionName}" not available.`;
|
|
47
53
|
} else {
|
|
48
|
-
message =
|
|
54
|
+
message = 'Service not available.';
|
|
49
55
|
}
|
|
50
56
|
|
|
51
|
-
super(message,
|
|
57
|
+
super(message, 503, 'WEAVE_SERVICE_NOT_AVAILABLE_ERROR', data);
|
|
52
58
|
}
|
|
53
59
|
}
|
|
54
60
|
|
|
55
|
-
class WeaveRequestTimeoutError extends
|
|
61
|
+
class WeaveRequestTimeoutError extends WeaveRetryableError {
|
|
56
62
|
constructor (actionName, nodeId, timeout) {
|
|
57
|
-
const message = `Action ${actionName} timed out node ${nodeId || '<local>'}
|
|
63
|
+
const message = `Action ${actionName} timed out node ${nodeId || '<local>'}.`;
|
|
58
64
|
super(message, 504, 'WEAVE_REQUEST_TIMEOUT_ERROR', {
|
|
59
65
|
actionName,
|
|
60
|
-
nodeId
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
nodeId,
|
|
67
|
+
timeout
|
|
68
|
+
});
|
|
69
|
+
this.retryable = true;
|
|
63
70
|
}
|
|
64
71
|
}
|
|
65
72
|
|
|
66
73
|
class WeaveParameterValidationError extends WeaveError {
|
|
67
74
|
constructor (message, data) {
|
|
68
|
-
super(message, 422, 'WEAVE_PARAMETER_VALIDATION_ERROR', data)
|
|
75
|
+
super(message, 422, 'WEAVE_PARAMETER_VALIDATION_ERROR', data);
|
|
69
76
|
}
|
|
70
77
|
}
|
|
71
78
|
|
|
72
79
|
class WeaveBrokerOptionsError extends WeaveError {
|
|
73
80
|
constructor (message, data) {
|
|
74
|
-
super(message, 500, 'WEAVE_BROKER_OPTIONS_ERROR', data)
|
|
81
|
+
super(message, 500, 'WEAVE_BROKER_OPTIONS_ERROR', data);
|
|
75
82
|
}
|
|
76
83
|
}
|
|
77
84
|
|
|
78
85
|
class WeaveQueueSizeExceededError extends WeaveError {
|
|
79
86
|
constructor (data) {
|
|
80
|
-
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);
|
|
81
88
|
}
|
|
82
89
|
}
|
|
83
90
|
|
|
84
91
|
class WeaveMaxCallLevelError extends WeaveError {
|
|
85
92
|
constructor (data) {
|
|
86
|
-
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);
|
|
87
94
|
}
|
|
88
95
|
}
|
|
89
96
|
|
|
@@ -92,22 +99,22 @@ class WeaveGracefulStopTimeoutError extends WeaveError {
|
|
|
92
99
|
super(`Unable to stop service "${service.name}"`, 500, 'GRACEFUL_STOP_TIMEOUT', {
|
|
93
100
|
name: service.name,
|
|
94
101
|
version: service.version
|
|
95
|
-
})
|
|
102
|
+
});
|
|
96
103
|
}
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
const restoreError = error => {
|
|
100
|
-
const ErrorClass = module.exports[error.name]
|
|
107
|
+
const ErrorClass = module.exports[error.name];
|
|
101
108
|
|
|
102
109
|
if (ErrorClass) {
|
|
103
110
|
switch (error.name) {
|
|
104
111
|
case 'WeaveError':
|
|
105
|
-
return new ErrorClass(error.message, error.code, error.type, error.data)
|
|
112
|
+
return new ErrorClass(error.message, error.code, error.type, error.data);
|
|
106
113
|
// case 'WeaveParameterValidationError':
|
|
107
114
|
// return new ErrorClass(error.message, error.data)
|
|
108
115
|
}
|
|
109
116
|
}
|
|
110
|
-
}
|
|
117
|
+
};
|
|
111
118
|
|
|
112
119
|
module.exports = {
|
|
113
120
|
WeaveBrokerOptionsError,
|
|
@@ -116,9 +123,9 @@ module.exports = {
|
|
|
116
123
|
WeaveParameterValidationError,
|
|
117
124
|
WeaveQueueSizeExceededError,
|
|
118
125
|
WeaveRequestTimeoutError,
|
|
119
|
-
|
|
126
|
+
WeaveRetryableError,
|
|
120
127
|
WeaveServiceNotAvailableError,
|
|
121
128
|
WeaveServiceNotFoundError,
|
|
122
129
|
WeaveGracefulStopTimeoutError,
|
|
123
130
|
restoreError
|
|
124
|
-
}
|
|
131
|
+
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/* istanbul ignore next */
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @typedef {import('../types.js').ServiceActionDefinition} ServiceActionDefinition
|
|
3
5
|
*/
|
|
@@ -6,7 +8,7 @@
|
|
|
6
8
|
* Create and register a new service action.
|
|
7
9
|
* @param {ServiceActionDefinition} actionDefinition - Schema of the the action
|
|
8
10
|
* @returns {ServiceActionDefinition} Action definition
|
|
9
|
-
|
|
11
|
+
*/
|
|
10
12
|
module.exports = function (actionDefinition) {
|
|
11
|
-
return actionDefinition
|
|
12
|
-
}
|
|
13
|
+
return actionDefinition;
|
|
14
|
+
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/* istanbul ignore next */
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @typedef {import('../types.js').BrokerOptions} BrokerOptions
|
|
3
5
|
*/
|
|
@@ -8,5 +10,5 @@
|
|
|
8
10
|
* @returns {BrokerOptions} Broker options
|
|
9
11
|
*/
|
|
10
12
|
module.exports = function (options) {
|
|
11
|
-
return options
|
|
12
|
-
}
|
|
13
|
+
return options;
|
|
14
|
+
};
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
|
|
2
|
+
/* istanbul ignore next */
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* @typedef {import('../types.js').ServiceSchema} ServiceSchema
|
|
3
6
|
*/
|
|
@@ -6,7 +9,7 @@
|
|
|
6
9
|
* Create and register a new service
|
|
7
10
|
* @param {ServiceSchema} serviceSchema - Schema of the Service
|
|
8
11
|
* @returns {ServiceSchema} Service schema
|
|
9
|
-
|
|
12
|
+
*/
|
|
10
13
|
module.exports = function (serviceSchema) {
|
|
11
|
-
return serviceSchema
|
|
12
|
-
}
|
|
14
|
+
return serviceSchema;
|
|
15
|
+
};
|
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/
|
|
8
|
-
const { defaultsDeep } = require('@weave-js/utils')
|
|
9
|
-
const {
|
|
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 =
|
|
15
|
+
exports.defaultOptions = getDefaultOptions();
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Build runtime object
|
|
@@ -21,39 +21,42 @@ exports.defaultOptions = require('./broker/default-options')
|
|
|
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 =
|
|
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
|
-
// eslint-disable-next-line valid-jsdoc
|
|
37
36
|
/**
|
|
38
37
|
* @deprecated since version 0.9.0
|
|
39
38
|
* @param {import('./types.js').BrokerOptions} options Broker options.
|
|
40
39
|
* @returns {import('./types.js').Broker} Broker instance
|
|
41
40
|
*/
|
|
42
|
-
exports.Weave = exports.createBroker
|
|
41
|
+
exports.Weave = exports.createBroker;
|
|
43
42
|
|
|
44
43
|
// Errors
|
|
45
|
-
exports.Errors = require('./errors')
|
|
44
|
+
exports.Errors = require('./errors');
|
|
46
45
|
|
|
47
|
-
exports.Constants = require('./constants')
|
|
48
|
-
// Transport
|
|
49
|
-
exports.TransportAdapters = require('./transport/adapters')
|
|
46
|
+
exports.Constants = require('./constants');
|
|
50
47
|
|
|
51
48
|
// Caching
|
|
52
|
-
exports.Cache = require('./cache')
|
|
53
|
-
|
|
54
|
-
|
|
49
|
+
exports.Cache = require('./cache/adapters');
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @deprecated since version 0.10.0
|
|
53
|
+
*/
|
|
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');
|
|
55
58
|
|
|
56
59
|
// Helper
|
|
57
|
-
exports.defineBrokerOptions = require('./helper/
|
|
58
|
-
exports.defineService = require('./helper/
|
|
59
|
-
exports.defineAction = require('./helper/
|
|
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,
|
|
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,31 +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
19
|
// Log level label
|
|
19
|
-
logResult +=
|
|
20
|
+
logResult += color(currentLabel.toUpperCase());
|
|
20
21
|
|
|
21
22
|
// date time
|
|
22
|
-
logResult += ' [' + new Date(time).toISOString() + '] '
|
|
23
|
+
logResult += ' [' + new Date(time).toISOString() + '] ';
|
|
23
24
|
|
|
24
25
|
if (options.base.pid && options.base.hostname) {
|
|
25
|
-
logResult += ` (${options.base.pid} on ${options.base.hostname})
|
|
26
|
+
logResult += ` (${options.base.pid} on ${options.base.hostname})`;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
if (message) {
|
|
29
|
-
logResult += ' ' +
|
|
30
|
+
logResult += ' ' + color(message);
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
if (Object.keys(originObj).length > 0) {
|
|
33
34
|
// logResult += gray(' Json:')
|
|
34
|
-
logResult += os.EOL
|
|
35
|
-
logResult += gray(JSON.stringify(originObj, null, 2))
|
|
35
|
+
logResult += os.EOL;
|
|
36
|
+
logResult += gray(JSON.stringify(originObj, null, 2));
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
logResult += os.EOL
|
|
39
|
+
logResult += os.EOL;
|
|
39
40
|
|
|
40
|
-
return logResult
|
|
41
|
-
}
|
|
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,24 +1,27 @@
|
|
|
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,
|
|
18
18
|
level: 'info',
|
|
19
19
|
messageKey: 'message',
|
|
20
20
|
customLevels: null,
|
|
21
|
-
base: {
|
|
21
|
+
base: {
|
|
22
|
+
pid,
|
|
23
|
+
hostname
|
|
24
|
+
},
|
|
22
25
|
hooks: {
|
|
23
26
|
logMethod: undefined
|
|
24
27
|
},
|
|
@@ -26,50 +29,50 @@ const defaultOptions = {
|
|
|
26
29
|
messageFormat: false
|
|
27
30
|
},
|
|
28
31
|
destination: process.stdout
|
|
29
|
-
}
|
|
32
|
+
};
|
|
30
33
|
|
|
31
34
|
exports.createLogger = (options) => {
|
|
32
|
-
options = Object.assign(defaultOptions, options)
|
|
35
|
+
options = Object.assign(defaultOptions, options);
|
|
33
36
|
|
|
34
|
-
const instance = {}
|
|
37
|
+
const instance = {};
|
|
35
38
|
const runtime = {
|
|
36
39
|
options,
|
|
37
40
|
logMethods: {},
|
|
38
41
|
destination: options.destination,
|
|
39
42
|
formatter: process.stdout.isTTY ? asHumanReadable : asJson
|
|
40
|
-
}
|
|
43
|
+
};
|
|
41
44
|
|
|
42
45
|
if (options.enabled === false) {
|
|
43
|
-
options.level = 'silent'
|
|
46
|
+
options.level = 'silent';
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
if (options.base !== null) {
|
|
47
50
|
if (options.name === undefined) {
|
|
48
|
-
runtime.fixtures = coreFixtures(options.base)
|
|
51
|
+
runtime.fixtures = coreFixtures(options.base);
|
|
49
52
|
} else {
|
|
50
|
-
runtime.fixtures = coreFixtures(Object.assign({}, options.base, { name: options.name }))
|
|
53
|
+
runtime.fixtures = coreFixtures(Object.assign({}, options.base, { name: options.name }));
|
|
51
54
|
}
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
if (options.mixin && typeof options.mixin !== 'function') {
|
|
55
|
-
throw Error(`Unknown mixin type "${typeof options.mixin}" - expected "function"`)
|
|
58
|
+
throw Error(`Unknown mixin type "${typeof options.mixin}" - expected "function"`);
|
|
56
59
|
} else if (options.mixin) {
|
|
57
|
-
runtime.mixin = options.mixin
|
|
60
|
+
runtime.mixin = options.mixin;
|
|
58
61
|
}
|
|
59
62
|
|
|
60
|
-
const levels = mappings(options.customLevels)
|
|
63
|
+
const levels = mappings(options.customLevels);
|
|
61
64
|
|
|
62
65
|
// merge levels in logger runtime
|
|
63
|
-
Object.assign(runtime, { levels })
|
|
66
|
+
Object.assign(runtime, { levels });
|
|
64
67
|
|
|
65
|
-
initBase(runtime)
|
|
68
|
+
initBase(runtime);
|
|
66
69
|
|
|
67
|
-
runtime.setLevel(options.level)
|
|
70
|
+
runtime.setLevel(options.level);
|
|
68
71
|
|
|
69
72
|
Object.assign(instance, {
|
|
70
73
|
levels,
|
|
71
74
|
...runtime.logMethods
|
|
72
|
-
})
|
|
75
|
+
});
|
|
73
76
|
|
|
74
|
-
return instance
|
|
75
|
-
}
|
|
77
|
+
return instance;
|
|
78
|
+
};
|