UNPKG

8.91 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("fs");
4const path = require("path");
5// Will use chalk if chalk is available to add color to console logging
6let chalk;
7let printRed;
8let printYellow;
9let printGray;
10try {
11 chalk = require('chalk');
12 printRed = chalk.red;
13 printYellow = chalk.yellow;
14 printGray = chalk.gray;
15}
16catch (e) {
17 printRed = printYellow = printGray = (msg) => {
18 return msg;
19 };
20}
21var LogLevel;
22(function (LogLevel) {
23 LogLevel[LogLevel["ERROR"] = 0] = "ERROR";
24 LogLevel[LogLevel["WARN"] = 1] = "WARN";
25 LogLevel[LogLevel["INFO"] = 2] = "INFO";
26 LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
27})(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
28var WriteTo;
29(function (WriteTo) {
30 WriteTo[WriteTo["CONSOLE"] = 0] = "CONSOLE";
31 WriteTo[WriteTo["FILE"] = 1] = "FILE";
32 WriteTo[WriteTo["BOTH"] = 2] = "BOTH";
33 WriteTo[WriteTo["NONE"] = 3] = "NONE";
34})(WriteTo = exports.WriteTo || (exports.WriteTo = {}));
35let logFile = 'protractor.log'; // the default log file name
36/**
37 * Logger class adds timestamp output, log levels, and identifiers to help
38 * when debugging. Also could write to console, file, both, or none.
39 */
40class Logger {
41 /**
42 * Creates a logger instance with an ID for the logger.
43 * @constructor
44 */
45 constructor(id) {
46 this.id = id;
47 }
48 /**
49 * Set up the logging configuration from the protractor configuration file.
50 * @param config The protractor configuration
51 */
52 static set(config) {
53 if (config.troubleshoot) {
54 Logger.logLevel = LogLevel.DEBUG;
55 }
56 else if (config.logLevel) {
57 Logger.logLevel = LogLevel[config.logLevel];
58 }
59 }
60 /**
61 * Set up the write location. If writing to a file, get the file descriptor.
62 * @param writeTo The enum for where to write the logs.
63 * @param opt_logFile An optional parameter to override the log file location.
64 */
65 static setWrite(writeTo, opt_logFile) {
66 if (opt_logFile) {
67 logFile = opt_logFile;
68 }
69 Logger.writeTo = writeTo;
70 if (Logger.writeTo == WriteTo.FILE || Logger.writeTo == WriteTo.BOTH) {
71 Logger.fd = fs.openSync(path.resolve(logFile), 'a');
72 Logger.firstWrite = false;
73 }
74 }
75 /**
76 * Log INFO
77 * @param ...msgs multiple arguments to be logged.
78 */
79 info(...msgs) {
80 this.log_(LogLevel.INFO, msgs);
81 }
82 /**
83 * Log DEBUG
84 * @param ...msgs multiple arguments to be logged.
85 */
86 debug(...msgs) {
87 this.log_(LogLevel.DEBUG, msgs);
88 }
89 /**
90 * Log WARN
91 * @param ...msgs multiple arguments to be logged.
92 */
93 warn(...msgs) {
94 this.log_(LogLevel.WARN, msgs);
95 }
96 /**
97 * Log ERROR
98 * @param ...msgs multiple arguments to be logged.
99 */
100 error(...msgs) {
101 this.log_(LogLevel.ERROR, msgs);
102 }
103 /**
104 * For the log level set, check to see if the messages should be logged.
105 * @param logLevel The log level of the message.
106 * @param msgs The messages to be logged
107 */
108 log_(logLevel, msgs) {
109 switch (Logger.logLevel) {
110 case LogLevel.ERROR:
111 if (logLevel <= LogLevel.ERROR) {
112 this.print_(logLevel, msgs);
113 }
114 break;
115 case LogLevel.WARN:
116 if (logLevel <= LogLevel.WARN) {
117 this.print_(logLevel, msgs);
118 }
119 break;
120 case LogLevel.INFO:
121 if (logLevel <= LogLevel.INFO) {
122 this.print_(logLevel, msgs);
123 }
124 break;
125 case LogLevel.DEBUG:
126 if (logLevel <= LogLevel.DEBUG) {
127 this.print_(logLevel, msgs);
128 }
129 break;
130 default:
131 throw new Error('Invalid log level');
132 }
133 }
134 /**
135 * Format with timestamp, log level, identifier, and message and log to
136 * specified medium (console, file, both, none).
137 * @param logLevel The log level of the message.
138 * @param msgs The messages to be logged.
139 */
140 print_(logLevel, msgs) {
141 let consoleLog = '';
142 let fileLog = '';
143 if (Logger.showTimestamp) {
144 consoleLog += Logger.timestamp_(WriteTo.CONSOLE);
145 fileLog += Logger.timestamp_(WriteTo.FILE);
146 }
147 consoleLog += Logger.level_(logLevel, this.id, WriteTo.CONSOLE);
148 fileLog += Logger.level_(logLevel, this.id, WriteTo.FILE);
149 if (Logger.showId) {
150 consoleLog += Logger.id_(logLevel, this.id, WriteTo.CONSOLE);
151 fileLog += Logger.id_(logLevel, this.id, WriteTo.FILE);
152 }
153 consoleLog += ' -';
154 fileLog += ' - ';
155 switch (Logger.writeTo) {
156 case WriteTo.CONSOLE:
157 msgs.unshift(consoleLog);
158 console.log.apply(console, msgs);
159 break;
160 case WriteTo.FILE:
161 // for the first line written to the file, add a space
162 if (!Logger.firstWrite) {
163 fs.writeSync(Logger.fd, '\n');
164 Logger.firstWrite = true;
165 }
166 fileLog += ' ' + Logger.msgToFile_(msgs);
167 fs.writeSync(Logger.fd, fileLog + '\n');
168 break;
169 case WriteTo.BOTH:
170 // for the first line written to the file, add a space
171 if (!Logger.firstWrite) {
172 fs.writeSync(Logger.fd, '\n');
173 Logger.firstWrite = true;
174 }
175 fileLog += ' ' + Logger.msgToFile_(msgs);
176 fs.writeSync(Logger.fd, fileLog + '\n');
177 msgs.unshift(consoleLog);
178 console.log.apply(console, msgs);
179 break;
180 case WriteTo.NONE:
181 break;
182 }
183 }
184 /**
185 * Get a timestamp formatted with [hh:mm:ss]
186 * @param writeTo The enum for where to write the logs.
187 * @return The string of the formatted timestamp
188 */
189 static timestamp_(writeTo) {
190 let d = new Date();
191 let ts = '[';
192 let hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours();
193 let minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
194 let seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
195 if (writeTo == WriteTo.CONSOLE) {
196 ts += printGray(hours + ':' + minutes + ':' + seconds) + ']';
197 }
198 else {
199 ts += hours + ':' + minutes + ':' + seconds + ']';
200 }
201 ts += ' ';
202 return ts;
203 }
204 /**
205 * Get the identifier of the logger as '/<id>'
206 * @param logLevel The log level of the message.
207 * @param writeTo The enum for where to write the logs.
208 * @return The string of the formatted id
209 */
210 static id_(logLevel, id, writeTo) {
211 if (writeTo === WriteTo.FILE) {
212 return '/' + id;
213 }
214 else if (logLevel === LogLevel.ERROR) {
215 return printRed('/' + id);
216 }
217 else if (logLevel === LogLevel.WARN) {
218 return printYellow('/' + id);
219 }
220 else {
221 return '/' + id;
222 }
223 }
224 /**
225 * Get the log level formatted with the first letter. For info, it is I.
226 * @param logLevel The log level of the message.
227 * @param writeTo The enum for where to write the logs.
228 * @return The string of the formatted log level
229 */
230 static level_(logLevel, id, writeTo) {
231 let level = LogLevel[logLevel].toString();
232 if (writeTo === WriteTo.FILE) {
233 return level[0];
234 }
235 else if (logLevel === LogLevel.ERROR) {
236 return printRed(level[0]);
237 }
238 else if (logLevel === LogLevel.WARN) {
239 return printYellow(level[0]);
240 }
241 else {
242 return level[0];
243 }
244 }
245 /**
246 * Convert the list of messages to a single string message.
247 * @param msgs The list of messages.
248 * @return The string of the formatted messages
249 */
250 static msgToFile_(msgs) {
251 let log = '';
252 for (let pos = 0; pos < msgs.length; pos++) {
253 let msg = msgs[pos];
254 let ret;
255 if (typeof msg === 'object') {
256 ret = JSON.stringify(msg);
257 }
258 else {
259 ret = msg;
260 }
261 if (pos !== msgs.length - 1) {
262 ret += ' ';
263 }
264 log += ret;
265 }
266 return log;
267 }
268}
269Logger.logLevel = LogLevel.INFO;
270Logger.showTimestamp = true;
271Logger.showId = true;
272Logger.writeTo = WriteTo.CONSOLE;
273Logger.firstWrite = false;
274exports.Logger = Logger;
275//# sourceMappingURL=logger.js.map
\No newline at end of file