old logger was blocking indefinitely. made new logger.

This commit is contained in:
2018-09-01 15:59:11 -07:00
parent 31d059972c
commit 229f79e837
11 changed files with 359 additions and 51 deletions
+1 -1
View File
@@ -7,12 +7,12 @@ module.exports = class Door {
return new Promise(success => {
console.log('OPEN DOOR!')
const board = new Board(2)
board.allOn()
setTimeout(() => {
console.log('CLOSING DOOR!')
board.allOff()
success()
}, DELAY)
return board.allOn()
})
}
+65
View File
@@ -0,0 +1,65 @@
var winston = require('winston'); // for transports.Console
const fs = require('fs')
const path = require('path')
const LOGS_PATH = path.join(process.cwd(), 'logs.log')
var winston = winston.createLogger({
transports: [
new (winston.transports.Console)({ level: 'debug' }),
new (winston.transports.File)({ filename: 'logs.log', level: 'debug' })
]
});
winston.logError = function(card, message) {
winston.log({
level: 'error',
message: message,
timestamp: new Date().getTime(),
card: card,
})
}
winston.logGrantedCard = function(card) {
winston.log({
level: 'info',
message: 'granted',
timestamp: new Date().getTime(),
card: card
})
}
winston.logRejectedCard = function(card) {
winston.log({
level: 'info',
message: 'rejected',
timestamp: new Date().getTime(),
card: card
})
}
winston.readLogs = function () {
return new Promise((resolve, reject) => {
// specify "utf8" to return a string
fs.readFile(LOGS_PATH, "utf8", (err, data) => {
if (err) return resolve(JSON.parse("[]"))
var out = []
var split = data.split("\n")
// console.log(split)
for(var i=0; i<split.length; i++){
var line=split[i]
if (line) {
// console.log(line)
myLine = JSON.parse(line);
// console.log(myLine)
if (myLine.timestamp) {
out.push(myLine)
}
}
}
// console.log(out)
resolve(out)
})
})
}
module.exports = winston