1 | export function ConsoleListener(prefix, colors) {
|
2 | return new _ConsoleListener(prefix, colors);
|
3 | }
|
4 | function withColor(msg, color) {
|
5 | if (typeof color === "undefined") {
|
6 | console.log(msg);
|
7 | }
|
8 | else {
|
9 | console.log(`%c${msg}`, `color:${color}`);
|
10 | }
|
11 | }
|
12 | /**
|
13 | * Formats the message
|
14 | *
|
15 | * @param entry The information to format into a string
|
16 | */
|
17 | function entryToString(entry, prefix) {
|
18 | const msg = [];
|
19 | if (prefix.length > 0) {
|
20 | msg.push(`${prefix} -`);
|
21 | }
|
22 | msg.push(entry.message);
|
23 | if (entry.data !== undefined) {
|
24 | try {
|
25 | msg.push("Data: " + JSON.stringify(entry.data));
|
26 | }
|
27 | catch (e) {
|
28 | msg.push(`Data: Error in stringify of supplied data ${e}`);
|
29 | }
|
30 | }
|
31 | return msg.join(" ");
|
32 | }
|
33 | // index order matters, this is a lookup table based on the corresponding LogLevel value
|
34 | const colorProps = ["verbose", "info", "warning", "error"];
|
35 | /**
|
36 | * Implementation of LogListener which logs to the console
|
37 | *
|
38 | */
|
39 | class _ConsoleListener {
|
40 | /**
|
41 | * Makes a new one
|
42 | *
|
43 | * @param prefix Optional text to include at the start of all messages (useful for filtering)
|
44 | * @param colors Optional text color settings
|
45 | */
|
46 | constructor(_prefix = "", _colors = {}) {
|
47 | this._prefix = _prefix;
|
48 | this._colors = _colors;
|
49 | }
|
50 | /**
|
51 | * Any associated data that a given logging listener may choose to log or ignore
|
52 | *
|
53 | * @param entry The information to be logged
|
54 | */
|
55 | log(entry) {
|
56 | withColor(entryToString(entry, this._prefix), this._colors[colorProps[entry.level]]);
|
57 | }
|
58 | }
|
59 | export function FunctionListener(impl) {
|
60 | return new _FunctionListener(impl);
|
61 | }
|
62 | /**
|
63 | * Implementation of LogListener which logs to the supplied function
|
64 | *
|
65 | */
|
66 | class _FunctionListener {
|
67 | /**
|
68 | * Creates a new instance of the FunctionListener class
|
69 | *
|
70 | * @constructor
|
71 | * @param method The method to which any logging data will be passed
|
72 | */
|
73 | constructor(method) {
|
74 | this.method = method;
|
75 | }
|
76 | /**
|
77 | * Any associated data that a given logging listener may choose to log or ignore
|
78 | *
|
79 | * @param entry The information to be logged
|
80 | */
|
81 | log(entry) {
|
82 | this.method(entry);
|
83 | }
|
84 | }
|
85 | //# sourceMappingURL=listeners.js.map |
\ | No newline at end of file |