logger.js
9.28 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"use strict";
var fs = require("fs");
var path = require("path");
// Will use chalk if chalk is available to add color to console logging
var chalk;
var printRed;
var printYellow;
var printGray;
try {
chalk = require('chalk');
printRed = chalk.red;
printYellow = chalk.yellow;
printGray = chalk.gray;
}
catch (e) {
printRed = printYellow = printGray = function (msg) {
return msg;
};
}
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["ERROR"] = 0] = "ERROR";
LogLevel[LogLevel["WARN"] = 1] = "WARN";
LogLevel[LogLevel["INFO"] = 2] = "INFO";
LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
})(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
var WriteTo;
(function (WriteTo) {
WriteTo[WriteTo["CONSOLE"] = 0] = "CONSOLE";
WriteTo[WriteTo["FILE"] = 1] = "FILE";
WriteTo[WriteTo["BOTH"] = 2] = "BOTH";
WriteTo[WriteTo["NONE"] = 3] = "NONE";
})(WriteTo = exports.WriteTo || (exports.WriteTo = {}));
var logFile = 'protractor.log'; // the default log file name
/**
* Logger class adds timestamp output, log levels, and identifiers to help
* when debugging. Also could write to console, file, both, or none.
*/
var Logger = (function () {
/**
* Creates a logger instance with an ID for the logger.
* @constructor
*/
function Logger(id) {
this.id = id;
}
/**
* Set up the logging configuration from the protractor configuration file.
* @param config The protractor configuration
*/
Logger.set = function (config) {
if (config.troubleshoot) {
Logger.logLevel = LogLevel.DEBUG;
}
};
/**
* Set up the write location. If writing to a file, get the file descriptor.
* @param writeTo The enum for where to write the logs.
* @param opt_logFile An optional parameter to override the log file location.
*/
Logger.setWrite = function (writeTo, opt_logFile) {
if (opt_logFile) {
logFile = opt_logFile;
}
Logger.writeTo = writeTo;
if (Logger.writeTo == WriteTo.FILE || Logger.writeTo == WriteTo.BOTH) {
Logger.fd = fs.openSync(path.resolve(logFile), 'a');
Logger.firstWrite = false;
}
};
/**
* Log INFO
* @param ...msgs multiple arguments to be logged.
*/
Logger.prototype.info = function () {
var msgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
msgs[_i] = arguments[_i];
}
this.log_(LogLevel.INFO, msgs);
};
/**
* Log DEBUG
* @param ...msgs multiple arguments to be logged.
*/
Logger.prototype.debug = function () {
var msgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
msgs[_i] = arguments[_i];
}
this.log_(LogLevel.DEBUG, msgs);
};
/**
* Log WARN
* @param ...msgs multiple arguments to be logged.
*/
Logger.prototype.warn = function () {
var msgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
msgs[_i] = arguments[_i];
}
this.log_(LogLevel.WARN, msgs);
};
/**
* Log ERROR
* @param ...msgs multiple arguments to be logged.
*/
Logger.prototype.error = function () {
var msgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
msgs[_i] = arguments[_i];
}
this.log_(LogLevel.ERROR, msgs);
};
/**
* For the log level set, check to see if the messages should be logged.
* @param logLevel The log level of the message.
* @param msgs The messages to be logged
*/
Logger.prototype.log_ = function (logLevel, msgs) {
switch (Logger.logLevel) {
case LogLevel.ERROR:
if (logLevel <= LogLevel.ERROR) {
this.print_(logLevel, msgs);
}
break;
case LogLevel.WARN:
if (logLevel <= LogLevel.WARN) {
this.print_(logLevel, msgs);
}
break;
case LogLevel.INFO:
if (logLevel <= LogLevel.INFO) {
this.print_(logLevel, msgs);
}
break;
case LogLevel.DEBUG:
if (logLevel <= LogLevel.DEBUG) {
this.print_(logLevel, msgs);
}
break;
default:
throw new Error('Log level undefined');
}
};
/**
* Format with timestamp, log level, identifier, and message and log to
* specified medium (console, file, both, none).
* @param logLevel The log level of the message.
* @param msgs The messages to be logged.
*/
Logger.prototype.print_ = function (logLevel, msgs) {
var consoleLog = '';
var fileLog = '';
if (Logger.showTimestamp) {
consoleLog += Logger.timestamp_(WriteTo.CONSOLE);
fileLog += Logger.timestamp_(WriteTo.FILE);
}
consoleLog += Logger.level_(logLevel, this.id, WriteTo.CONSOLE);
fileLog += Logger.level_(logLevel, this.id, WriteTo.FILE);
if (Logger.showId) {
consoleLog += Logger.id_(logLevel, this.id, WriteTo.CONSOLE);
fileLog += Logger.id_(logLevel, this.id, WriteTo.FILE);
}
consoleLog += ' -';
fileLog += ' - ';
switch (Logger.writeTo) {
case WriteTo.CONSOLE:
msgs.unshift(consoleLog);
console.log.apply(console, msgs);
break;
case WriteTo.FILE:
// for the first line written to the file, add a space
if (!Logger.firstWrite) {
fs.writeSync(Logger.fd, '\n');
Logger.firstWrite = true;
}
fileLog += ' ' + Logger.msgToFile_(msgs);
fs.writeSync(Logger.fd, fileLog + '\n');
break;
case WriteTo.BOTH:
// for the first line written to the file, add a space
if (!Logger.firstWrite) {
fs.writeSync(Logger.fd, '\n');
Logger.firstWrite = true;
}
fileLog += ' ' + Logger.msgToFile_(msgs);
fs.writeSync(Logger.fd, fileLog + '\n');
msgs.unshift(consoleLog);
console.log.apply(console, msgs);
break;
case WriteTo.NONE:
break;
}
};
/**
* Get a timestamp formatted with [hh:mm:ss]
* @param writeTo The enum for where to write the logs.
* @return The string of the formatted timestamp
*/
Logger.timestamp_ = function (writeTo) {
var d = new Date();
var ts = '[';
var hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours();
var minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
var seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
if (writeTo == WriteTo.CONSOLE) {
ts += printGray(hours + ':' + minutes + ':' + seconds) + ']';
}
else {
ts += hours + ':' + minutes + ':' + seconds + ']';
}
ts += ' ';
return ts;
};
/**
* Get the identifier of the logger as '/<id>'
* @param logLevel The log level of the message.
* @param writeTo The enum for where to write the logs.
* @return The string of the formatted id
*/
Logger.id_ = function (logLevel, id, writeTo) {
if (writeTo === WriteTo.FILE) {
return '/' + id;
}
else if (logLevel === LogLevel.ERROR) {
return printRed('/' + id);
}
else if (logLevel === LogLevel.WARN) {
return printYellow('/' + id);
}
else {
return '/' + id;
}
};
/**
* Get the log level formatted with the first letter. For info, it is I.
* @param logLevel The log level of the message.
* @param writeTo The enum for where to write the logs.
* @return The string of the formatted log level
*/
Logger.level_ = function (logLevel, id, writeTo) {
var level = LogLevel[logLevel].toString();
if (writeTo === WriteTo.FILE) {
return level[0];
}
else if (logLevel === LogLevel.ERROR) {
return printRed(level[0]);
}
else if (logLevel === LogLevel.WARN) {
return printYellow(level[0]);
}
else {
return level[0];
}
};
/**
* Convert the list of messages to a single string message.
* @param msgs The list of messages.
* @return The string of the formatted messages
*/
Logger.msgToFile_ = function (msgs) {
var log = '';
for (var pos = 0; pos < msgs.length; pos++) {
var msg = msgs[pos];
var ret = void 0;
if (typeof msg === 'object') {
ret = JSON.stringify(msg);
}
else {
ret = msg;
}
if (pos !== msgs.length - 1) {
ret += ' ';
}
log += ret;
}
return log;
};
return Logger;
}());
Logger.logLevel = LogLevel.INFO;
Logger.showTimestamp = true;
Logger.showId = true;
Logger.writeTo = WriteTo.CONSOLE;
Logger.firstWrite = false;
exports.Logger = Logger;