diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 91904f0..f5d6393 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -13,4 +13,4 @@ jobs: uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest, macos-latest' - version: '14, 16, 18, 20, 22' + version: '14.18.0, 14, 16, 18, 20, 22' diff --git a/.gitignore b/.gitignore index ec9db9e..e200f1f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ tmp_file_* *.log .nyc_output .idea +package-lock.json diff --git a/README.md b/README.md index 244a6ff..6ced472 100644 --- a/README.md +++ b/README.md @@ -116,19 +116,12 @@ logger.info('info'); set environment NODE_CONSOLE_LOGGRE_LEVEL = 'INFO' | 'WARN' | 'ERROR' - ## License [MIT](LICENSE) - ## Contributors -|[
fengmk2](https://github.com/fengmk2)
|[
dead-horse](https://github.com/dead-horse)
|[
popomore](https://github.com/popomore)
|[
atian25](https://github.com/atian25)
|[
semantic-release-bot](https://github.com/semantic-release-bot)
|[
mansonchor](https://github.com/mansonchor)
| -| :---: | :---: | :---: | :---: | :---: | :---: | -|[
whxaxes](https://github.com/whxaxes)
|[
Jeff-Tian](https://github.com/Jeff-Tian)
|[
waitingsong](https://github.com/waitingsong)
|[
sjfkai](https://github.com/sjfkai)
|[
congyuandong](https://github.com/congyuandong)
|[
lix059](https://github.com/lix059)
| -[
killagu](https://github.com/killagu)
|[
duqingyu](https://github.com/duqingyu)
|[
AmazingCaddy](https://github.com/AmazingCaddy)
|[
linrf](https://github.com/linrf)
|[
XadillaX](https://github.com/XadillaX)
|[
luckydrq](https://github.com/luckydrq)
- -This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed May 08 2024 09:39:22 GMT+0800`. +[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-logger)](https://github.com/eggjs/egg-logger/graphs/contributors) - +Made with [contributors-img](https://contrib.rocks). diff --git a/README.zh-CN.md b/README.zh-CN.md index 886dfd3..ca59d2d 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -118,15 +118,8 @@ logger.info('info'); ## License [MIT](LICENSE) - - ## Contributors -|[
fengmk2](https://github.com/fengmk2)
|[
dead-horse](https://github.com/dead-horse)
|[
popomore](https://github.com/popomore)
|[
atian25](https://github.com/atian25)
|[
semantic-release-bot](https://github.com/semantic-release-bot)
|[
mansonchor](https://github.com/mansonchor)
| -| :---: | :---: | :---: | :---: | :---: | :---: | -|[
whxaxes](https://github.com/whxaxes)
|[
Jeff-Tian](https://github.com/Jeff-Tian)
|[
waitingsong](https://github.com/waitingsong)
|[
sjfkai](https://github.com/sjfkai)
|[
congyuandong](https://github.com/congyuandong)
|[
lix059](https://github.com/lix059)
| -[
killagu](https://github.com/killagu)
|[
duqingyu](https://github.com/duqingyu)
|[
AmazingCaddy](https://github.com/AmazingCaddy)
|[
linrf](https://github.com/linrf)
|[
XadillaX](https://github.com/XadillaX)
|[
luckydrq](https://github.com/luckydrq)
- -This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed May 08 2024 09:39:22 GMT+0800`. +[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-logger)](https://github.com/eggjs/egg-logger/graphs/contributors) - +Made with [contributors-img](https://contrib.rocks). diff --git a/lib/utils.js b/lib/utils.js index 8b0abaa..a36a75d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -11,7 +11,7 @@ const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors'); const levels = require('./level'); const hostname = os.hostname(); -const duartionRegexp = /([0-9]+ms)/g; +const durationRegexp = /([0-9]+ms)/g; // eslint-disable-next-line no-useless-escape const categoryRegexp = /(\[[\w\-_.:]+\])/g; const httpMethodRegexp = /(GET|POST|PUT|PATH|HEAD|DELETE) /g; @@ -88,7 +88,7 @@ module.exports = { return chalk.yellow(msg); } - msg = msg.replace(duartionRegexp, chalk.green('$1')); + msg = msg.replace(durationRegexp, chalk.green('$1')); msg = msg.replace(categoryRegexp, chalk.blue('$1')); msg = msg.replace(httpMethodRegexp, chalk.cyan('$1 ')); return msg; @@ -200,36 +200,50 @@ function errorToString(err, options, causeLength) { const maxCauseChainLength = options.maxCauseChainLength || 10; causeLength = causeLength || 0; - if (causeLength > maxCauseChainLength) return 'too long cause chain'; + if (causeLength > maxCauseChainLength) { + return 'too long cause chain'; + } + let errName = err.name || 'no_name'; if (err.name === 'Error' && typeof err.code === 'string') { - err.name = err.code + err.name; + errName = err.code + errName; } + let errMessage = err.message || 'no_message'; + if (err.host) { - err.message += ` (${err.host})`; + errMessage += ` (${err.host})`; } // name and stack could not be change on node 0.11+ const errStack = err.stack || 'no_stack'; const errProperties = Object.keys(err).map(key => inspect(key, err[key])).join('\n'); + let errorString = util.format('nodejs.%s: %s\n%s\n%s', + errName, + errMessage, + errStack.substring(errStack.indexOf('\n') + 1), + errProperties + ); + if (err.name === 'AggregateError' && err.errors) { + for (let i = 0; i < err.errors.length; i++) { + const subError = err.errors[i]; + const subErrorMsg = errorToString(subError, options, causeLength + 1); + errorString = util.format('%s\n[error-%d]:\n\n%s', + errorString, + i, + subErrorMsg + ); + } + } if (err.cause) { const causeMsg = errorToString(err.cause, options, causeLength + 1); - return util.format('nodejs.%s: %s\n%s\n%s\ncause:\n\n%s', - err.name, - err.message, - errStack.substring(errStack.indexOf('\n') + 1), - errProperties, + errorString = util.format('%s\ncause:\n\n%s', + errorString, causeMsg ); } - return util.format('nodejs.%s: %s\n%s\n%s', - err.name, - err.message, - errStack.substring(errStack.indexOf('\n') + 1), - errProperties - ); + return errorString; } function formatError(err, options, causeLength) { diff --git a/package.json b/package.json index e8840de..ea3fcb0 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "egg-bin": "6", "eslint": "^8.29.0", "eslint-config-egg": "^12.1.0", - "git-contributor": "^2.0.0", "koa": "^2.14.1", "mm": "^3.2.1", "supertest": "^6.3.3", diff --git a/test/lib/utils.test.js b/test/lib/utils.test.js index c8eca9d..2b3a31d 100644 --- a/test/lib/utils.test.js +++ b/test/lib/utils.test.js @@ -145,5 +145,42 @@ describe('test/lib/utils.test.js', () => { assert(msg.match(/pid: /)); assert(msg.match(/hostname: /)); }); + + it('should format AggregateError', () => { + // eslint-disable-next-line no-undef + const rootError = new AggregateError([ + new Error('error 1'), + new TypeError('error 2', { cause: new Error('error 2 cause error') }), + ]); + // nodejs.AggregateError: no_message + // at Context. (/github.com/eggjs/egg-logger/test/lib/utils.test.js:151:25) + // at process.processImmediate (node:internal/timers:491:21) + // + // [error-0]: + // + // nodejs.Error: error 1 + // at Context. (/github.com/eggjs/egg-logger/test/lib/utils.test.js:152:9) + // at process.processImmediate (node:internal/timers:491:21) + // + // [error-1]: + // + // nodejs.TypeError: error 2 + // at Context. (/github.com/eggjs/egg-logger/test/lib/utils.test.js:153:9) + // at process.processImmediate (node:internal/timers:491:21) + // + // pid: 71661 + // hostname: xxxx + + const msg = formatError(rootError); + // console.log(msg); + assert(msg.match(/nodejs.AggregateError: no_message/)); + assert(msg.match(/\[error-0]:/)); + assert(msg.match(/nodejs.Error: error 1/)); + assert(msg.match(/\[error-1]:/)); + assert(msg.match(/nodejs.TypeError: error 2/)); + assert(msg.match(/nodejs.Error: error 2 cause error/)); + assert(msg.match(/pid: /)); + assert(msg.match(/hostname: /)); + }); }); });