From d2b5a3b40091e8326f46dbeb0c82a78572b8b3ab Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Sat, 4 Jun 2022 10:52:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=E5=9F=BA=E4=BA=8E=20bunyan=20?= =?UTF-8?q?=E7=9A=84=E6=97=A5=E5=BF=97=E5=B7=A5=E5=85=B7=EF=BC=8C=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E8=87=AA=E5=B7=B1=E5=AE=9A=E4=B9=89=E7=9A=84=20color?= =?UTF-8?q?=20console?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 83 ++++++++++++++++++++++++++-------------------------- package.json | 20 ++++++------- 2 files changed, 50 insertions(+), 53 deletions(-) diff --git a/index.js b/index.js index c2ff16d..d378029 100644 --- a/index.js +++ b/index.js @@ -1,42 +1,41 @@ -/* -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') -const PrettyStream = require('bunyan-pretty-colors') -const path = require('path') - -var prettyStdOut = new PrettyStream() -prettyStdOut.pipe(process.stdout) - -var logger = function (option) { - option = option || {} - return bunyan.createLogger({ - name: 'log', - src: false, - streams: [ - { - level: 'info', - stream: prettyStdOut, - }, - { - level: 'info', - type: 'rotating-file', - path: path.join(option.root || '_logstore/', '/', option.file || 'info.log'), - period: '1d', // daily rotation - count: 30, // keep 30 days - }, - ], - }) -} - -module.exports = logger // trace, debug, info, warn, error, fatal +const colors = require('colors') + +// HBuilder 内置环境的 console 不支持颜色。为了检查是否支持颜色,可测试 uniCloud 是否存在(不存在说明在自己的server环境里),或 require('supports-color'),相应的返回不同的 ccXXX 函数。 + +module.exports = typeof uniCloud === 'undefined' ? { + cclog(...args) { + console.log({timeiso: new Date().toJSON()}, ...args) + }, + ccinfo(...args) { + console.info({timeiso: new Date().toJSON()}, ...args) + }, + 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) + } + } +} diff --git a/package.json b/package.json index c50c54c..dd60f93 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,12 @@ { - "name": "base.logger", - "version": "0.1.0", - "private": true, + "name": "corend-cocon", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": {}, + "author": "", + "license": "ISC", "dependencies": { - "bunyan": "^1.8.12", - "bunyan-pretty-colors": "^0.1.7" - }, - "devDependencies": {}, - "scripts": { - "setup": "npm install" - }, - "author": "" + "colors": "^1.4.0" + } }