-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Added Logger and RequestContextClasses #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ namespace MSAL { | |
this.ExpiresIn = expiresIn; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,106 @@ | ||
namespace MSAL { | ||
export enum LoggingLevel { | ||
ERROR, | ||
WARN, | ||
INFO, | ||
VERBOSE | ||
|
||
export interface ILoggerCallback { | ||
(level: LogLevel, message: string,containsPii:boolean): void; | ||
} | ||
|
||
export enum LogLevel { | ||
Error, | ||
Warning, | ||
Info, | ||
Verbose | ||
} | ||
|
||
export class Logger { | ||
static log(level: number, message: string, error: string): void { | ||
if (level <= Logging._logLevel) { | ||
var timestamp = new Date().toUTCString(); | ||
var formattedMessage = ''; | ||
if (Logging._correlationId) | ||
formattedMessage = timestamp + ':' + Logging._correlationId + '-' + this.libVersion() + '-' + LoggingLevel[level] + ' ' + message; | ||
else | ||
formattedMessage = timestamp + ':' + this.libVersion() + '-' + LoggingLevel[level] + ' ' + message; | ||
if (error) { | ||
formattedMessage += '\nstack:\n' + error; | ||
} | ||
|
||
if (Logging._loginCallback) | ||
Logging._loginCallback(formattedMessage); | ||
export class Logger {// Singleton Class | ||
private static _instance: Logger; | ||
private _correlationId: string; | ||
get correlationId(): string { return this._correlationId; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should have set property on the correlationID. |
||
set correlationId(correlationId: string) { | ||
this._correlationId = correlationId; | ||
}; | ||
private _level: LogLevel = LogLevel.Info; | ||
get level(): LogLevel { return this._level; } | ||
set level(logLevel: LogLevel) { | ||
if (LogLevel[logLevel]) { | ||
this._level = logLevel; | ||
} | ||
else throw new Error("Provide a valid value for level. Possibles range for logLevel is 0-3"); | ||
}; | ||
|
||
private _piiLoggingEnabled: boolean = false; | ||
get piiLoggingEnabled(): boolean { return this._piiLoggingEnabled; } | ||
set piiLoggingEnabled(piiLoggingEnabled: boolean) { | ||
this._piiLoggingEnabled = piiLoggingEnabled; | ||
}; | ||
|
||
private _localCallback: ILoggerCallback; | ||
get localCallback(): ILoggerCallback { return this._localCallback; } | ||
set localCallback(localCallback: ILoggerCallback) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set should fail is callback is already set. |
||
if (this.localCallback) { | ||
throw new Error("MSAL logging callback can only be set once per process and should never change once set."); | ||
} | ||
this._localCallback = localCallback; | ||
}; | ||
|
||
constructor(correlationId: string) { | ||
if (Logger._instance) { | ||
return Logger._instance; | ||
} | ||
this._correlationId = correlationId; | ||
Logger._instance = this; | ||
} | ||
|
||
private LogMessage(logMessage: string, logLevel: LogLevel, containsPii: boolean) { | ||
if ((logLevel > this.level) || (!this.piiLoggingEnabled && containsPii)) { | ||
return; | ||
} | ||
var timestamp = new Date().toUTCString(); | ||
var log: string = ''; | ||
if (!Utils.isEmpty(this.correlationId)) { | ||
log = timestamp + ':' + this._correlationId + '-' + Utils.GetLibraryVersion() + '-' + LogLevel[logLevel] + ' ' + logMessage; | ||
} | ||
else { | ||
log = timestamp + ':' + Utils.GetLibraryVersion() + '-' + LogLevel[logLevel] + ' ' + logMessage; | ||
} | ||
this.executeCallback(logLevel, log, containsPii); | ||
} | ||
|
||
executeCallback(level: LogLevel, message: string, containsPii: boolean) { | ||
if (this.localCallback) { | ||
this.localCallback(level, message, containsPii); | ||
} | ||
} | ||
|
||
error(message: string): void { | ||
this.LogMessage(message, LogLevel.Error, false); | ||
} | ||
|
||
errorPii(message: string): void { | ||
this.LogMessage(message, LogLevel.Error, true); | ||
} | ||
|
||
warning(message: string): void { | ||
this.LogMessage(message, LogLevel.Warning, false); | ||
} | ||
|
||
static error(message: string, error: string): void { | ||
this.log(LoggingLevel.ERROR, message, error); | ||
warningPii(message: string): void { | ||
this.LogMessage(message, LogLevel.Warning, true); | ||
} | ||
|
||
static warn(message: string): void { | ||
this.log(LoggingLevel.WARN, message, null); | ||
info(message: string): void { | ||
this.LogMessage(message, LogLevel.Info, false); | ||
} | ||
|
||
static info(message: string): void { | ||
this.log(LoggingLevel.INFO, message, null); | ||
infoPii(message: string): void { | ||
this.LogMessage(message, LogLevel.Info, true); | ||
} | ||
|
||
static verbose(message: string): void { | ||
this.log(LoggingLevel.VERBOSE, message, null); | ||
verbose(message: string): void { | ||
this.LogMessage(message, LogLevel.Verbose, false); | ||
} | ||
|
||
static libVersion(): string { | ||
return '1.0.0'; | ||
verbosePii(message: string): void { | ||
this.LogMessage(message, LogLevel.Verbose, true); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace MSAL {// Singleton Class | ||
export class RequestContext { | ||
private static _instance: RequestContext; | ||
private _correlationId: string; | ||
get correlationId(): string { return this._correlationId;} | ||
private _logger: Logger; | ||
get logger(): Logger { return this._logger; } | ||
|
||
constructor(correlationId: string) { | ||
if (RequestContext._instance) { | ||
return RequestContext._instance; | ||
} | ||
|
||
this._logger = new Logger(correlationId); | ||
this._correlationId = this._logger.correlationId; | ||
RequestContext._instance = this; | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace MSAL { | ||
export class User { | ||
export interface User { | ||
username: string; | ||
profile: any; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️ this change.