This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): improve logging methods
- Loading branch information
Showing
9 changed files
with
413 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
import * as Logger from './logger'; | ||
import {Logger} from './logger2'; | ||
|
||
export class ProtractorError extends Error { | ||
msg: string; | ||
const CONFIG_ERROR_CODE = 105; | ||
|
||
export class ProtractorError { | ||
error: Error; | ||
description: string; | ||
code: number; | ||
constructor(msg: string, code: number) { | ||
super(msg); | ||
this.msg = msg; | ||
stack: string; | ||
constructor(logger: Logger, description: string, code: number) { | ||
this.error = new Error(); | ||
this.description = description; | ||
this.code = code; | ||
Logger.error('error code: ' + this.code + ' - ' + this.msg); | ||
logger.error('error code: ' + this.code); | ||
logger.error('description: ' + this.description); | ||
this.stack = this.error.stack; | ||
} | ||
} | ||
|
||
const CONFIG_ERROR_CODE = 105; | ||
|
||
/** | ||
* Configuration file error | ||
*/ | ||
export class ConfigError extends ProtractorError { | ||
constructor(msg: string) { super(msg, CONFIG_ERROR_CODE); } | ||
static CODE = CONFIG_ERROR_CODE; | ||
constructor(logger: Logger, description: string) { | ||
super(logger, description, ConfigError.CODE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import * as chalk from 'chalk'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import {Config} from './configParser'; | ||
|
||
export enum LogLevel { | ||
ERROR, | ||
WARN, | ||
INFO, | ||
DEBUG | ||
} | ||
|
||
export enum WriteTo { | ||
CONSOLE, | ||
FILE, | ||
BOTH, | ||
NONE | ||
} | ||
let logFile = 'protractor.log'; | ||
|
||
export class Logger { | ||
static logLevel: LogLevel = LogLevel.INFO; | ||
static showTimestamp: boolean = true; | ||
static showId: boolean = true; | ||
static writeTo: WriteTo = WriteTo.CONSOLE; | ||
static fd: any; | ||
static firstWrite: boolean = false; | ||
|
||
static set(config: Config): void { | ||
if (config.troubleshoot) { | ||
Logger.logLevel = LogLevel.DEBUG; | ||
} | ||
} | ||
|
||
static setWrite(writeTo: WriteTo, opt_logFile?: string): void { | ||
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; | ||
} | ||
} | ||
|
||
constructor(private id: string) {} | ||
|
||
log(logLevel: LogLevel, msgs: any[]): void { | ||
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'); | ||
} | ||
} | ||
|
||
print_(logLevel: LogLevel, msgs: any[]): void { | ||
this.printBuffer_(logLevel, msgs); | ||
} | ||
|
||
printBuffer_(logLevel: LogLevel, msgs: any[]): void { | ||
let consoleLog: string = ''; | ||
let fileLog: string = ''; | ||
|
||
if (Logger.showTimestamp) { | ||
consoleLog += Logger.timestamp_(true); | ||
fileLog += Logger.timestamp_(false); | ||
} | ||
consoleLog += Logger.id_(logLevel, this.id, true); | ||
fileLog += Logger.id_(logLevel, this.id, false); | ||
if (Logger.showId) { | ||
consoleLog += Logger.level_(logLevel, this.id, true); | ||
fileLog += Logger.level_(logLevel, this.id, false); | ||
} | ||
consoleLog += ' - '; | ||
fileLog += ' - '; | ||
|
||
switch (Logger.writeTo) { | ||
case WriteTo.CONSOLE: | ||
msgs.unshift(consoleLog); | ||
console.log.apply(console, msgs); | ||
break; | ||
case WriteTo.FILE: | ||
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: | ||
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; | ||
} | ||
} | ||
|
||
info(...msgs: any[]): void { this.log(LogLevel.INFO, msgs); } | ||
|
||
debug(...msgs: any[]): void { this.log(LogLevel.DEBUG, msgs); } | ||
|
||
warn(...msgs: any[]): void { this.log(LogLevel.WARN, msgs); } | ||
|
||
error(...msgs: any[]): void { this.log(LogLevel.ERROR, msgs); } | ||
|
||
static timestamp_(format: boolean): string { | ||
let d = new Date(); | ||
let ts = '['; | ||
let spaces = 1; | ||
let hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours(); | ||
let minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes(); | ||
let seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds(); | ||
if (format) { | ||
ts += chalk.gray(hours + ':' + minutes + ':' + seconds) + ']'; | ||
} else { | ||
ts += hours + ':' + minutes + ':' + seconds + ']'; | ||
spaces = 12 - ts.length; | ||
} | ||
ts += ' '; | ||
return ts; | ||
} | ||
|
||
static level_(logLevel: LogLevel, id: string, format: boolean): string { | ||
let level = LogLevel[logLevel].toString(); | ||
if (!format) { | ||
return '/' + id; | ||
} else if (logLevel == LogLevel.ERROR) { | ||
return chalk.red('/' + id); | ||
} else if (logLevel == LogLevel.WARN) { | ||
return chalk.yellow('/' + id); | ||
} else { | ||
return '/' + id; | ||
} | ||
} | ||
|
||
static id_(logLevel: LogLevel, id: string, format: boolean): string { | ||
let level = LogLevel[logLevel].toString(); | ||
if (!format) { | ||
return level[0]; | ||
} else if (logLevel == LogLevel.ERROR) { | ||
return chalk.red(level[0]); | ||
} else if (logLevel == LogLevel.WARN) { | ||
return chalk.yellow(level[0]); | ||
} else { | ||
return level[0]; | ||
} | ||
} | ||
|
||
static msgToFile_(msgs: any): string { | ||
let log = ''; | ||
for (let pos = 0; pos < msgs.length; pos++) { | ||
let msg = msgs[pos]; | ||
let ret: any; | ||
if (typeof msg === 'object') { | ||
ret = JSON.stringify(msg); | ||
} else { | ||
ret = msg; | ||
} | ||
if (pos !== msgs.length - 1) { | ||
ret += ' '; | ||
} | ||
log += ret; | ||
} | ||
return log; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.