From d5ee048b3ef6dd5dbcfe1384e275b8bc41b1425d Mon Sep 17 00:00:00 2001 From: Yuga Sun Date: Thu, 2 Nov 2017 08:54:31 -0500 Subject: [PATCH] feat: add logger system (#40) and close #26 * feat: fix some eslint bug * feat: add logger system feature close #26 * feat: divide log into different types * fix: errors log write bug * fix: remove unuse console.log * feat: add pm2 support * feat: make logger type configurable * fix: remove unuse test code --- .gitignore | 1 + README.md | 26 ++++++++++- env | 3 ++ package.json | 4 +- src/app.js | 7 +-- src/github.js | 16 +++++++ src/logger.js | 62 +++++++++++++++++++++++++ src/modules/releases/autoReleaseNote.js | 7 ++- src/utils.js | 6 --- 9 files changed, 119 insertions(+), 13 deletions(-) create mode 100644 src/logger.js diff --git a/.gitignore b/.gitignore index ecbf6c1..abbb9c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Logs logs +log *.log npm-debug.log* yarn-debug.log* diff --git a/README.md b/README.md index 1396279..452f0f4 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ github 机器人:在服务端上启动一个基于 [koajs](http://koajs.com/) 的 http server ,建立一些项目的规范(如 issue 格式、 pull request 格式、配置一些指定 label 根据的 owner 、统一 git commit log 格式等),基于 [github webhooks](https://developer.github.com/webhooks/) 和 [github api](https://developer.github.com/v3/) 让机器人(通常是一个单独的帐号,如 [@jiandansousuo-bot](https://github.com/jiandansousuo-bot) )自动处理一些事情,从而达到快速响应、自动化、解放人力的效果。 [![Build Status](https://travis-ci.org/xuexb/github-bot.svg?branch=master)](https://travis-ci.org/xuexb/github-bot) +[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) ## 声明 @@ -85,15 +86,36 @@ https://github.com/用户名/项目名/settings/hooks/new - trigger: Send me everything. - Secret: xxx (_需要在 .env 里配置_) -### 3. run server +### 3. 开发运行 -``` +```bash npm install cp env .env vim .env npm start ``` +### 4. 部署 + +本项目使用 [pm2](https://github.com/Unitech/pm2) 进行服务管理,发布前请先全局安装 [pm2](https://github.com/Unitech/pm2) + +```bash +npm install pm2 -g +npm run deploy +``` + +后台启动该服务后,可以通过 `pm2 ls` 来查看服务名称为 `github-bot` 的运行状态。具体 [pm2](https://github.com/Unitech/pm2) 使用,请访问:https://github.com/Unitech/pm2 + +### 5. 日志系统说明 + +本系统 `logger` 服务基于 [log4js](https://github.com/log4js-node/log4js-node)。 +在根目录的 `.env` 文件中有个参数 `LOG_TYPE` 默认为 `console`,参数值说明: + +``` +console - 通过 console 输出log。 +file - 将所有相关log输出到更根目录的 `log` 文件夹中。 +``` + ## contributors > [用户贡献指南](.github/CONTRIBUTING.md) diff --git a/env b/env index e88ab5c..36ffed9 100755 --- a/env +++ b/env @@ -5,3 +5,6 @@ GITHUB_TOKEN=token # Webhook secret token, see https://developer.github.com/webhooks/securing/ GITHUB_SECRET_TOKEN=secret + +# Logger type: default is console, if you want write log to file, set 'file' +LOG_TYPE=console diff --git a/package.json b/package.json index 3c336c1..16629c4 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "src/app.js", "scripts": { "start": "NODE_ENV=development node src/app", - "lint": "eslint src/**/* --quiet", + "lint": "eslint src/**/*.js --quiet", + "deploy": "pm2 start src/app.js --name=github-bot", "precommit": "npm run lint", "commitmsg": "validate-commit-msg" }, @@ -26,6 +27,7 @@ "github": "^11.0.0", "koa": "^2.3.0", "koa-bodyparser": "^4.2.0", + "log4js": "^2.3.10", "require-dir": "^0.3.2", "string-template": "^1.0.0" }, diff --git a/src/app.js b/src/app.js index 6957cd6..0e87183 100755 --- a/src/app.js +++ b/src/app.js @@ -15,6 +15,7 @@ const pullRequestActions = requireDir('./modules/pull_request') const releasesActions = requireDir('./modules/releases') const app = new Koa() const githubEvent = new EventEmitter() +const { appLog, accessLog } = require('./logger') app.use(bodyParser()) @@ -28,7 +29,7 @@ app.use(ctx => { eventName += `_${action}` } - console.log(`receive event: ${eventName}`) + accessLog.info(`receive event: ${eventName}`) githubEvent.emit(eventName, { repo: payload.repository.name, @@ -44,9 +45,9 @@ app.use(ctx => { const actions = Object.assign({}, issueActions, pullRequestActions, releasesActions) Object.keys(actions).forEach((key) => { actions[key](githubEvent.on.bind(githubEvent)) - console.log(`bind ${key} success!`) + appLog.info(`bind ${key} success!`) }) const port = 8000 app.listen(port) -console.log(`Listening on http://0.0.0.0:${port}`) +appLog.info('Listening on http://0.0.0.0:', port) diff --git a/src/github.js b/src/github.js index 0f32b2d..61be693 100755 --- a/src/github.js +++ b/src/github.js @@ -5,6 +5,7 @@ const GitHub = require('github') const { toArray } = require('./utils') +const { appLog } = require('./logger') const github = new GitHub({ debug: process.env.NODE_ENV === 'development' @@ -36,6 +37,7 @@ module.exports = { }) return res.data.map(v => v.name).indexOf(label) > -1 } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -60,6 +62,7 @@ module.exports = { }) return res.data.map(v => v.name).indexOf(label) > -1 } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -85,6 +88,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -110,6 +114,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -134,6 +139,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -159,6 +165,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -184,6 +191,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -209,6 +217,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -234,6 +243,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -258,6 +268,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -290,6 +301,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -313,6 +325,7 @@ module.exports = { }) return res.data } catch (e) { + appLog.error(new Error(e)) return null } }, @@ -340,6 +353,7 @@ module.exports = { }) return true } catch (e) { + appLog.error(new Error(e)) return false } }, @@ -360,6 +374,7 @@ module.exports = { }) return res.data } catch (e) { + appLog.error(new Error(e)) return [] } }, @@ -384,6 +399,7 @@ module.exports = { }) return res.data } catch (e) { + appLog.error(new Error(e)) return null } } diff --git a/src/logger.js b/src/logger.js new file mode 100644 index 0000000..0fdaa3c --- /dev/null +++ b/src/logger.js @@ -0,0 +1,62 @@ +/** + * Created Date: Wednesday, November 1st 2017, 4:03:55 pm + * Author: yugasun + * Email: yuga.sun.bj@gmail.com + * ----- + * Last Modified: + * Modified By: + * ----- + * Copyright (c) 2017 yugasun + */ + +const log4js = require('log4js') + +const fileConfig = { + pm2: true, + appenders: { + app: { + 'type': 'file', + 'filename': 'log/app.log', + 'maxLogSize': 10485760, + 'numBackups': 3 + }, + access: { + 'type': 'dateFile', + 'filename': 'log/access.log', + 'pattern': '-yyyy-MM-dd', + 'category': 'http' + }, + errorFile: { type: 'file', filename: 'log/errors.log' }, + errors: { + 'type': 'logLevelFilter', + 'level': 'error', + 'appender': 'errorFile' + } + }, + categories: { + default: { appenders: ['app', 'errors'], level: 'trace' }, + http: { appenders: ['access'], level: 'info' } + } +} +const consoleConfig = { + pm2: true, + appenders: { + console: { type: 'console' } + }, + categories: { + default: { appenders: ['console'], level: 'info' }, + http: { appenders: ['console'], level: 'info' } + } +} + +const config = process.env.LOG_TYPE === 'file' ? fileConfig : consoleConfig + +log4js.configure(config) + +const appLog = log4js.getLogger('app') +const accessLog = log4js.getLogger('http') + +module.exports = { + appLog, + accessLog +} diff --git a/src/modules/releases/autoReleaseNote.js b/src/modules/releases/autoReleaseNote.js index d79b79a..dd4f004 100644 --- a/src/modules/releases/autoReleaseNote.js +++ b/src/modules/releases/autoReleaseNote.js @@ -3,7 +3,12 @@ * @author xuexb */ -const { getTags, compareCommits, getReleaseByTag, createRelease } = require('../../github') +const { + getTags, + compareCommits, + getReleaseByTag, + createRelease +} = require('../../github') const RELEASE_CHANGE_MAP = { document: 'docs', diff --git a/src/utils.js b/src/utils.js index 447ada2..7afbb93 100755 --- a/src/utils.js +++ b/src/utils.js @@ -2,14 +2,8 @@ * @file 工具集 * @author xuexb */ - -const format = require('string-template') -const path = require('path') -const fs = require('fs') const crypto = require('crypto') const { fixedTimeComparison } = require('cryptiles') -const { execSync } = require('child_process') -// const gitPullOrClone = require('git-pull-or-clone') const utils = {