Skip to content

Commit

Permalink
Fix printing erros with context (#2)
Browse files Browse the repository at this point in the history
* build: upgrade dependencies

* fix: handle case with manually entered error with additional context
  • Loading branch information
jshemfrid authored Oct 7, 2021
1 parent 94e32e0 commit 0ef72d3
Show file tree
Hide file tree
Showing 4 changed files with 5,387 additions and 5,315 deletions.
132 changes: 66 additions & 66 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
{
"name": "nest-logging-transport",
"version": "0.0.6",
"description": "Custom NestJS logger allowing you to attach custom transports",
"main": "dist/main.js",
"types": "dist/main.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/DevAndDeliver/nest-logging-transport.git"
},
"author": "kkm <[email protected]>",
"license": "MIT",
"scripts": {
"prebuild": "yarn clean",
"clean": "rimraf coverage build tmp dist",
"build": "ttsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix --color",
"lint:diagnostics": "ttsc -p tsconfig.diagnostics.json",
"test": "jest --passWithNoTests",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./jest.config.e2e.ts",
"test:e2e:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/jest/bin/jest --config ./jest.config.e2e.ts --runInBand",
"release": "./node_modules/.bin/standard-version --no-verify",
"preview": "node -r tsconfig-paths/register -r ts-node/register scripts/preview.ts",
"release:major": "yarn release --release-as major",
"release:minor": "yarn release --release-as minor",
"release:patch": "yarn release --release-as patch"
},
"devDependencies": {
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@nestjs/common": "^8.0.6",
"@nestjs/core": "^8.0.6",
"@nestjs/platform-express": "^8.0.6",
"@types/jest": "^27.0.1",
"@types/node": "16.9.0",
"@typescript-eslint/eslint-plugin": "^4.31.0",
"@typescript-eslint/parser": "^4.31.0",
"conventional-recommended-bump": "^6.1.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-jest": "^24.4.0",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^4.3.8",
"jest": "^27.1.1",
"prettier": "^2.3.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.3.0",
"standard-version": "^9.3.1",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"tsconfig-paths": "^3.11.0",
"tsd": "^0.17.0",
"ttypescript": "^1.5.12",
"typescript": "^4.4.2",
"typescript-transform-paths": "^3.3.1"
},
"peerDependencies": {
"@nestjs/common": "^8.0.6"
},
"dependencies": {
"colors": "^1.4.0"
}
}
{
"name": "nest-logging-transport",
"version": "0.0.6",
"description": "Custom NestJS logger allowing you to attach custom transports",
"main": "dist/main.js",
"types": "dist/main.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/DevAndDeliver/nest-logging-transport.git"
},
"author": "kkm <[email protected]>",
"license": "MIT",
"scripts": {
"prebuild": "yarn clean",
"clean": "rimraf coverage build tmp dist",
"build": "ttsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix --color",
"lint:diagnostics": "ttsc -p tsconfig.diagnostics.json",
"test": "jest --passWithNoTests",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./jest.config.e2e.ts",
"test:e2e:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/jest/bin/jest --config ./jest.config.e2e.ts --runInBand",
"release": "./node_modules/.bin/standard-version --no-verify",
"preview": "node -r tsconfig-paths/register -r ts-node/register scripts/preview.ts",
"release:major": "yarn release --release-as major",
"release:minor": "yarn release --release-as minor",
"release:patch": "yarn release --release-as patch"
},
"devDependencies": {
"@commitlint/cli": "^13.2.0",
"@commitlint/config-conventional": "^13.2.0",
"@nestjs/common": "^8.0.11",
"@nestjs/core": "^8.0.11",
"@nestjs/platform-express": "^8.0.11",
"@types/jest": "^27.0.2",
"@types/node": "16.10.3",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"conventional-recommended-bump": "^6.1.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-jest": "^24.5.2",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^4.3.8",
"jest": "^27.2.4",
"prettier": "^2.3.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.4.0",
"standard-version": "^9.3.1",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"tsconfig-paths": "^3.11.0",
"tsd": "^0.17.0",
"ttypescript": "^1.5.12",
"typescript": "^4.4.3",
"typescript-transform-paths": "^3.3.1"
},
"peerDependencies": {
"@nestjs/common": "^8.0.6"
},
"dependencies": {
"colors": "^1.4.0"
}
}
13 changes: 11 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,19 @@ export class NestTransportLogger implements LoggerService {
}

if (args.length === 2) {
return { messages: [message], baseContext: args[args.length - 1], stacktrace: args[0] };
return { messages: [message], baseContext: args[1], stacktrace: args[0] };
}

// args.length > 2
if (args.length === 3) {
return {
messages: [message],
baseContext: args[2],
additionalContext: args[1],
stacktrace: args[0],
};
}

// args.length > 3
return {
messages: [message].concat(args.slice(1, args.length - 1)),
baseContext: args[args.length - 1],
Expand Down
15 changes: 15 additions & 0 deletions test/logger.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ describe('Logger (e2e)', () => {
type: 'error',
});
});

it('should properly parse error with custom message, stacktrace and context', () => {
const err = new Error();
const additionalCtx = 'additionalContext';
mockedService.loggerWithContext.error(err.name, err.stack, additionalCtx);

expect(eventListener).toBeCalledWith({
additionalContext: additionalCtx,
message: err.name,
baseContext: MockedService.name,
stacktrace: err.stack,
timestamp: mockedDate,
type: 'error',
});
});
});

const eventListener = jest.fn();
Expand Down
Loading

0 comments on commit 0ef72d3

Please sign in to comment.