Authored by 周奇琪

日志记录

... ... @@ -3,15 +3,17 @@
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/4/25
*/
'use strict';
var express = require('express'),
let express = require('express'),
path = require('path'),
hbs = require('express-handlebars'),
bodyParser = require('body-parser'),
favicon = require('serve-favicon'),
cookieParser = require('cookie-parser');
var app = express();
require('express-handlebars');
require('serve-favicon');
let app = express();
app.set('view engine', '.hbs');
... ...
/**
* sub app guang
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/06
*/
var express = require('express'),
path = require('path'),
hbs = require('express-handlebars');
var app = express();
// set view engin
var doraemon = path.join(__dirname, '../../doraemon/views'); //parent view root
app.set('views', path.join(__dirname, 'views/action'));
app.engine('.hbs', hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: ['./views/partial', `${doraemon}/partial`],
helpers: 'helpers'
}));
// router
app.use(require('./router'));
module.exports = app;
\ No newline at end of file
... ...
/**
* router of sub app guang
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/06
*/
'use strict';
const router = require('express').Router();
const cRoot = './controllers';
let API = require('../../library/api');
// Your controller here
router.get('/',function(req,res){
let api = new API();
api.get('/v2/book/1220562',{}).then(function(body){
res.send(body);
}).catch(function (error) {
res.send('some thing wrong');
});
});
module.exports = router;
\ No newline at end of file
... ...
/**
* 系统配置
*
* @author hbomb qiqi.zhou@yoho.cn
* @date 2016/05/06
*/
module.exports = {
domains: {
api:'http://api.douban.com'
},
loggers: {
file:{
level:'info',
maxsize:100*1024*1024,
handleExceptions:true,
zippedArchive:true,
timestamp:true,
filename:'info.log',
},
udp: { //send by udp
level:'debug', //logger level
host:'192.168.102.162', //influxdb host
port:'4444'//influxdb port
},
'console':{
level:'debug',
colorize: 'all',
prettyPrint:true
}
}
};
\ No newline at end of file
... ...
... ... @@ -8,8 +8,10 @@
const rp = require('request-promise');
const _ = require('lodash');
const log = require('./logger');
const api = require('../config/common').domains.api;
const ApiUrl = 'http://api.yoho.yohoops.org/';
const ApiUrl = api;
class API {
... ... @@ -19,10 +21,24 @@ class API {
* @param data Obejct
*/
get(url, data) {
return rp({
log.info('API GET: %s, parms: %j',url,data,{});
log.profile('%s %j',url,data);
let ret = rp({
url: `${ApiUrl}${url}`,
qs: data
});
ret.then((body)=>{
log.profile('%s %j',url,data);
log.info('API GET: %s, parms: %j ',url,data,body);
}).catch((error)=>{
log.profile('%s %j',url,data);
log.error('API GET: %s, parms: %j ',url,data,error);
});
return ret;
}
/**
... ...
/**
* 日志工具类
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/06
*/
'use strict';
let winston = require('winston'),
config = require('../config/common');
require('influxdb-winston');
let logger = new (winston.Logger)({
transports: [
new (winston.transports.File)(config.loggers.file),
new (winston.transports.UdpTransport)(config.loggers.udp),
new (winston.transports.Console)(config.loggers.console)
]
});
module.exports = logger;
\ No newline at end of file
... ...
... ... @@ -12,8 +12,8 @@
"dev": "node_modules/.bin/nodemon -e js,hbs -i public/ app.js",
"online": "NODE_ENV=\"production\" node app.js",
"debug": "DEBUG=\"express:*\" node app.js",
"lint-js": "node_modules/.bin/eslint -c .eslintrc --cache --fix `git diff --cached --name-only --diff-filter=ACM | grep .js$` app.js",
"lint-css": "node_modules/.bin/stylelint --config .stylelintrc `git diff --cached --name-only --diff-filter=ACM | grep .css$`",
"lint-js": "node_modules/.bin/eslint -c .eslintrc --cache --fix \"git diff --cached --name-only --diff-filter=ACM | grep .js$\" app.js",
"lint-css": "node_modules/.bin/stylelint --config .stylelintrc \"git diff --cached --name-only --diff-filter=ACM | grep .css$\" public/css/*.css&",
"precommit": "npm run lint-js && npm run lint-css"
},
"license": "MIT",
... ... @@ -22,10 +22,12 @@
"cookie-parser": "^1.4.1",
"express": "^4.13.1",
"express-handlebars": "^3.0.0",
"influxdb-winston": "^1.0.1",
"lodash": "^4.8.2",
"morgan": "^1.7.0",
"request-promise": "^2.0.1",
"serve-favicon": "^2.3.0"
"serve-favicon": "^2.3.0",
"winston": "^2.2.0"
},
"devDependencies": {
"autoprefixer": "^6.3.6",
... ...