Skip to content

Commit

Permalink
feat(logger): new feature esTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
vanthome committed Jul 25, 2021
1 parent b008dde commit 682c118
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 47 deletions.
10 changes: 7 additions & 3 deletions packages/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ 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).

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",
Expand All @@ -34,6 +36,7 @@ the options hash:
"elasticsearch": {
...
}
}
```

The logger returns a Winston logger instance which has methods that correspond
Expand All @@ -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).

Expand Down
92 changes: 53 additions & 39 deletions packages/logger/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down
17 changes: 17 additions & 0 deletions packages/logger/src/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -46,6 +50,10 @@ export function createLogger(opts: RestoreLoggerOptions = {}) {
...opts
});

logger.on('error', (error) => {
console.error('Logger error', error);
});

return logger;
};

Expand Down

0 comments on commit 682c118

Please sign in to comment.