-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
105 lines (86 loc) · 2.81 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env node
/*
* Logger.js
* ---------------------------------------------------------------
* This script logs all the entries in a log file and additionally
* outputs the message to console.
* ---------------------------------------------------------------
*
* @messageType can be E (Error), D (Debug), I (Information)
* @messageTag is typically the function name e.g. getLogFilename
* @messageBody is the real message to log
*
*/
var fs = require('fs');
var messageType = process.argv[2];
var messageTag = process.argv[3];
var messageBody = process.argv[4];
var DEBUG = false;
var logDirectory = '/home/smartscan/logs';
var logFile = logDirectory + '/' + getLogFilename();
/*
* Main execution
* ----------------------------------------------------------------------------------
*/
// Always show the message in console
var logMessage = timestamp() + ' - ' + '[' + messageType +'] ' + messageTag + ': ' + messageBody;
console.log(logMessage);
// Check if log directory exists and create it if not.
/* node.0.10.36 only have fs.exists
* in newer version fs.existsSync should be replaced with
* fs.accessSync(path, fs.F_OK) and be surrouned in try/catch
*/
//try {
if(fs.existsSync(logDirectory)) {
if (DEBUG) console.log('Directory exists.');
} else {
if(DEBUG) console.log('Directory doesnt exist. Creating it!');
fs.mkdirSync(logDirectory);
}
//} catch(e) {
// if(DEBUG) console.log('Directory doesnt exist. Creating it!. error: %s',e.message);
// fs.mkdirSync(logDirectory);
//}
// Check if log file exists and create it if not.
//try {
if(fs.existsSync(logFile)) {
if (DEBUG) console.log('Log file %s exists.',logFile);
} else {
if(DEBUG) console.log('Log file %s doesnt exists! Creating it!',logFile);
var fd = fs.openSync(logFile,'w');
fs.closeSync(fd);
}
//} catch(e) {
// if(DEBUG) console.log('Log file %s doesnt exists! Creating it!',logFile);
// var fd = fs.openSync(logFile,'w');
// fs.closeSync(fd);
//}
// Write to file
fs.appendFile(logFile,logMessage+'\n', function(err){
//
});
/*
* ------------------------------------------------------------------------------------
*/
/*
* Functions
*/
function timestamp() {
return now = new Date().toISOString().replace(/T/, ' ').replace(/Z/, '');
}
function getLogFilename() {
return new Date().toISOString().substring(0,10) + '.log';
}
/*
* logger -- add this function your script
* @type E, D, I.
* @tag Typically the name of the function.
*/
function logger(type,tag,msg) {
var exec = require('child_process').exec;
exec('/home/smartscan/scripts/logger.js ' + type + ' \'' + tag + '\' \'' + msg +'\'', function(error,stdout,stderr){
//if(error != null) {console.log(error);}
}).stdout.on('data',function(data){
console.log(data);
});
}