egg-logger
Advanced tools
Comparing version 2.9.1 to 3.0.0
@@ -0,1 +1,3 @@ | ||
import { AsyncLocalStorage } from 'async_hooks'; | ||
interface ILoggerLevel { | ||
@@ -29,2 +31,3 @@ ALL: number; | ||
concentrateError?: 'duplicate' | 'redirect' | 'ignore'; | ||
localStorage?: AsyncLocalStorage<any>; | ||
} | ||
@@ -31,0 +34,0 @@ |
'use strict'; | ||
const { performance } = require('perf_hooks'); | ||
const depd = require('depd')('egg-logger'); | ||
const { defaultContextPaddingMessage } = require('../utils'); | ||
@@ -9,3 +10,2 @@ /** | ||
class ContextLogger { | ||
/** | ||
@@ -19,25 +19,8 @@ * @class | ||
this._logger = logger; | ||
depd('EggContextLogger is deprecated, use the EggLogger directly'); | ||
} | ||
// sub class can override this getter | ||
get paddingMessage() { | ||
const ctx = this.ctx; | ||
// Auto record necessary request context infomation, e.g.: user id, request spend time | ||
// format: '[$userId/$ip/$traceId/$use_ms $method $url]' | ||
const userId = ctx.userId || '-'; | ||
const traceId = ctx.tracer && ctx.tracer.traceId || '-'; | ||
let use = 0; | ||
if (ctx.performanceStarttime) { | ||
use = Math.floor((performance.now() - ctx.performanceStarttime) * 1000) / 1000; | ||
} else if (ctx.starttime) { | ||
use = Date.now() - ctx.starttime; | ||
} | ||
return '[' + | ||
userId + '/' + | ||
ctx.ip + '/' + | ||
traceId + '/' + | ||
use + 'ms ' + | ||
ctx.method + ' ' + | ||
ctx.url + | ||
']'; | ||
return defaultContextPaddingMessage(this.ctx); | ||
} | ||
@@ -50,11 +33,11 @@ | ||
// logger.error()/warn()/info()/debug() | ||
[ 'error', 'warn', 'info', 'debug' ].forEach(level => { | ||
const LEVEL = level.toUpperCase(); | ||
ContextLogger.prototype[level] = function() { | ||
ContextLogger.prototype[level] = function(...args) { | ||
const meta = { | ||
formatter: contextFormatter, | ||
paddingMessage: this.paddingMessage, | ||
ctx: this.ctx, | ||
}; | ||
this._logger.log(LEVEL, arguments, meta); | ||
this._logger.log(LEVEL, args, meta); | ||
}; | ||
@@ -64,5 +47,1 @@ }); | ||
module.exports = ContextLogger; | ||
function contextFormatter(meta) { | ||
return meta.date + ' ' + meta.level + ' ' + meta.pid + ' ' + meta.paddingMessage + ' ' + meta.message; | ||
} |
@@ -29,2 +29,3 @@ 'use strict'; | ||
* - {String} [concentrateError] - whether write error logger to common-error.log, `duplicate` / `redirect` / `ignore` | ||
* - {AsyncLocalStorage} [localStorage] - AsyncLocalStorage instance to get current ctx | ||
*/ | ||
@@ -51,2 +52,3 @@ constructor(options) { | ||
eol: this.options.eol, | ||
localStorage: this.options.localStorage, | ||
}); | ||
@@ -64,2 +66,3 @@ this.set('file', fileTransport); | ||
eol: this.options.eol, | ||
localStorage: this.options.localStorage, | ||
}); | ||
@@ -74,2 +77,3 @@ this.set('jsonFile', jsonFileTransport); | ||
eol: this.options.eol, | ||
localStorage: this.options.localStorage, | ||
}); | ||
@@ -76,0 +80,0 @@ this.set('console', consoleTransport); |
@@ -8,3 +8,3 @@ 'use strict'; | ||
* | ||
* @method Logger#debug | ||
* @function Logger#debug | ||
* @param {String} msg - log message | ||
@@ -16,3 +16,3 @@ */ | ||
* Normal infomaction loging | ||
* @method Logger#info | ||
* @function Logger#info | ||
* @param {String} msg - log message | ||
@@ -24,3 +24,3 @@ */ | ||
* Warning information loging | ||
* @method Logger#warn | ||
* @function Logger#warn | ||
* @param {String} msg - log message | ||
@@ -32,3 +32,3 @@ */ | ||
* Error or exception loging | ||
* @method Logger#error | ||
* @function Logger#error | ||
* @param {String|Error} msg - log message or error instance | ||
@@ -35,0 +35,0 @@ */ |
@@ -7,3 +7,2 @@ 'use strict'; | ||
/** | ||
@@ -169,4 +168,4 @@ * Base class for all sub Logger class. | ||
const LEVEL = level.toUpperCase(); | ||
Logger.prototype[level] = function() { | ||
this.log(LEVEL, arguments); | ||
Logger.prototype[level] = function(...args) { | ||
this.log(LEVEL, args); | ||
}; | ||
@@ -173,0 +172,0 @@ }); |
@@ -6,3 +6,3 @@ 'use strict'; | ||
const assert = require('assert'); | ||
const mkdirp = require('mkdirp'); | ||
const { mkdirSync } = require('fs'); | ||
const utility = require('utility'); | ||
@@ -106,3 +106,3 @@ const depd = require('depd')('egg-logger'); | ||
_createStream() { | ||
mkdirp.sync(path.dirname(this.options.file)); | ||
mkdirSync(path.dirname(this.options.file), { recursive: true }); | ||
const stream = fs.createWriteStream(this.options.file, { flags: 'a' }); | ||
@@ -109,0 +109,0 @@ |
@@ -24,2 +24,3 @@ 'use strict'; | ||
* - {String} [eol = os.EOL] - end of line | ||
* - {AsyncLocalStorage} [localStorage] - AsyncLocalStorage instance to get current ctx | ||
*/ | ||
@@ -43,2 +44,3 @@ constructor(options) { | ||
eol: os.EOL, | ||
// localStorage: null, | ||
}; | ||
@@ -105,2 +107,8 @@ } | ||
log(level, args, meta) { | ||
if (!meta?.ctx && this.options.localStorage) { | ||
const ctx = this.options.localStorage.getStore(); | ||
if (ctx) { | ||
meta = { ...meta, ctx }; | ||
} | ||
} | ||
return utils.format(level, args, meta, this.options); | ||
@@ -107,0 +115,0 @@ } |
@@ -5,8 +5,9 @@ 'use strict'; | ||
const util = require('util'); | ||
const { performance } = require('perf_hooks'); | ||
const chalk = require('chalk'); | ||
const utility = require('utility'); | ||
const iconv = require('iconv-lite'); | ||
const levels = require('./level'); | ||
const circularJSON = require('circular-json-for-egg'); | ||
const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors'); | ||
const levels = require('./level'); | ||
@@ -34,5 +35,39 @@ const hostname = os.hostname(); | ||
// default format | ||
defaultContextPaddingMessage(ctx) { | ||
// Auto record necessary request context infomation, e.g.: user id, request spend time | ||
// format: '[$userId/$ip/$traceId/$use_ms $method $url]' | ||
const userId = ctx.userId || '-'; | ||
const traceId = ctx.tracer?.traceId || '-'; | ||
let use = 0; | ||
if (ctx.performanceStarttime) { | ||
use = Math.floor((performance.now() - ctx.performanceStarttime) * 1000) / 1000; | ||
} else if (ctx.starttime) { | ||
use = Date.now() - ctx.starttime; | ||
} | ||
return '[' + | ||
userId + '/' + | ||
ctx.ip + '/' + | ||
traceId + '/' + | ||
use + 'ms ' + | ||
ctx.method + ' ' + | ||
ctx.url + | ||
']'; | ||
}, | ||
// format with ctx and ctx.tracer | ||
// Auto record necessary request context infomation, e.g.: user id, request spend time | ||
// format: '[$userId/$ip/$traceId/$use_ms $method $url]' | ||
defaultFormatter(meta) { | ||
return meta.date + ' ' + meta.level + ' ' + meta.pid + ' ' + meta.message; | ||
let paddingMessage = ' '; | ||
// try to use the exists paddingMessage first | ||
if (meta.paddingMessage) { | ||
paddingMessage = ` ${meta.paddingMessage} `; | ||
} else { | ||
// gen the default ctx paddingMessage | ||
const ctx = meta.ctx; | ||
if (ctx) { | ||
paddingMessage = ` ${module.exports.defaultContextPaddingMessage(ctx)} `; | ||
} | ||
} | ||
return meta.date + ' ' + meta.level + ' ' + meta.pid + paddingMessage + meta.message; | ||
}, | ||
@@ -39,0 +74,0 @@ |
{ | ||
"name": "egg-logger", | ||
"version": "2.9.1", | ||
"version": "3.0.0", | ||
"description": "egg logger", | ||
"main": "index.js", | ||
"typings": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"lib", | ||
"index.d.ts" | ||
], | ||
"dependencies": { | ||
"chalk": "^2.4.1", | ||
"chalk": "^4.1.2", | ||
"circular-json-for-egg": "^1.0.0", | ||
"debug": "^2.6.9", | ||
"debug": "^4.3.4", | ||
"depd": "^2.0.0", | ||
"egg-errors": "^2.2.0", | ||
"iconv-lite": "^0.4.24", | ||
"mkdirp": "^0.5.1", | ||
"utility": "^1.15.0" | ||
"egg-errors": "^2.3.1", | ||
"iconv-lite": "^0.6.3", | ||
"utility": "^1.17.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^10.3.4", | ||
"autod": "^3.0.1", | ||
"@types/node": "^14.18.34", | ||
"beautify-benchmark": "^0.2.4", | ||
"benchmark": "^2.1.4", | ||
"coffee": "^5.2.1", | ||
"egg-bin": "^4.9.0", | ||
"egg-ci": "^1.18.0", | ||
"eslint": "^5.11.1", | ||
"eslint-config-egg": "^7.1.0", | ||
"coffee": "^5.5.0", | ||
"egg-bin": "^5.5.0", | ||
"egg-ci": "^2.2.0", | ||
"eslint": "^8.29.0", | ||
"eslint-config-egg": "^12.1.0", | ||
"git-contributor": "^1.0.10", | ||
"ko-sleep": "^1.0.3", | ||
"koa": "^1.6.2", | ||
"mm": "^2.4.1", | ||
"mz": "^2.7.0", | ||
"mz-modules": "^2.1.0", | ||
"rimraf": "^2.6.2", | ||
"should": "^13.2.3", | ||
"supertest": "^3.3.0", | ||
"ts-node": "^7.0.1", | ||
"typescript": "^2.8.3" | ||
"koa": "^2.14.1", | ||
"mm": "^3.2.1", | ||
"supertest": "^6.3.3", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.9.4" | ||
}, | ||
@@ -46,26 +45,16 @@ "repository": { | ||
"contributor": "git-contributor", | ||
"autod": "autod", | ||
"lint": "eslint .", | ||
"test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local", | ||
"test-local": "egg-bin test", | ||
"cov": "egg-bin cov", | ||
"ci": "npm run lint && egg-bin pkgfiles --check && npm run cov" | ||
"test": "npm run lint -- --fix && npm run test-local", | ||
"test-local": "egg-bin test --full-trace", | ||
"cov": "egg-bin cov --full-trace", | ||
"ci": "npm run lint && npm run cov" | ||
}, | ||
"typings": "index.d.ts", | ||
"engines": { | ||
"node": ">=8.5.0" | ||
"node": ">=14.17.0" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"lib", | ||
"index.d.ts" | ||
], | ||
"ci": { | ||
"version": "8, 10, 12, 14, 16, 18", | ||
"type": "github", | ||
"os": { | ||
"github": "linux" | ||
} | ||
"version": "14, 16, 18", | ||
"os": "linux, macos" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -120,4 +120,2 @@ # egg-logger | ||
## License | ||
@@ -124,0 +122,0 @@ |
61389
7
14
20
1298
8
+ Addedansi-styles@4.3.0(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddebug@4.3.5(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedms@2.1.2(transitive)
+ Addedsupports-color@7.2.0(transitive)
- Removedmkdirp@^0.5.1
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removeddebug@2.6.9(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedms@2.0.0(transitive)
- Removedsupports-color@5.5.0(transitive)
Updatedchalk@^4.1.2
Updateddebug@^4.3.4
Updatedegg-errors@^2.3.1
Updatediconv-lite@^0.6.3
Updatedutility@^1.17.0