You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// When provided nothing assume the empty stringif(args.length===0){self.log(level,'');returnself;}
一个参数逻辑:
// Optimize the hot-path which is the single object.if(args.length===1){const[msg]=args;constinfo=msg&&msg.message&&msg||{message: msg};info.level=info[LEVEL]=level;self._addDefaultMeta(info);self.write(info);return(this||logger);}
二个及以上参数逻辑:
// Otherwise build argument list which could potentially conform to either:// 1. v3 API: log(obj)// 2. v1/v2 API: log(level, msg, ... [string interpolate], [{metadata}], [callback])returnself.log(level, ...args);
{"name":"Error","message":"Error","stack":"Error\n at Object../src/index.js (/Users/nonocast/Develop/hello/hello-winston/test1/dist/bundle.js:126:13)\n at __webpack_require__ (/Users/nonocast/Develop/hello/hello-winston/test1/dist/bundle.js:20:30)\n at /Users/nonocast/Develop/hello/hello-winston/test1/dist/bundle.js:84:18\n at Object.<anonymous> (/Users/nonocast/Develop/hello/hello-winston/test1/dist/bundle.js:87:10)\n at Module._compile (internal/modules/cjs/loader.js:1137:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)\n at Module.load (internal/modules/cjs/loader.js:985:32)\n at Function.Module._load (internal/modules/cjs/loader.js:878:14)\n at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)\n at internal/main/run_main_module.js:17:47"}
{"level":"info","name":"Error","message":"user not found","stack":"Error: user not found\n at Object../src/index.js (/Users/nonocast/Develop/hello/hello-winston/test1/dist/bundle.js:127:13)\n at __webpack_require__ (/Users/nonocast/Develop/hello/hello-winston/test1/dist/bundle.js:20:30)\n at /Users/nonocast/Develop/hello/hello-winston/test1/dist/bundle.js:84:18\n at Object.<anonymous> (/Users/nonocast/Develop/hello/hello-winston/test1/dist/bundle.js:87:10)\n at Module._compile (internal/modules/cjs/loader.js:1137:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)\n at Module.load (internal/modules/cjs/loader.js:985:32)\n at Function.Module._load (internal/modules/cjs/loader.js:878:14)\n at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)\n at internal/main/run_main_module.js:17:47"}
目录
基本逻辑
使用方式
javascript本身的弱类型带来的灵活和不确定性相辅相成。最简单的方式直接看quick start
标准调用方法:
具体来说:
无参数逻辑:
一个参数逻辑:
二个及以上参数逻辑:
所以一般来说,禁用splat, 然后常规记录信息的方式:
如果是error,
但问题是logger.error(error)有bug,所以需要一个format进行修复,将Error提前转换为plain object。
所以需要通过format fix掉这个bug
输出:
Error有没有message都会发生不一样的情况,所以说,fixerror也只是权宜,还是在log之前就将Error转换为string或者plain object再继续,否则很麻烦。
create-logger.js中,
也就是说传入没有message的Error,winston就会将其置为info.message,而如果是有message则会将Error误认为是info object。winston通过format.error来处理error问题,
此时message会采用error的message, 然后加上stack,完美。
如果有显示message,则最终message会是error: null reference。
其他
output
如果每个unit需要对自己的日志打上tag,则通过child就可以很方便实现:
output:
如果日志根据情况不同需要写入不同的transport,形式或者文件,那么应该采用多个logger,通过container save&load。而如果是打不同tag,则通过meta实现即可。
参考
The text was updated successfully, but these errors were encountered: