-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
67 lines (52 loc) · 1.43 KB
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//Import path module for directory crawling/management
const path = require('path');
//Import fs module for file read/write
const fs = require('fs');
//Time formatting module
const moment = require('moment');
//Provides OS consistend end of line
const eol = require('eol')
/*-------------------------------------------------------------------
LOG FUNCTIONS
-------------------------------------------------------------------*/
//Define logger class/constructor
function Logger(logDir)
{
this.logDir=logDir;
this.init(logDir);
}
//Define class functions
Logger.prototype.init = function(logDir)
{
if (!this.directoryExists(logDir))
{
fs.mkdirSync(logDir);
}
console.log('Logger created logging to: ' + logDir);
}
Logger.prototype.directoryExists = function(filePath)
{
try
{
return fs.statSync(filePath).isDirectory();
}
catch (err)
{
return false;
}
};
Logger.prototype.logSensorData = function(fileName, logString, append = true)
{
var logPath = path.join(this.logDir, fileName);
var log = eol.crlf(logString);
var flags = append ? { flag: 'a' } : {};
fs.writeFile(logPath, log, flags, (error) =>
{
if (error)
{
console.error('Write error to ${logPath} : ${error.message}');
}
});
};
//Enable class to be exported
module.exports = Logger;