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" + } }