UNPKG

2.92 kBJavaScriptView Raw
1export * from "./listeners.js";
2/**
3 * A set of logging levels
4 */
5export var LogLevel;
6(function (LogLevel) {
7 LogLevel[LogLevel["Verbose"] = 0] = "Verbose";
8 LogLevel[LogLevel["Info"] = 1] = "Info";
9 LogLevel[LogLevel["Warning"] = 2] = "Warning";
10 LogLevel[LogLevel["Error"] = 3] = "Error";
11 LogLevel[LogLevel["Off"] = 99] = "Off";
12})(LogLevel || (LogLevel = {}));
13const _subscribers = [];
14let _activeLogLevel = 2 /* Warning */;
15/**
16 * Class used to subscribe ILogListener and log messages throughout an application
17 *
18 */
19export class Logger {
20 /**
21 * Gets or sets the active log level to apply for log filtering
22 */
23 static get activeLogLevel() {
24 return _activeLogLevel;
25 }
26 static set activeLogLevel(value) {
27 _activeLogLevel = value;
28 }
29 /**
30 * Adds ILogListener instances to the set of subscribed listeners
31 *
32 * @param listeners One or more listeners to subscribe to this log
33 */
34 static subscribe(...listeners) {
35 _subscribers.push(...listeners);
36 }
37 /**
38 * Clears the subscribers collection, returning the collection before modification
39 */
40 static clearSubscribers() {
41 const s = _subscribers.slice(0);
42 _subscribers.length = 0;
43 return s;
44 }
45 /**
46 * Gets the current subscriber count
47 */
48 static get count() {
49 return _subscribers.length;
50 }
51 /**
52 * Writes the supplied string to the subscribed listeners
53 *
54 * @param message The message to write
55 * @param level [Optional] if supplied will be used as the level of the entry (Default: LogLevel.Info)
56 */
57 static write(message, level = 1 /* Info */) {
58 Logger.log({ level: level, message: message });
59 }
60 /**
61 * Writes the supplied string to the subscribed listeners
62 *
63 * @param json The json object to stringify and write
64 * @param level [Optional] if supplied will be used as the level of the entry (Default: LogLevel.Info)
65 */
66 static writeJSON(json, level = 1 /* Info */) {
67 Logger.write(JSON.stringify(json), level);
68 }
69 /**
70 * Logs the supplied entry to the subscribed listeners
71 *
72 * @param entry The message to log
73 */
74 static log(entry) {
75 if (entry !== undefined && Logger.activeLogLevel <= entry.level) {
76 _subscribers.map(subscriber => subscriber.log(entry));
77 }
78 }
79 /**
80 * Logs an error object to the subscribed listeners
81 *
82 * @param err The error object
83 */
84 static error(err) {
85 Logger.log({ data: err, level: 3 /* Error */, message: err.message });
86 }
87}
88export function PnPLogging(activeLevel) {
89 return (instance) => {
90 instance.on.log(function (message, level) {
91 if (activeLevel <= level) {
92 _subscribers.map(subscriber => subscriber.log({ level, message }));
93 }
94 });
95 return instance;
96 };
97}
98//# sourceMappingURL=index.js.map
\No newline at end of file