@commercetools-backend/loggers 0.0.0-CRAFT-1791-20251006162610
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/LICENSE +21 -0
- package/README.md +94 -0
- package/dist/commercetools-backend-loggers.cjs.d.ts +2 -0
- package/dist/commercetools-backend-loggers.cjs.dev.js +156 -0
- package/dist/commercetools-backend-loggers.cjs.js +7 -0
- package/dist/commercetools-backend-loggers.cjs.prod.js +156 -0
- package/dist/commercetools-backend-loggers.esm.js +114 -0
- package/dist/declarations/src/create-application-logger.d.ts +4 -0
- package/dist/declarations/src/formatters/rewrite-fields.d.ts +11 -0
- package/dist/declarations/src/index.d.ts +3 -0
- package/dist/declarations/src/middlewares/create-access-logger.d.ts +5 -0
- package/dist/declarations/src/types.d.ts +10 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) commercetools GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# @commercetools-backend/loggers
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/@commercetools-backend/loggers"><img src="https://badgen.net/npm/v/@commercetools-backend/loggers" alt="Latest release (latest dist-tag)" /></a> <a href="https://www.npmjs.com/package/@commercetools-backend/loggers"><img src="https://badgen.net/npm/v/@commercetools-backend/loggers/next" alt="Latest release (next dist-tag)" /></a> <a href="https://bundlephobia.com/result?p=@commercetools-backend/loggers"><img src="https://badgen.net/bundlephobia/minzip/@commercetools-backend/loggers" alt="Minified + GZipped size" /></a> <a href="https://github.com/commercetools/merchant-center-application-kit/blob/main/LICENSE"><img src="https://badgen.net/github/license/commercetools/merchant-center-application-kit" alt="GitHub license" /></a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
Opinionated JSON loggers for HTTP server applications.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
$ npm install --save @commercetools-backend/loggers
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Middlewares
|
|
16
|
+
|
|
17
|
+
### Access logger
|
|
18
|
+
|
|
19
|
+
Creates a logger to be used for HTTP requests access logs.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const {
|
|
23
|
+
createAccessLoggerMiddleware,
|
|
24
|
+
} = require('@commercetools-backend/loggers');
|
|
25
|
+
|
|
26
|
+
app.use(createAccessLoggerMiddleware());
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Options**
|
|
30
|
+
|
|
31
|
+
- `level` (_string_): The log level to be used. **Default: `info`**
|
|
32
|
+
- `silent` (_boolean_): In case logs should be skipped. **Default: `false`**
|
|
33
|
+
- `json` (_boolean_): To use the JSON formatter, otherwise falls back to CLI format. It's recommended to use the JSON formatter on production.
|
|
34
|
+
- `formatters` (_Array of Winston formatters_): In case you want to pass additional Winston formatters.
|
|
35
|
+
- `ignoreUrls` (_Array of string_): A list of URL paths to be ignored from being logged.
|
|
36
|
+
|
|
37
|
+
## Application logger
|
|
38
|
+
|
|
39
|
+
Creates a logger to be used programmatically in the application code.
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
const { createApplicationLogger } = require('@commercetools-backend/loggers');
|
|
43
|
+
|
|
44
|
+
const app = createApplicationLogger();
|
|
45
|
+
|
|
46
|
+
app.info('Hey there', { meta: { name: 'Tom' } });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**Options**
|
|
50
|
+
|
|
51
|
+
- `level` (_string_): The log level to be used. **Default: `info`**
|
|
52
|
+
- `silent` (_boolean_): In case logs should be skipped. **Default: `false`**
|
|
53
|
+
- `json` (_boolean_): To use the JSON formatter, otherwise falls back to CLI format. It's recommended to use the JSON formatter on production.
|
|
54
|
+
- `formatters` (_Array of Winston formatters_): In case you want to pass additional Winston formatters.
|
|
55
|
+
|
|
56
|
+
## Formatters
|
|
57
|
+
|
|
58
|
+
The package provides some come Winston formatters that can be passed to the given loggers.
|
|
59
|
+
|
|
60
|
+
### Rewrite fields
|
|
61
|
+
|
|
62
|
+
This formatter allows to rewrite fields from the JSON logger. It can be useful for redacting insecure information, or to map certain fields to a specific format (for example for Kibana).
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
const {
|
|
66
|
+
createAccessLoggerMiddleware,
|
|
67
|
+
rewriteFieldsFormatter,
|
|
68
|
+
} = require('@commercetools-backend/loggers');
|
|
69
|
+
|
|
70
|
+
app.use(
|
|
71
|
+
createAccessLoggerMiddleware({
|
|
72
|
+
formatters: [
|
|
73
|
+
rewriteFieldsFormatter({
|
|
74
|
+
fields: [
|
|
75
|
+
{ from: 'level', to: 'logLevel' },
|
|
76
|
+
{ from: 'meta.error.message', to: 'meta.errorMessage' },
|
|
77
|
+
{
|
|
78
|
+
from: 'meta.error',
|
|
79
|
+
to: 'meta.errorJsonString',
|
|
80
|
+
replaceValue: (value) => JSON.stringify(value),
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
}),
|
|
84
|
+
],
|
|
85
|
+
})
|
|
86
|
+
);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Options**
|
|
90
|
+
|
|
91
|
+
- `fields` (_Array of RewriteField_): A `RewriteField` is an object with the following properties:
|
|
92
|
+
- `from` (_string_): A JSON path to one of the fields of the log information that needs to be rewritten. The field will be deleted.
|
|
93
|
+
- `to` (_string_): A JSON path to the new field that should be created.
|
|
94
|
+
- `replaceValue` (_function_): An optional function that takes the value from the original field and returns a new value for the field. It can be used for example to serialize the value with `JSON.stringify`.
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "./declarations/src/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29tbWVyY2V0b29scy1iYWNrZW5kLWxvZ2dlcnMuY2pzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvaW5kZXguZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var expressWinston = require('express-winston');
|
|
6
|
+
var winston = require('winston');
|
|
7
|
+
var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty');
|
|
8
|
+
var _slicedToArray = require('@babel/runtime-corejs3/helpers/slicedToArray');
|
|
9
|
+
var _Array$isArray = require('@babel/runtime-corejs3/core-js-stable/array/is-array');
|
|
10
|
+
var _someInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/some');
|
|
11
|
+
var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys');
|
|
12
|
+
var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols');
|
|
13
|
+
var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
|
14
|
+
var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor');
|
|
15
|
+
var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each');
|
|
16
|
+
var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
|
|
17
|
+
var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
|
|
18
|
+
var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property');
|
|
19
|
+
var cloneDeep = require('lodash/cloneDeep');
|
|
20
|
+
var getIn = require('lodash/get');
|
|
21
|
+
var setIn = require('lodash/set');
|
|
22
|
+
var unsetIn = require('lodash/unset');
|
|
23
|
+
var logform = require('logform');
|
|
24
|
+
|
|
25
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
26
|
+
|
|
27
|
+
function _interopNamespace(e) {
|
|
28
|
+
if (e && e.__esModule) return e;
|
|
29
|
+
var n = Object.create(null);
|
|
30
|
+
if (e) {
|
|
31
|
+
Object.keys(e).forEach(function (k) {
|
|
32
|
+
if (k !== 'default') {
|
|
33
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
34
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: function () { return e[k]; }
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
n["default"] = e;
|
|
42
|
+
return Object.freeze(n);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
var expressWinston__default = /*#__PURE__*/_interopDefault(expressWinston);
|
|
46
|
+
var winston__namespace = /*#__PURE__*/_interopNamespace(winston);
|
|
47
|
+
var _Array$isArray__default = /*#__PURE__*/_interopDefault(_Array$isArray);
|
|
48
|
+
var _someInstanceProperty__default = /*#__PURE__*/_interopDefault(_someInstanceProperty);
|
|
49
|
+
var _Object$keys__default = /*#__PURE__*/_interopDefault(_Object$keys);
|
|
50
|
+
var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertySymbols);
|
|
51
|
+
var _filterInstanceProperty__default = /*#__PURE__*/_interopDefault(_filterInstanceProperty);
|
|
52
|
+
var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptor);
|
|
53
|
+
var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefault(_forEachInstanceProperty);
|
|
54
|
+
var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptors);
|
|
55
|
+
var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties);
|
|
56
|
+
var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty);
|
|
57
|
+
var cloneDeep__default = /*#__PURE__*/_interopDefault(cloneDeep);
|
|
58
|
+
var getIn__default = /*#__PURE__*/_interopDefault(getIn);
|
|
59
|
+
var setIn__default = /*#__PURE__*/_interopDefault(setIn);
|
|
60
|
+
var unsetIn__default = /*#__PURE__*/_interopDefault(unsetIn);
|
|
61
|
+
|
|
62
|
+
function ownKeys(e, r) { var t = _Object$keys__default["default"](e); if (_Object$getOwnPropertySymbols__default["default"]) { var o = _Object$getOwnPropertySymbols__default["default"](e); r && (o = _filterInstanceProperty__default["default"](o).call(o, function (r) { return _Object$getOwnPropertyDescriptor__default["default"](e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
63
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context2, _context3; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty__default["default"](_context2 = ownKeys(Object(t), !0)).call(_context2, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](e, _Object$getOwnPropertyDescriptors__default["default"](t)) : _forEachInstanceProperty__default["default"](_context3 = ownKeys(Object(t))).call(_context3, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; }
|
|
64
|
+
const parseIps = request => {
|
|
65
|
+
const forwardedFor = request.headers['x-forwarded-for'];
|
|
66
|
+
if (!forwardedFor) {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
const remoteAddresses = _Array$isArray__default["default"](forwardedFor) ? forwardedFor : forwardedFor.split(',');
|
|
70
|
+
return remoteAddresses;
|
|
71
|
+
};
|
|
72
|
+
const createAccessLogSkipper = options => request => {
|
|
73
|
+
var _context;
|
|
74
|
+
if (Boolean(options.silent)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
const hasMatchingIgnoreUrl = _someInstanceProperty__default["default"](_context = options.ignoreUrls ?? []).call(_context, uriPathOrRegex => {
|
|
78
|
+
if (typeof uriPathOrRegex === 'string') {
|
|
79
|
+
return request.originalUrl === uriPathOrRegex;
|
|
80
|
+
}
|
|
81
|
+
return request.originalUrl.match(uriPathOrRegex);
|
|
82
|
+
});
|
|
83
|
+
return hasMatchingIgnoreUrl;
|
|
84
|
+
};
|
|
85
|
+
const mapRequestMetadata = request => {
|
|
86
|
+
try {
|
|
87
|
+
const remoteAddress = request.socket?.remoteAddress;
|
|
88
|
+
const proxyIps = parseIps(request);
|
|
89
|
+
const _proxyIps = _slicedToArray(proxyIps, 1),
|
|
90
|
+
clientIp = _proxyIps[0];
|
|
91
|
+
return _objectSpread({
|
|
92
|
+
clientIp: clientIp ?? remoteAddress,
|
|
93
|
+
proxyIps,
|
|
94
|
+
hostname: request.socket ? request.hostname : undefined
|
|
95
|
+
}, remoteAddress ? {
|
|
96
|
+
remoteAddress
|
|
97
|
+
} : {});
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error(`Failed to parse request metadata`, error);
|
|
100
|
+
return {};
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const createAccessLoggerMiddleware = function () {
|
|
105
|
+
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
106
|
+
const formatters = winston__namespace["default"].format.combine(winston__namespace["default"].format.timestamp(), ...(options.formatters ?? []), options.json ? winston__namespace["default"].format.json() : winston__namespace["default"].format.cli());
|
|
107
|
+
const skip = createAccessLogSkipper(options);
|
|
108
|
+
return expressWinston__default["default"].logger({
|
|
109
|
+
level: options.level ?? 'info',
|
|
110
|
+
transports: [new winston__namespace["default"].transports.Console()],
|
|
111
|
+
format: formatters,
|
|
112
|
+
meta: true,
|
|
113
|
+
expressFormat: true,
|
|
114
|
+
// Use default morgan access log formatting
|
|
115
|
+
colorize: !options.json,
|
|
116
|
+
skip,
|
|
117
|
+
dynamicMeta: mapRequestMetadata
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const createApplicationLogger = function () {
|
|
122
|
+
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
123
|
+
const formatters = winston__namespace.format.combine(...(options.formatters ?? []), options.json ? winston__namespace.format.json() : winston__namespace.format.combine(winston__namespace.format.cli(), winston__namespace.format.simple()));
|
|
124
|
+
return winston__namespace.createLogger({
|
|
125
|
+
level: options.level ?? 'info',
|
|
126
|
+
format: formatters,
|
|
127
|
+
transports: [new winston__namespace.transports.Console({
|
|
128
|
+
silent: Boolean(options.silent)
|
|
129
|
+
})]
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
function rewriteField(info, field) {
|
|
134
|
+
const fromFieldValue = getIn__default["default"](info, field.from);
|
|
135
|
+
const preserveFromField = field.preserveFromField ?? false;
|
|
136
|
+
if (fromFieldValue) {
|
|
137
|
+
if (!preserveFromField) {
|
|
138
|
+
unsetIn__default["default"](info, field.from);
|
|
139
|
+
}
|
|
140
|
+
setIn__default["default"](info, field.to, field.replaceValue ? field.replaceValue(fromFieldValue) : fromFieldValue);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function rewriteFieldsFormatter(options) {
|
|
144
|
+
return logform.format(info => {
|
|
145
|
+
var _context;
|
|
146
|
+
const clone = cloneDeep__default["default"](info);
|
|
147
|
+
_forEachInstanceProperty__default["default"](_context = options.fields).call(_context, field => {
|
|
148
|
+
rewriteField(clone, field);
|
|
149
|
+
});
|
|
150
|
+
return clone;
|
|
151
|
+
})(options);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
exports.createAccessLoggerMiddleware = createAccessLoggerMiddleware;
|
|
155
|
+
exports.createApplicationLogger = createApplicationLogger;
|
|
156
|
+
exports.rewriteFieldsFormatter = rewriteFieldsFormatter;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var expressWinston = require('express-winston');
|
|
6
|
+
var winston = require('winston');
|
|
7
|
+
var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty');
|
|
8
|
+
var _slicedToArray = require('@babel/runtime-corejs3/helpers/slicedToArray');
|
|
9
|
+
var _Array$isArray = require('@babel/runtime-corejs3/core-js-stable/array/is-array');
|
|
10
|
+
var _someInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/some');
|
|
11
|
+
var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys');
|
|
12
|
+
var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols');
|
|
13
|
+
var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
|
14
|
+
var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor');
|
|
15
|
+
var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each');
|
|
16
|
+
var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
|
|
17
|
+
var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
|
|
18
|
+
var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property');
|
|
19
|
+
var cloneDeep = require('lodash/cloneDeep');
|
|
20
|
+
var getIn = require('lodash/get');
|
|
21
|
+
var setIn = require('lodash/set');
|
|
22
|
+
var unsetIn = require('lodash/unset');
|
|
23
|
+
var logform = require('logform');
|
|
24
|
+
|
|
25
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
26
|
+
|
|
27
|
+
function _interopNamespace(e) {
|
|
28
|
+
if (e && e.__esModule) return e;
|
|
29
|
+
var n = Object.create(null);
|
|
30
|
+
if (e) {
|
|
31
|
+
Object.keys(e).forEach(function (k) {
|
|
32
|
+
if (k !== 'default') {
|
|
33
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
34
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: function () { return e[k]; }
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
n["default"] = e;
|
|
42
|
+
return Object.freeze(n);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
var expressWinston__default = /*#__PURE__*/_interopDefault(expressWinston);
|
|
46
|
+
var winston__namespace = /*#__PURE__*/_interopNamespace(winston);
|
|
47
|
+
var _Array$isArray__default = /*#__PURE__*/_interopDefault(_Array$isArray);
|
|
48
|
+
var _someInstanceProperty__default = /*#__PURE__*/_interopDefault(_someInstanceProperty);
|
|
49
|
+
var _Object$keys__default = /*#__PURE__*/_interopDefault(_Object$keys);
|
|
50
|
+
var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertySymbols);
|
|
51
|
+
var _filterInstanceProperty__default = /*#__PURE__*/_interopDefault(_filterInstanceProperty);
|
|
52
|
+
var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptor);
|
|
53
|
+
var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefault(_forEachInstanceProperty);
|
|
54
|
+
var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptors);
|
|
55
|
+
var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties);
|
|
56
|
+
var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty);
|
|
57
|
+
var cloneDeep__default = /*#__PURE__*/_interopDefault(cloneDeep);
|
|
58
|
+
var getIn__default = /*#__PURE__*/_interopDefault(getIn);
|
|
59
|
+
var setIn__default = /*#__PURE__*/_interopDefault(setIn);
|
|
60
|
+
var unsetIn__default = /*#__PURE__*/_interopDefault(unsetIn);
|
|
61
|
+
|
|
62
|
+
function ownKeys(e, r) { var t = _Object$keys__default["default"](e); if (_Object$getOwnPropertySymbols__default["default"]) { var o = _Object$getOwnPropertySymbols__default["default"](e); r && (o = _filterInstanceProperty__default["default"](o).call(o, function (r) { return _Object$getOwnPropertyDescriptor__default["default"](e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
63
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context2, _context3; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty__default["default"](_context2 = ownKeys(Object(t), !0)).call(_context2, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](e, _Object$getOwnPropertyDescriptors__default["default"](t)) : _forEachInstanceProperty__default["default"](_context3 = ownKeys(Object(t))).call(_context3, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; }
|
|
64
|
+
const parseIps = request => {
|
|
65
|
+
const forwardedFor = request.headers['x-forwarded-for'];
|
|
66
|
+
if (!forwardedFor) {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
const remoteAddresses = _Array$isArray__default["default"](forwardedFor) ? forwardedFor : forwardedFor.split(',');
|
|
70
|
+
return remoteAddresses;
|
|
71
|
+
};
|
|
72
|
+
const createAccessLogSkipper = options => request => {
|
|
73
|
+
var _context;
|
|
74
|
+
if (Boolean(options.silent)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
const hasMatchingIgnoreUrl = _someInstanceProperty__default["default"](_context = options.ignoreUrls ?? []).call(_context, uriPathOrRegex => {
|
|
78
|
+
if (typeof uriPathOrRegex === 'string') {
|
|
79
|
+
return request.originalUrl === uriPathOrRegex;
|
|
80
|
+
}
|
|
81
|
+
return request.originalUrl.match(uriPathOrRegex);
|
|
82
|
+
});
|
|
83
|
+
return hasMatchingIgnoreUrl;
|
|
84
|
+
};
|
|
85
|
+
const mapRequestMetadata = request => {
|
|
86
|
+
try {
|
|
87
|
+
const remoteAddress = request.socket?.remoteAddress;
|
|
88
|
+
const proxyIps = parseIps(request);
|
|
89
|
+
const _proxyIps = _slicedToArray(proxyIps, 1),
|
|
90
|
+
clientIp = _proxyIps[0];
|
|
91
|
+
return _objectSpread({
|
|
92
|
+
clientIp: clientIp ?? remoteAddress,
|
|
93
|
+
proxyIps,
|
|
94
|
+
hostname: request.socket ? request.hostname : undefined
|
|
95
|
+
}, remoteAddress ? {
|
|
96
|
+
remoteAddress
|
|
97
|
+
} : {});
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error(`Failed to parse request metadata`, error);
|
|
100
|
+
return {};
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const createAccessLoggerMiddleware = function () {
|
|
105
|
+
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
106
|
+
const formatters = winston__namespace["default"].format.combine(winston__namespace["default"].format.timestamp(), ...(options.formatters ?? []), options.json ? winston__namespace["default"].format.json() : winston__namespace["default"].format.cli());
|
|
107
|
+
const skip = createAccessLogSkipper(options);
|
|
108
|
+
return expressWinston__default["default"].logger({
|
|
109
|
+
level: options.level ?? 'info',
|
|
110
|
+
transports: [new winston__namespace["default"].transports.Console()],
|
|
111
|
+
format: formatters,
|
|
112
|
+
meta: true,
|
|
113
|
+
expressFormat: true,
|
|
114
|
+
// Use default morgan access log formatting
|
|
115
|
+
colorize: !options.json,
|
|
116
|
+
skip,
|
|
117
|
+
dynamicMeta: mapRequestMetadata
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const createApplicationLogger = function () {
|
|
122
|
+
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
123
|
+
const formatters = winston__namespace.format.combine(...(options.formatters ?? []), options.json ? winston__namespace.format.json() : winston__namespace.format.combine(winston__namespace.format.cli(), winston__namespace.format.simple()));
|
|
124
|
+
return winston__namespace.createLogger({
|
|
125
|
+
level: options.level ?? 'info',
|
|
126
|
+
format: formatters,
|
|
127
|
+
transports: [new winston__namespace.transports.Console({
|
|
128
|
+
silent: Boolean(options.silent)
|
|
129
|
+
})]
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
function rewriteField(info, field) {
|
|
134
|
+
const fromFieldValue = getIn__default["default"](info, field.from);
|
|
135
|
+
const preserveFromField = field.preserveFromField ?? false;
|
|
136
|
+
if (fromFieldValue) {
|
|
137
|
+
if (!preserveFromField) {
|
|
138
|
+
unsetIn__default["default"](info, field.from);
|
|
139
|
+
}
|
|
140
|
+
setIn__default["default"](info, field.to, field.replaceValue ? field.replaceValue(fromFieldValue) : fromFieldValue);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function rewriteFieldsFormatter(options) {
|
|
144
|
+
return logform.format(info => {
|
|
145
|
+
var _context;
|
|
146
|
+
const clone = cloneDeep__default["default"](info);
|
|
147
|
+
_forEachInstanceProperty__default["default"](_context = options.fields).call(_context, field => {
|
|
148
|
+
rewriteField(clone, field);
|
|
149
|
+
});
|
|
150
|
+
return clone;
|
|
151
|
+
})(options);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
exports.createAccessLoggerMiddleware = createAccessLoggerMiddleware;
|
|
155
|
+
exports.createApplicationLogger = createApplicationLogger;
|
|
156
|
+
exports.rewriteFieldsFormatter = rewriteFieldsFormatter;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import expressWinston from 'express-winston';
|
|
2
|
+
import * as winston from 'winston';
|
|
3
|
+
import winston__default from 'winston';
|
|
4
|
+
import _defineProperty from '@babel/runtime-corejs3/helpers/esm/defineProperty';
|
|
5
|
+
import _slicedToArray from '@babel/runtime-corejs3/helpers/esm/slicedToArray';
|
|
6
|
+
import _Array$isArray from '@babel/runtime-corejs3/core-js-stable/array/is-array';
|
|
7
|
+
import _someInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/some';
|
|
8
|
+
import _Object$keys from '@babel/runtime-corejs3/core-js-stable/object/keys';
|
|
9
|
+
import _Object$getOwnPropertySymbols from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols';
|
|
10
|
+
import _filterInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/filter';
|
|
11
|
+
import _Object$getOwnPropertyDescriptor from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor';
|
|
12
|
+
import _forEachInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/for-each';
|
|
13
|
+
import _Object$getOwnPropertyDescriptors from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors';
|
|
14
|
+
import _Object$defineProperties from '@babel/runtime-corejs3/core-js-stable/object/define-properties';
|
|
15
|
+
import _Object$defineProperty from '@babel/runtime-corejs3/core-js-stable/object/define-property';
|
|
16
|
+
import cloneDeep from 'lodash/cloneDeep';
|
|
17
|
+
import getIn from 'lodash/get';
|
|
18
|
+
import setIn from 'lodash/set';
|
|
19
|
+
import unsetIn from 'lodash/unset';
|
|
20
|
+
import { format } from 'logform';
|
|
21
|
+
|
|
22
|
+
function ownKeys(e, r) { var t = _Object$keys(e); if (_Object$getOwnPropertySymbols) { var o = _Object$getOwnPropertySymbols(e); r && (o = _filterInstanceProperty(o).call(o, function (r) { return _Object$getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
23
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context2, _context3; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty(_context2 = ownKeys(Object(t), !0)).call(_context2, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(e, _Object$getOwnPropertyDescriptors(t)) : _forEachInstanceProperty(_context3 = ownKeys(Object(t))).call(_context3, function (r) { _Object$defineProperty(e, r, _Object$getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
24
|
+
const parseIps = request => {
|
|
25
|
+
const forwardedFor = request.headers['x-forwarded-for'];
|
|
26
|
+
if (!forwardedFor) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
const remoteAddresses = _Array$isArray(forwardedFor) ? forwardedFor : forwardedFor.split(',');
|
|
30
|
+
return remoteAddresses;
|
|
31
|
+
};
|
|
32
|
+
const createAccessLogSkipper = options => request => {
|
|
33
|
+
var _context;
|
|
34
|
+
if (Boolean(options.silent)) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
const hasMatchingIgnoreUrl = _someInstanceProperty(_context = options.ignoreUrls ?? []).call(_context, uriPathOrRegex => {
|
|
38
|
+
if (typeof uriPathOrRegex === 'string') {
|
|
39
|
+
return request.originalUrl === uriPathOrRegex;
|
|
40
|
+
}
|
|
41
|
+
return request.originalUrl.match(uriPathOrRegex);
|
|
42
|
+
});
|
|
43
|
+
return hasMatchingIgnoreUrl;
|
|
44
|
+
};
|
|
45
|
+
const mapRequestMetadata = request => {
|
|
46
|
+
try {
|
|
47
|
+
const remoteAddress = request.socket?.remoteAddress;
|
|
48
|
+
const proxyIps = parseIps(request);
|
|
49
|
+
const _proxyIps = _slicedToArray(proxyIps, 1),
|
|
50
|
+
clientIp = _proxyIps[0];
|
|
51
|
+
return _objectSpread({
|
|
52
|
+
clientIp: clientIp ?? remoteAddress,
|
|
53
|
+
proxyIps,
|
|
54
|
+
hostname: request.socket ? request.hostname : undefined
|
|
55
|
+
}, remoteAddress ? {
|
|
56
|
+
remoteAddress
|
|
57
|
+
} : {});
|
|
58
|
+
} catch (error) {
|
|
59
|
+
console.error(`Failed to parse request metadata`, error);
|
|
60
|
+
return {};
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const createAccessLoggerMiddleware = function () {
|
|
65
|
+
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
66
|
+
const formatters = winston__default.format.combine(winston__default.format.timestamp(), ...(options.formatters ?? []), options.json ? winston__default.format.json() : winston__default.format.cli());
|
|
67
|
+
const skip = createAccessLogSkipper(options);
|
|
68
|
+
return expressWinston.logger({
|
|
69
|
+
level: options.level ?? 'info',
|
|
70
|
+
transports: [new winston__default.transports.Console()],
|
|
71
|
+
format: formatters,
|
|
72
|
+
meta: true,
|
|
73
|
+
expressFormat: true,
|
|
74
|
+
// Use default morgan access log formatting
|
|
75
|
+
colorize: !options.json,
|
|
76
|
+
skip,
|
|
77
|
+
dynamicMeta: mapRequestMetadata
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const createApplicationLogger = function () {
|
|
82
|
+
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
83
|
+
const formatters = winston.format.combine(...(options.formatters ?? []), options.json ? winston.format.json() : winston.format.combine(winston.format.cli(), winston.format.simple()));
|
|
84
|
+
return winston.createLogger({
|
|
85
|
+
level: options.level ?? 'info',
|
|
86
|
+
format: formatters,
|
|
87
|
+
transports: [new winston.transports.Console({
|
|
88
|
+
silent: Boolean(options.silent)
|
|
89
|
+
})]
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
function rewriteField(info, field) {
|
|
94
|
+
const fromFieldValue = getIn(info, field.from);
|
|
95
|
+
const preserveFromField = field.preserveFromField ?? false;
|
|
96
|
+
if (fromFieldValue) {
|
|
97
|
+
if (!preserveFromField) {
|
|
98
|
+
unsetIn(info, field.from);
|
|
99
|
+
}
|
|
100
|
+
setIn(info, field.to, field.replaceValue ? field.replaceValue(fromFieldValue) : fromFieldValue);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function rewriteFieldsFormatter(options) {
|
|
104
|
+
return format(info => {
|
|
105
|
+
var _context;
|
|
106
|
+
const clone = cloneDeep(info);
|
|
107
|
+
_forEachInstanceProperty(_context = options.fields).call(_context, field => {
|
|
108
|
+
rewriteField(clone, field);
|
|
109
|
+
});
|
|
110
|
+
return clone;
|
|
111
|
+
})(options);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export { createAccessLoggerMiddleware, createApplicationLogger, rewriteFieldsFormatter };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type TRewriteField = {
|
|
2
|
+
from: string;
|
|
3
|
+
to: string;
|
|
4
|
+
replaceValue?: (value: unknown) => unknown;
|
|
5
|
+
preserveFromField?: boolean;
|
|
6
|
+
};
|
|
7
|
+
type TRewriteFieldsFormatterOption = {
|
|
8
|
+
fields: TRewriteField[];
|
|
9
|
+
};
|
|
10
|
+
declare function rewriteFieldsFormatter(options: TRewriteFieldsFormatterOption): import("logform").Format;
|
|
11
|
+
export default rewriteFieldsFormatter;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import expressWinston from 'express-winston';
|
|
2
|
+
import type { TAccessLoggerOptions } from "../types.js";
|
|
3
|
+
type TAccessLoggerMiddleware = ReturnType<typeof expressWinston.logger>;
|
|
4
|
+
declare const createAccessLoggerMiddleware: (options?: TAccessLoggerOptions) => TAccessLoggerMiddleware;
|
|
5
|
+
export default createAccessLoggerMiddleware;
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@commercetools-backend/loggers",
|
|
3
|
+
"version": "0.0.0-CRAFT-1791-20251006162610",
|
|
4
|
+
"description": "Opinionated JSON loggers for HTTP server applications",
|
|
5
|
+
"bugs": "https://github.com/commercetools/merchant-center-application-kit/issues",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/commercetools/merchant-center-application-kit.git",
|
|
9
|
+
"directory": "packages-backend/loggers"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://docs.commercetools.com/merchant-center-customizations",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"javascript",
|
|
14
|
+
"nodejs",
|
|
15
|
+
"express",
|
|
16
|
+
"logger",
|
|
17
|
+
"server",
|
|
18
|
+
"toolkit"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"main": "dist/commercetools-backend-loggers.cjs.js",
|
|
25
|
+
"module": "dist/commercetools-backend-loggers.esm.js",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"package.json",
|
|
29
|
+
"LICENSE",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@babel/runtime": "^7.22.15",
|
|
34
|
+
"@babel/runtime-corejs3": "^7.22.15",
|
|
35
|
+
"@types/lodash": "^4.14.198",
|
|
36
|
+
"@types/triple-beam": "1.3.5",
|
|
37
|
+
"express-winston": "4.2.0",
|
|
38
|
+
"fast-safe-stringify": "2.1.1",
|
|
39
|
+
"lodash": "4.17.21",
|
|
40
|
+
"logform": "2.7.0",
|
|
41
|
+
"triple-beam": "1.4.1",
|
|
42
|
+
"winston": "3.17.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@tsconfig/node22": "^22.0.0",
|
|
46
|
+
"@types/express": "^4.17.17",
|
|
47
|
+
"express": "^4.21.2",
|
|
48
|
+
"typescript": "^5.2.2"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": "18.x || 20.x || >=22.0.0"
|
|
52
|
+
}
|
|
53
|
+
}
|