Skip to content

Commit

Permalink
Merge pull request #202 from Shenniey/add_timestampDefault
Browse files Browse the repository at this point in the history
adding timestamp logging to be configurable (turned on by default)
  • Loading branch information
roblourens authored Oct 1, 2018
2 parents f479e3d + 1ad8b31 commit bd24ef8
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions adapter/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ export class Logger {
private _currentLogger: InternalLogger;
private _pendingLogQ: ILogItem[] = [];

private _prependTimestamp: boolean;

log(msg: string, level = LogLevel.Log): void {
if (this._prependTimestamp) {
msg = this._formatTimeString() + ":: " + msg;
}
msg = msg + '\n';
this._write(msg, level);
}
Expand Down Expand Up @@ -75,15 +80,37 @@ export class Logger {
}
}

/*
* Private method to pad zeroes for numbers
*/
private _padZeroes(minDesiredLength: number, numberToPad: string): string {
if (numberToPad.length >= minDesiredLength) {
return numberToPad;
} else {
return String("0".repeat(minDesiredLength) + numberToPad).slice(-minDesiredLength);
}
}

private _formatTimeString(): string {
let d = new Date();
let hourString = this._padZeroes(2, String(d.getHours()));
let minuteString = this._padZeroes(2, String(d.getMinutes()));
let secondString = this._padZeroes(2, String(d.getSeconds()));
let millisecondString = this._padZeroes(3, String(d.getMilliseconds()));
return hourString + ":" + minuteString + ":" + secondString + '.' + millisecondString;
}

/**
* Set the logger's minimum level to log in the console, and whether to log to the file. Log messages are queued before this is
* called the first time, because minLogLevel defaults to Warn.
*/
setup(consoleMinLogLevel: LogLevel, _logFilePath?: string|boolean): void {
setup(consoleMinLogLevel: LogLevel, _logFilePath?: string|boolean, prependTimestamp: boolean = true): void {
const logFilePath = typeof _logFilePath === 'string' ?
_logFilePath :
(_logFilePath && this._logFilePathFromInit);

this._prependTimestamp = prependTimestamp;

if (this._currentLogger) {
this._currentLogger.setup(consoleMinLogLevel, logFilePath).then(() => {
// Now that we have a minimum logLevel, we can clear out the queue of pending messages
Expand All @@ -103,9 +130,11 @@ export class Logger {
this._currentLogger = new InternalLogger(logCallback, logToConsole);
this._logFilePathFromInit = logFilePath;

// Log the date at the top
const d = new Date();
const timestamp = d.toLocaleTimeString() + ', ' + d.toLocaleDateString();
// Set timestamps to false for initialization logging
this._prependTimestamp = false;

// Log the date and start time at the top
const timestamp = new Date().toLocaleDateString() + ", " + this._formatTimeString();
this.verbose(timestamp);
}
}
Expand Down

0 comments on commit bd24ef8

Please sign in to comment.