diff --git a/packages/logger/README.md b/packages/logger/README.md index f8499176..c361c5e7 100644 --- a/packages/logger/README.md +++ b/packages/logger/README.md @@ -12,7 +12,7 @@ Opinionated wrapper and configurator for the The following transports are supported: -- [Elasticsearch transport](https://github.com/vanthome/winston-elasticsearch) using a [specific transformer](https://github.com/restorecommerce/winston-elasticsearch-transformer) and ES data streams. +- [Elasticsearch transport](https://github.com/vanthome/winston-elasticsearch) using a local transformer function and ES data streams. - Console (Winston built-in transport). - File (Winston built-in transport). @@ -20,8 +20,10 @@ These transports can be added and configured with a corresponding property in the options hash: ```json - "loggerName": "somelogger", // Optional name - "sourcePointer": true, // Whether the source file and line where the log statement was issued should be logged [default: `false`] +{ + "loggerName": "somelogger", // Optional name + "sourcePointer": true, // Whether the source file and line where the log statement was issued should be logged [default: `false`] + "esTransformer": function() // "console": { "handleExceptions": false, "level": "silly", @@ -34,6 +36,7 @@ the options hash: "elasticsearch": { ... } +} ``` The logger returns a Winston logger instance which has methods that correspond @@ -54,6 +57,7 @@ In addition there is a generic `log()` function. - Source pointer logging -- show the source code file and line where the log statement was issued. - Implicit Request ID logging based on [cls-rtracer](https://github.com/puzpuzpuz/cls-rtracer). - Logger `AsyncLocalStorage` logger context to log implicit context information. +- Supports local transformer function for the `fields`. An example how to use the `AsyncLocalStorage` logger context can be found [here](test/test.js#L4-L11). diff --git a/packages/logger/package-lock.json b/packages/logger/package-lock.json index 80a4b720..570463a2 100644 --- a/packages/logger/package-lock.json +++ b/packages/logger/package-lock.json @@ -1,30 +1,30 @@ { "name": "@restorecommerce/logger", - "version": "0.9.1", + "version": "0.10.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@restorecommerce/logger", - "version": "0.9.0", + "version": "0.10.0", "license": "MIT", "dependencies": { "cls-hooked": "^4.2.2", "cls-rtracer": "^2.6.0", "source-map-support": "^0.5.19", "winston": "^3.3.3", - "winston-elasticsearch": "^0.15.7" + "winston-elasticsearch": "^0.15.8" }, "devDependencies": { "@types/jest": "^26.0.24", - "@types/node": "^16.3.2", + "@types/node": "^16.4.2", "@types/should": "^13.0.0", "coveralls": "^3.1.1", "jest": "^27.0.6", "npm-run-all": "^4.1.5", "nyc": "^15.1.0", "rimraf": "^3.0.2", - "ts-jest": "^27.0.3", + "ts-jest": "^27.0.4", "tslint": "^6.1.3", "typescript": "^4.3.5" }, @@ -1713,9 +1713,9 @@ } }, "node_modules/@types/node": { - "version": "16.3.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-16.3.2.tgz", - "integrity": "sha512-jJs9ErFLP403I+hMLGnqDRWT0RYKSvArxuBVh2veudHV7ifEC1WAmjJADacZ7mRbA2nWgHtn8xyECMAot0SkAw==", + "version": "16.4.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.4.2.tgz", + "integrity": "sha512-vxyhOzFCm+jC/T5KugbVsYy1DbQM0h3NCFUrVbu0+pYa/nr+heeucpqxpa8j4pUmIGLPYzboY9zIdOF0niFAjQ==", "dev": true }, "node_modules/@types/normalize-package-data": { @@ -2705,14 +2705,14 @@ } }, "node_modules/dayjs": { - "version": "1.10.5", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.5.tgz", - "integrity": "sha512-BUFis41ikLz+65iH6LHQCDm4YPMj5r1YFLdupPIyM4SGcXMmtiLQ7U37i+hGS8urIuqe7I/ou3IS1jVc4nbN4g==" + "version": "1.10.6", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.6.tgz", + "integrity": "sha512-AztC/IOW4L1Q41A86phW5Thhcrco3xuAA+YX/BLpLWWjRcTj5TOt/QImBLmCKlrF7u7k47arTnOyL6GnbG8Hvw==" }, "node_modules/debug": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", - "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", + "integrity": "sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw==", "dependencies": { "ms": "2.1.2" }, @@ -9465,9 +9465,9 @@ "integrity": "sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw==" }, "node_modules/ts-jest": { - "version": "27.0.3", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-27.0.3.tgz", - "integrity": "sha512-U5rdMjnYam9Ucw+h0QvtNDbc5+88nxt7tbIvqaZUhFrfG4+SkWhMXjejCLVGcpILTPuV+H3W/GZDZrnZFpPeXw==", + "version": "27.0.4", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-27.0.4.tgz", + "integrity": "sha512-c4E1ECy9Xz2WGfTMyHbSaArlIva7Wi2p43QOMmCqjSSjHP06KXv+aT+eSY+yZMuqsMi3k7pyGsGj2q5oSl5WfQ==", "dev": true, "dependencies": { "bs-logger": "0.x", @@ -9488,8 +9488,22 @@ "node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" }, "peerDependencies": { + "@babel/core": ">=7.0.0-beta.0 <8", + "@types/jest": "^26.0.0", + "babel-jest": ">=27.0.0 <28", "jest": "^27.0.0", "typescript": ">=3.8 <5.0" + }, + "peerDependenciesMeta": { + "@babel/core": { + "optional": true + }, + "@types/jest": { + "optional": true + }, + "babel-jest": { + "optional": true + } } }, "node_modules/ts-jest/node_modules/mkdirp": { @@ -9863,13 +9877,13 @@ } }, "node_modules/winston-elasticsearch": { - "version": "0.15.7", - "resolved": "https://registry.npmjs.org/winston-elasticsearch/-/winston-elasticsearch-0.15.7.tgz", - "integrity": "sha512-q/uyumPxbhBH9cE85PmYHCRaxngxuBeJb55LpJqIvXu4ix5Fq7xOUBfMP4tgCrHMvg8wqZCQY+JxIwwSHCK2Pg==", + "version": "0.15.8", + "resolved": "https://registry.npmjs.org/winston-elasticsearch/-/winston-elasticsearch-0.15.8.tgz", + "integrity": "sha512-pKO9nr7OsXvDpNI8p5FgWaI7paqnRiRKo97WFm0fHWlSQrRxMP79B2m2bJe5lJjcJ6ChYUT80lhILpzMXA0KGg==", "dependencies": { "@elastic/elasticsearch": "^7.13.0", - "dayjs": "^1.10.5", - "debug": "^4.3.1", + "dayjs": "^1.10.6", + "debug": "^4.3.2", "lodash.defaults": "^4.2.0", "lodash.omit": "^4.5.0", "promise": "^8.1.0", @@ -11446,9 +11460,9 @@ } }, "@types/node": { - "version": "16.3.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-16.3.2.tgz", - "integrity": "sha512-jJs9ErFLP403I+hMLGnqDRWT0RYKSvArxuBVh2veudHV7ifEC1WAmjJADacZ7mRbA2nWgHtn8xyECMAot0SkAw==", + "version": "16.4.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.4.2.tgz", + "integrity": "sha512-vxyhOzFCm+jC/T5KugbVsYy1DbQM0h3NCFUrVbu0+pYa/nr+heeucpqxpa8j4pUmIGLPYzboY9zIdOF0niFAjQ==", "dev": true }, "@types/normalize-package-data": { @@ -12279,14 +12293,14 @@ } }, "dayjs": { - "version": "1.10.5", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.5.tgz", - "integrity": "sha512-BUFis41ikLz+65iH6LHQCDm4YPMj5r1YFLdupPIyM4SGcXMmtiLQ7U37i+hGS8urIuqe7I/ou3IS1jVc4nbN4g==" + "version": "1.10.6", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.6.tgz", + "integrity": "sha512-AztC/IOW4L1Q41A86phW5Thhcrco3xuAA+YX/BLpLWWjRcTj5TOt/QImBLmCKlrF7u7k47arTnOyL6GnbG8Hvw==" }, "debug": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", - "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", + "integrity": "sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw==", "requires": { "ms": "2.1.2" } @@ -17576,9 +17590,9 @@ "integrity": "sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw==" }, "ts-jest": { - "version": "27.0.3", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-27.0.3.tgz", - "integrity": "sha512-U5rdMjnYam9Ucw+h0QvtNDbc5+88nxt7tbIvqaZUhFrfG4+SkWhMXjejCLVGcpILTPuV+H3W/GZDZrnZFpPeXw==", + "version": "27.0.4", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-27.0.4.tgz", + "integrity": "sha512-c4E1ECy9Xz2WGfTMyHbSaArlIva7Wi2p43QOMmCqjSSjHP06KXv+aT+eSY+yZMuqsMi3k7pyGsGj2q5oSl5WfQ==", "dev": true, "requires": { "bs-logger": "0.x", @@ -17894,13 +17908,13 @@ } }, "winston-elasticsearch": { - "version": "0.15.7", - "resolved": "https://registry.npmjs.org/winston-elasticsearch/-/winston-elasticsearch-0.15.7.tgz", - "integrity": "sha512-q/uyumPxbhBH9cE85PmYHCRaxngxuBeJb55LpJqIvXu4ix5Fq7xOUBfMP4tgCrHMvg8wqZCQY+JxIwwSHCK2Pg==", + "version": "0.15.8", + "resolved": "https://registry.npmjs.org/winston-elasticsearch/-/winston-elasticsearch-0.15.8.tgz", + "integrity": "sha512-pKO9nr7OsXvDpNI8p5FgWaI7paqnRiRKo97WFm0fHWlSQrRxMP79B2m2bJe5lJjcJ6ChYUT80lhILpzMXA0KGg==", "requires": { "@elastic/elasticsearch": "^7.13.0", - "dayjs": "^1.10.5", - "debug": "^4.3.1", + "dayjs": "^1.10.6", + "debug": "^4.3.2", "elastic-apm-node": "^3.16.0", "lodash.defaults": "^4.2.0", "lodash.omit": "^4.5.0", diff --git a/packages/logger/package.json b/packages/logger/package.json index 01f67e16..8e2b1355 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -1,6 +1,6 @@ { "name": "@restorecommerce/logger", - "version": "0.9.1", + "version": "0.10.0", "description": "Opinionated wrapper and configurator for the winston logging toolkit", "main": "dist/index.js", "typings": "dist/index.d.ts", @@ -20,18 +20,18 @@ "cls-rtracer": "^2.6.0", "source-map-support": "^0.5.19", "winston": "^3.3.3", - "winston-elasticsearch": "^0.15.7" + "winston-elasticsearch": "^0.15.8" }, "devDependencies": { "@types/jest": "^26.0.24", - "@types/node": "^16.3.2", + "@types/node": "^16.4.2", "@types/should": "^13.0.0", "coveralls": "^3.1.1", "jest": "^27.0.6", "npm-run-all": "^4.1.5", "nyc": "^15.1.0", "rimraf": "^3.0.2", - "ts-jest": "^27.0.3", + "ts-jest": "^27.0.4", "tslint": "^6.1.3", "typescript": "^4.3.5" }, diff --git a/packages/logger/src/elasticsearch.ts b/packages/logger/src/elasticsearch.ts index c7600d00..46d76f54 100644 --- a/packages/logger/src/elasticsearch.ts +++ b/packages/logger/src/elasticsearch.ts @@ -49,12 +49,29 @@ function createTransformer(opts: RestoreLoggerElasticsearchTransportOptions) { if (typeof transformed.fields !== 'object') { transformed.fields = { 0: transformed.fields }; } + + if (opts.esTransformer && typeof opts.esTransformer === 'function') { + opts.esTransformer(transformed); + } + return transformed; }; } +// Fields transformer to convert all values into strings +// function toString(o: any) { +// Object.keys(o).forEach(k => { +// if (typeof o[k] === 'object') { +// return toString(o[k]); +// } +// o[k] = '' + o[k]; +// }); +// return o; +// } + export interface RestoreLoggerElasticsearchTransportOptions extends ElasticsearchTransportOptions { sourcePointer?: any; + esTransformer?: Function; } export function createElasticSearchTransport(opts: RestoreLoggerElasticsearchTransportOptions) { diff --git a/packages/logger/src/index.ts b/packages/logger/src/index.ts index 2f66060c..240ffcfe 100644 --- a/packages/logger/src/index.ts +++ b/packages/logger/src/index.ts @@ -35,7 +35,11 @@ export function createLogger(opts: RestoreLoggerOptions = {}) { } if (opts.elasticsearch) { opts.elasticsearch.dataStream = true; - transports.push(createElasticSearchTransport({ ...opts.elasticsearch, sourcePointer: opts.sourcePointer })); + const esTransport = createElasticSearchTransport({ ...opts.elasticsearch, sourcePointer: opts.sourcePointer }); + esTransport.on('error', (error) => { + console.error('Elasticsearch indexing error', error); + }); + transports.push(esTransport); } if (transports.length === 0) { transports.push(createConsoleTransport()); @@ -46,6 +50,10 @@ export function createLogger(opts: RestoreLoggerOptions = {}) { ...opts }); + logger.on('error', (error) => { + console.error('Logger error', error); + }); + return logger; };