-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6de448
commit a81acb0
Showing
3 changed files
with
200 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
export * from "./listeners.js"; | ||
/** | ||
* A set of logging levels | ||
*/ | ||
export var LogLevel; | ||
(function (LogLevel) { | ||
LogLevel[LogLevel["Verbose"] = 0] = "Verbose"; | ||
LogLevel[LogLevel["Info"] = 1] = "Info"; | ||
LogLevel[LogLevel["Warning"] = 2] = "Warning"; | ||
LogLevel[LogLevel["Error"] = 3] = "Error"; | ||
LogLevel[LogLevel["Off"] = 99] = "Off"; | ||
})(LogLevel || (LogLevel = {})); | ||
const _subscribers = []; | ||
let _activeLogLevel = 2 /* Warning */; | ||
/** | ||
* Class used to subscribe ILogListener and log messages throughout an application | ||
* | ||
*/ | ||
export class Logger { | ||
/** | ||
* Gets or sets the active log level to apply for log filtering | ||
*/ | ||
static get activeLogLevel() { | ||
return _activeLogLevel; | ||
} | ||
static set activeLogLevel(value) { | ||
_activeLogLevel = value; | ||
} | ||
/** | ||
* Adds ILogListener instances to the set of subscribed listeners | ||
* | ||
* @param listeners One or more listeners to subscribe to this log | ||
*/ | ||
static subscribe(...listeners) { | ||
_subscribers.push(...listeners); | ||
} | ||
/** | ||
* Clears the subscribers collection, returning the collection before modification | ||
*/ | ||
static clearSubscribers() { | ||
const s = _subscribers.slice(0); | ||
_subscribers.length = 0; | ||
return s; | ||
} | ||
/** | ||
* Gets the current subscriber count | ||
*/ | ||
static get count() { | ||
return _subscribers.length; | ||
} | ||
/** | ||
* Writes the supplied string to the subscribed listeners | ||
* | ||
* @param message The message to write | ||
* @param level [Optional] if supplied will be used as the level of the entry (Default: LogLevel.Info) | ||
*/ | ||
static write(message, level = 1 /* Info */) { | ||
Logger.log({ level: level, message: message }); | ||
} | ||
/** | ||
* Writes the supplied string to the subscribed listeners | ||
* | ||
* @param json The json object to stringify and write | ||
* @param level [Optional] if supplied will be used as the level of the entry (Default: LogLevel.Info) | ||
*/ | ||
static writeJSON(json, level = 1 /* Info */) { | ||
Logger.write(JSON.stringify(json), level); | ||
} | ||
/** | ||
* Logs the supplied entry to the subscribed listeners | ||
* | ||
* @param entry The message to log | ||
*/ | ||
static log(entry) { | ||
if (entry !== undefined && Logger.activeLogLevel <= entry.level) { | ||
_subscribers.map(subscriber => subscriber.log(entry)); | ||
} | ||
} | ||
/** | ||
* Logs an error object to the subscribed listeners | ||
* | ||
* @param err The error object | ||
*/ | ||
static error(err) { | ||
Logger.log({ data: err, level: 3 /* Error */, message: err.message }); | ||
} | ||
} | ||
export function PnPLogging(activeLevel) { | ||
return (instance) => { | ||
instance.on.log(function (message, level) { | ||
if (activeLevel <= level) { | ||
_subscribers.map(subscriber => subscriber.log({ level, message })); | ||
} | ||
}); | ||
return instance; | ||
}; | ||
} |
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,101 @@ | ||
export function ConsoleListener(prefix, colors) { | ||
return new _ConsoleListener(prefix, colors); | ||
} | ||
function withColor(msg, color, logMethod) { | ||
if (typeof color === "undefined") { | ||
logMethod(msg); | ||
} | ||
else { | ||
logMethod(`%c${msg}`, `color:${color}`); | ||
} | ||
} | ||
/** | ||
* Formats the message | ||
* | ||
* @param entry The information to format into a string | ||
*/ | ||
function entryToString(entry, prefix) { | ||
const msg = []; | ||
if (prefix.length > 0) { | ||
msg.push(`${prefix} -`); | ||
} | ||
msg.push(entry.message); | ||
if (entry.data !== undefined) { | ||
try { | ||
msg.push("Data: " + JSON.stringify(entry.data)); | ||
} | ||
catch (e) { | ||
msg.push(`Data: Error in stringify of supplied data ${e}`); | ||
} | ||
} | ||
return msg.join(" "); | ||
} | ||
// index order matters, this is a lookup table based on the corresponding LogLevel value | ||
const colorProps = ["verbose", "info", "warning", "error"]; | ||
/** | ||
* Implementation of LogListener which logs to the console | ||
* | ||
*/ | ||
class _ConsoleListener { | ||
/** | ||
* Makes a new one | ||
* | ||
* @param prefix Optional text to include at the start of all messages (useful for filtering) | ||
* @param colors Optional text color settings | ||
*/ | ||
constructor(_prefix = "", _colors = {}) { | ||
this._prefix = _prefix; | ||
this._colors = _colors; | ||
} | ||
/** | ||
* Any associated data that a given logging listener may choose to log or ignore | ||
* | ||
* @param entry The information to be logged | ||
*/ | ||
log(entry) { | ||
let logMethod = console.log; | ||
switch (entry.level) { | ||
case 3 /* Error */: | ||
logMethod = console.error; | ||
break; | ||
case 2 /* Warning */: | ||
logMethod = console.warn; | ||
break; | ||
case 0 /* Verbose */: | ||
logMethod = console.debug; | ||
break; | ||
case 1 /* Info */: | ||
logMethod = console.info; | ||
break; | ||
default: | ||
logMethod = console.log; | ||
} | ||
withColor(entryToString(entry, this._prefix), this._colors[colorProps[entry.level]], logMethod); | ||
} | ||
} | ||
export function FunctionListener(impl) { | ||
return new _FunctionListener(impl); | ||
} | ||
/** | ||
* Implementation of LogListener which logs to the supplied function | ||
* | ||
*/ | ||
class _FunctionListener { | ||
/** | ||
* Creates a new instance of the FunctionListener class | ||
* | ||
* @constructor | ||
* @param method The method to which any logging data will be passed | ||
*/ | ||
constructor(method) { | ||
this.method = method; | ||
} | ||
/** | ||
* Any associated data that a given logging listener may choose to log or ignore | ||
* | ||
* @param entry The information to be logged | ||
*/ | ||
log(entry) { | ||
this.method(entry); | ||
} | ||
} |