add logging
This commit is contained in:
parent
721e72c460
commit
7db4a15c1b
|
@ -14,9 +14,9 @@
|
|||
"database": "db"
|
||||
},
|
||||
"logger": {
|
||||
"errorLogging": {
|
||||
"filename": "error.log",
|
||||
"level": "error"
|
||||
"infoLogging": {
|
||||
"filename": "server.log",
|
||||
"level": "info"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,41 +1,42 @@
|
|||
const Device = require('../models/deviceModel');
|
||||
const logger = require('../logger');
|
||||
|
||||
function index(req, res) {
|
||||
if (req.query.name) {
|
||||
console.log(req.query.name);
|
||||
Device.findOne({ name: req.query.name }, (err, device) => {
|
||||
if (!device) {
|
||||
res.status(404).send();
|
||||
} else {
|
||||
res.json(device);
|
||||
logger.logError(`index: ${err}`);
|
||||
return res.status(404).send();
|
||||
}
|
||||
return res.json(device);
|
||||
});
|
||||
} else {
|
||||
Device.get((err, devices) => {
|
||||
if (err) {
|
||||
res.status(500).send();
|
||||
logger.logError(`index: ${err}`);
|
||||
return res.status(500).send();
|
||||
}
|
||||
res.json(devices);
|
||||
return res.json(devices);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function view(req, res) {
|
||||
Device.findById(req.params.deviceId)
|
||||
.then((device) => {
|
||||
res.json(device);
|
||||
})
|
||||
.then(device => res.json(device))
|
||||
.catch((err) => {
|
||||
res.send(err);
|
||||
logger.logError(`view: ${err}`);
|
||||
return res.status(500).send();
|
||||
});
|
||||
}
|
||||
|
||||
function update(req, res) {
|
||||
Device.findOneAndUpdate({ _id: req.params.deviceId }, req.body, (err) => {
|
||||
if (err) {
|
||||
res.status(500).send();
|
||||
logger.logError(`update: ${err}`);
|
||||
return res.status(500).send();
|
||||
}
|
||||
res.status(200).send();
|
||||
return res.status(200).send();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -43,20 +44,20 @@ function add(req, res) {
|
|||
const device = new Device(req.body);
|
||||
device
|
||||
.save()
|
||||
.then((item) => {
|
||||
res.status(201).json(item);
|
||||
})
|
||||
.then(item => res.status(201).json(item))
|
||||
.catch((err) => {
|
||||
res.status(500).send();
|
||||
logger.logError(`add: ${err}`);
|
||||
return res.status(500).send();
|
||||
});
|
||||
}
|
||||
|
||||
function remove(req, res) {
|
||||
Device.deleteOne({ _id: req.params.deviceId }, (err) => {
|
||||
if (err) {
|
||||
res.status(500).send();
|
||||
logger.logError(`delete: ${err}`);
|
||||
return res.status(500).send();
|
||||
}
|
||||
res.status(200).send();
|
||||
return res.status(200).send();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
/* eslint-disable no-console */
|
||||
const bunyan = require('bunyan');
|
||||
const config = require('./config.json');
|
||||
|
||||
const log = bunyan.createLogger({
|
||||
name: config.app.name,
|
||||
streams: [{
|
||||
level: 'info',
|
||||
path: config.logger.infoLogging.filename,
|
||||
}],
|
||||
});
|
||||
|
||||
function logError(msg) {
|
||||
log.error(msg);
|
||||
}
|
||||
|
||||
function logInfo(msg) {
|
||||
log.info(msg);
|
||||
}
|
||||
|
||||
function logFatal(msg) {
|
||||
log.fatal(msg);
|
||||
}
|
||||
|
||||
function logConsole(msg) {
|
||||
console.log(`(${config.app.name}): ${msg}`);
|
||||
}
|
||||
|
||||
module.exports.logError = logError;
|
||||
module.exports.logInfo = logInfo;
|
||||
module.exports.logFatal = logFatal;
|
||||
module.exports.logConsole = logConsole;
|
|
@ -109,8 +109,7 @@
|
|||
"balanced-match": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
|
||||
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
|
||||
"dev": true
|
||||
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
|
||||
},
|
||||
"bluebird": {
|
||||
"version": "3.5.1",
|
||||
|
@ -161,7 +160,6 @@
|
|||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"balanced-match": "^1.0.0",
|
||||
"concat-map": "0.0.1"
|
||||
|
@ -172,6 +170,17 @@
|
|||
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.0.tgz",
|
||||
"integrity": "sha512-9Aeai9TacfNtWXOYarkFJRW2CWo+dRon+fuLZYJmvLV3+MiUp0bEI6IAZfXEIg7/Pl/7IWlLaDnhzTsD81etQA=="
|
||||
},
|
||||
"bunyan": {
|
||||
"version": "1.8.12",
|
||||
"resolved": "https://registry.npmjs.org/bunyan/-/bunyan-1.8.12.tgz",
|
||||
"integrity": "sha1-8VDw9nSKvdcq6uhPBEA74u8RN5c=",
|
||||
"requires": {
|
||||
"dtrace-provider": "~0.8",
|
||||
"moment": "^2.10.6",
|
||||
"mv": "~2",
|
||||
"safe-json-stringify": "~1"
|
||||
}
|
||||
},
|
||||
"bytes": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
|
||||
|
@ -233,8 +242,7 @@
|
|||
"concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
|
||||
"dev": true
|
||||
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
|
||||
},
|
||||
"contains-path": {
|
||||
"version": "0.1.0",
|
||||
|
@ -318,6 +326,15 @@
|
|||
"esutils": "^2.0.2"
|
||||
}
|
||||
},
|
||||
"dtrace-provider": {
|
||||
"version": "0.8.7",
|
||||
"resolved": "https://registry.npmjs.org/dtrace-provider/-/dtrace-provider-0.8.7.tgz",
|
||||
"integrity": "sha1-3JObTT4GIM/gwc2APQ0tftBP/QQ=",
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"nan": "^2.10.0"
|
||||
}
|
||||
},
|
||||
"ee-first": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
|
||||
|
@ -904,7 +921,6 @@
|
|||
"version": "1.0.6",
|
||||
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
|
||||
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"once": "^1.3.0",
|
||||
"wrappy": "1"
|
||||
|
@ -1137,7 +1153,6 @@
|
|||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
|
||||
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"brace-expansion": "^1.1.7"
|
||||
}
|
||||
|
@ -1145,18 +1160,22 @@
|
|||
"minimist": {
|
||||
"version": "0.0.8",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
|
||||
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
|
||||
"dev": true
|
||||
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
|
||||
},
|
||||
"mkdirp": {
|
||||
"version": "0.5.1",
|
||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
|
||||
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"minimist": "0.0.8"
|
||||
}
|
||||
},
|
||||
"moment": {
|
||||
"version": "2.24.0",
|
||||
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
|
||||
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==",
|
||||
"optional": true
|
||||
},
|
||||
"mongodb": {
|
||||
"version": "3.1.13",
|
||||
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.13.tgz",
|
||||
|
@ -1244,12 +1263,59 @@
|
|||
"integrity": "sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=",
|
||||
"dev": true
|
||||
},
|
||||
"mv": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/mv/-/mv-2.1.1.tgz",
|
||||
"integrity": "sha1-rmzg1vbV4KT32JN5jQPB6pVZtqI=",
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"mkdirp": "~0.5.1",
|
||||
"ncp": "~2.0.0",
|
||||
"rimraf": "~2.4.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"glob": {
|
||||
"version": "6.0.4",
|
||||
"resolved": "https://registry.npmjs.org/glob/-/glob-6.0.4.tgz",
|
||||
"integrity": "sha1-DwiGD2oVUSey+t1PnOJLGqtuTSI=",
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"inflight": "^1.0.4",
|
||||
"inherits": "2",
|
||||
"minimatch": "2 || 3",
|
||||
"once": "^1.3.0",
|
||||
"path-is-absolute": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"rimraf": {
|
||||
"version": "2.4.5",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.4.5.tgz",
|
||||
"integrity": "sha1-7nEM5dk6j9uFb7Xqj/Di11k0sto=",
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"glob": "^6.0.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"nan": {
|
||||
"version": "2.12.1",
|
||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.12.1.tgz",
|
||||
"integrity": "sha512-JY7V6lRkStKcKTvHO5NVSQRv+RV+FIL5pvDoLiAtSL9pKlC5x9PKQcZDsq7m4FO4d57mkhC6Z+QhAh3Jdk5JFw==",
|
||||
"optional": true
|
||||
},
|
||||
"natural-compare": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
||||
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
|
||||
"dev": true
|
||||
},
|
||||
"ncp": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz",
|
||||
"integrity": "sha1-GVoh1sRuNh0vsSgbo4uR6d9727M=",
|
||||
"optional": true
|
||||
},
|
||||
"negotiator": {
|
||||
"version": "0.6.1",
|
||||
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz",
|
||||
|
@ -1315,7 +1381,6 @@
|
|||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
|
||||
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"wrappy": "1"
|
||||
}
|
||||
|
@ -1405,8 +1470,7 @@
|
|||
"path-is-absolute": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
|
||||
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
|
||||
"dev": true
|
||||
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
|
||||
},
|
||||
"path-is-inside": {
|
||||
"version": "1.0.2",
|
||||
|
@ -1618,6 +1682,12 @@
|
|||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
||||
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
||||
},
|
||||
"safe-json-stringify": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/safe-json-stringify/-/safe-json-stringify-1.2.0.tgz",
|
||||
"integrity": "sha512-gH8eh2nZudPQO6TytOvbxnuhYBOvDBBLW52tz5q6X58lJcd/tkmqFR+5Z9adS8aJtURSXWThWy/xJtJwixErvg==",
|
||||
"optional": true
|
||||
},
|
||||
"safer-buffer": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
||||
|
@ -1959,8 +2029,7 @@
|
|||
"wrappy": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
|
||||
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
|
||||
"dev": true
|
||||
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
|
||||
},
|
||||
"write": {
|
||||
"version": "1.0.3",
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"body-parser": "^1.18.3",
|
||||
"bunyan": "^1.8.12",
|
||||
"express": "^4.16.4",
|
||||
"mongoose": "^5.4.13"
|
||||
}
|
||||
|
|
19
server.js
19
server.js
|
@ -2,6 +2,7 @@ const express = require('express');
|
|||
const mongoose = require('mongoose');
|
||||
const bodyParser = require('body-parser');
|
||||
const config = require('./config');
|
||||
const logger = require('./logger');
|
||||
const apiRoutes = require('./apiRoutes');
|
||||
|
||||
const { db } = config;
|
||||
|
@ -10,18 +11,26 @@ const port = process.env.PORT || config.app.port;
|
|||
|
||||
const connectionString = `${db.prefix}${db.user}:${db.password}@${db.host}/${db.database}`;
|
||||
|
||||
console.log(connectionString);
|
||||
|
||||
mongoose.connect(connectionString, { useNewUrlParser: true });
|
||||
mongoose.set('useFindAndModify', false);
|
||||
|
||||
const mongodb = mongoose.connection;
|
||||
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.use('/api', apiRoutes);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`trackmyd-api running. PORT: ${port}`);
|
||||
logger.logInfo('Server started.');
|
||||
logger.logConsole('Server started.');
|
||||
});
|
||||
|
||||
process.on('uncaughtException', (err) => {
|
||||
logger.logError(err);
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
mongoose.connection.close();
|
||||
logger.logInfo('Server stopped');
|
||||
logger.logConsole('Server stopped');
|
||||
process.exit(0);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue