把基于 bunyan 的日志工具,改为自己定义的 color console
This commit is contained in:
parent
81a68903bb
commit
d2b5a3b400
73
index.js
73
index.js
@ -1,42 +1,41 @@
|
|||||||
/*
|
const colors = require('colors')
|
||||||
var colors = require('colors') // require后,字符串被添加了一系列方法: str.white, str.inverse, ...
|
|
||||||
// colors.styles: bold,italic,underline,inverse,yellow,cyan,white,magenta,green,red,grey,blue,rainbow,zebra,random
|
|
||||||
// 自定义的 themes:
|
|
||||||
colors.setTheme({
|
|
||||||
logprompt: 'inverse',
|
|
||||||
logok:'green',
|
|
||||||
logerror: 'red',
|
|
||||||
logwarn: 'magenta',
|
|
||||||
logtitle: 'cyan'
|
|
||||||
})
|
|
||||||
*/
|
|
||||||
|
|
||||||
const bunyan = require('bunyan')
|
// HBuilder 内置环境的 console 不支持颜色。为了检查是否支持颜色,可测试 uniCloud 是否存在(不存在说明在自己的server环境里),或 require('supports-color'),相应的返回不同的 ccXXX 函数。
|
||||||
const PrettyStream = require('bunyan-pretty-colors')
|
|
||||||
const path = require('path')
|
|
||||||
|
|
||||||
var prettyStdOut = new PrettyStream()
|
module.exports = typeof uniCloud === 'undefined' ? {
|
||||||
prettyStdOut.pipe(process.stdout)
|
cclog(...args) {
|
||||||
|
console.log({timeiso: new Date().toJSON()}, ...args)
|
||||||
var logger = function (option) {
|
|
||||||
option = option || {}
|
|
||||||
return bunyan.createLogger({
|
|
||||||
name: 'log',
|
|
||||||
src: false,
|
|
||||||
streams: [
|
|
||||||
{
|
|
||||||
level: 'info',
|
|
||||||
stream: prettyStdOut,
|
|
||||||
},
|
},
|
||||||
{
|
ccinfo(...args) {
|
||||||
level: 'info',
|
console.info({timeiso: new Date().toJSON()}, ...args)
|
||||||
type: 'rotating-file',
|
|
||||||
path: path.join(option.root || '_logstore/', '/', option.file || 'info.log'),
|
|
||||||
period: '1d', // daily rotation
|
|
||||||
count: 30, // keep 30 days
|
|
||||||
},
|
},
|
||||||
],
|
ccerror(...args) { // console.error will appear in pm2's error log
|
||||||
})
|
console.error({timeiso: new Date().toJSON()}, ...args)
|
||||||
|
},
|
||||||
|
ccwarn(...args) { // console.warn will appear in pm2's error log
|
||||||
|
console.warn({timeiso: new Date().toJSON()}, ...args)
|
||||||
|
},
|
||||||
|
ccdebug(...args) {
|
||||||
|
if ('development'===process.env.NODE_ENV) { // 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
|
||||||
|
console.log({timeiso: new Date().toJSON()}, ...args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} : {
|
||||||
|
cclog(...args) {
|
||||||
|
console.log(colors.blue({timeiso: new Date().toJSON()}), ...args)
|
||||||
|
},
|
||||||
|
ccinfo(...args) {
|
||||||
|
console.info(colors.green({timeiso: new Date().toJSON()}), ...args)
|
||||||
|
},
|
||||||
|
ccerror(...args) { // console.error will appear in pm2's error log
|
||||||
|
console.error(colors.red({timeiso: new Date().toJSON()}), ...args)
|
||||||
|
},
|
||||||
|
ccwarn(...args) { // console.warn will appear in pm2's error log
|
||||||
|
console.warn(colors.yellow({timeiso: new Date().toJSON()}), ...args)
|
||||||
|
},
|
||||||
|
ccdebug(...args) {
|
||||||
|
if ('development'===process.env.NODE_ENV) { // 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
|
||||||
|
console.log(colors.rainbow({timeiso: new Date().toJSON()}), ...args)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = logger // trace, debug, info, warn, error, fatal
|
|
||||||
|
20
package.json
20
package.json
@ -1,14 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "base.logger",
|
"name": "corend-cocon",
|
||||||
"version": "0.1.0",
|
"version": "1.0.0",
|
||||||
"private": true,
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bunyan": "^1.8.12",
|
"colors": "^1.4.0"
|
||||||
"bunyan-pretty-colors": "^0.1.7"
|
}
|
||||||
},
|
|
||||||
"devDependencies": {},
|
|
||||||
"scripts": {
|
|
||||||
"setup": "npm install"
|
|
||||||
},
|
|
||||||
"author": ""
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user