forked from kalamuna/kalaboxv1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
124 lines (117 loc) · 3.06 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @file
* Establishes an error logging service for Kalabox using the Winston module.
*
* Copyright 2013 Kalamuna LLC
*/
// Dependencies:
var winston = require('winston'),
flow = require('nue').flow,
as = require('nue').as,
fs = require('fs'),
exec = require('child_process').exec,
config = require('./config');
// "Constants":
var KALABOX_DIR = config.get('KALABOX_DIR'),
LOG_FILE = KALABOX_DIR + 'kalabox.log';
// Create a new logger instance that writes to the console and log file.
exports = module.exports = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: LOG_FILE, json: false })
]
});
// Connect to Socket.io so we can emit an error event.
var socket;
exports.on('logging', function(transport, level, msg, meta) {
// Send message to frontend via event on error.
if (level == 'error') {
socket.emit('appError');
}
});
/**
* Initializes the logger, creating the log file if it doesn't exists.
*
* @param function callback
* Function to call once the initialization completes.
* @param object io
* IO event emitter dependency.
*/
exports.initialize = flow('initialize')(
function initialize0(callback, io) {
this.data.callback = callback;
// Configure Socket.io dependency.
socket = io.sockets;
// Check if Kalabox directory exists.
fs.exists(KALABOX_DIR, this.async(as(0)));
},
// If directory doesn't exist, create it.
function initialize1(exists) {
if (!exists) {
exec('mkdir -p ' + KALABOX_DIR, this.async());
}
else {
this.next();
}
},
// Check if log file exists.
function initialize2() {
fs.exists(LOG_FILE, this.async(as(0)));
},
// If log file doesn't exist, create it.
function initialize3(exists) {
if (!exists) {
fs.writeFile(LOG_FILE, '### Kalabox Log ###\n\n', this.async());
}
else {
this.next();
}
},
function initializeEnd() {
if (this.err) {
exports.error('Error initializing logging service: ' + this.err.message);
this.err = null;
}
// Set logger to handle uncaught exceptions too.
process.on('uncaughtException', function(err) {
exports.error('Uncaught Exception\n' + '\nStack:\n' + err.stack);
});
this.data.callback();
this.next();
}
);
/**
* Loads contents of the log file.
*
* @param function callback
* Function to call with the contents of the log file.
*/
exports.loadLog = flow('loadLog')(
// Check if log file exists.
function loadLog0(callback) {
this.data.callback = callback;
fs.exists(LOG_FILE, this.async(as(0)));
},
// Read in the file.
function loadLog1(exists) {
if (!exists) {
this.end();
}
else {
fs.readFile(LOG_FILE, this.async());
}
},
// Return log contents.
function loadLogEnd(contents) {
if (this.err) {
exports.error('Error loading log file: ' + this.err.message);
this.err = null;
this.next();
this.data.callback();
return;
}
if (contents) {
this.data.callback(contents);
}
}
);