@vvlad1973/simple-logger 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,238 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+
6
+ <meta charset="utf-8">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1">
8
+ <title> SimpleLogger</title>
9
+
10
+ <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
11
+ <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
12
+ <script src="./build/entry.js"></script>
13
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
14
+ <!--[if lt IE 9]>
15
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
16
+ <![endif]-->
17
+ <link href="https://fonts.googleapis.com/css?family=Roboto:100,400,700|Inconsolata,700" rel="stylesheet">
18
+ <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
19
+ <link type="text/css" rel="stylesheet" href="https://jmblog.github.io/color-themes-for-google-code-prettify/themes/tomorrow-night.min.css">
20
+ <link type="text/css" rel="stylesheet" href="styles/app.min.css">
21
+ <link type="text/css" rel="stylesheet" href="styles/iframe.css">
22
+ <link type="text/css" rel="stylesheet" href="">
23
+ <script async defer src="https://buttons.github.io/buttons.js"></script>
24
+
25
+
26
+ </head>
27
+
28
+
29
+
30
+ <body class="layout small-header">
31
+ <div id="stickyNavbarOverlay"></div>
32
+
33
+
34
+ <div class="top-nav">
35
+ <div class="inner">
36
+ <a id="hamburger" role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
37
+ <span aria-hidden="true"></span>
38
+ <span aria-hidden="true"></span>
39
+ <span aria-hidden="true"></span>
40
+ </a>
41
+ <div class="logo">
42
+
43
+
44
+ </div>
45
+ <div class="menu">
46
+
47
+ <div class="navigation">
48
+ <a
49
+ href="index.html"
50
+ class="link"
51
+ >
52
+ Documentation
53
+ </a>
54
+
55
+
56
+
57
+ </div>
58
+ </div>
59
+ </div>
60
+ </div>
61
+ <div id="main">
62
+ <div
63
+ class="sidebar "
64
+ id="sidebarNav"
65
+ >
66
+
67
+ <nav>
68
+
69
+ <h2><a href="index.html">Documentation</a></h2><div class="category"><h3>Modules</h3><ul><li><a href="module-SimpleLogger.html">SimpleLogger</a></li></ul><h3>Classes</h3><ul><li><a href="module-SimpleLogger.SimpleLogger.html">SimpleLogger</a></li></ul></div>
70
+
71
+ </nav>
72
+ </div>
73
+ <div class="core" id="main-content-wrapper">
74
+ <div class="content">
75
+ <header class="page-title">
76
+ <p>Module</p>
77
+ <h1>SimpleLogger</h1>
78
+ </header>
79
+
80
+
81
+
82
+
83
+
84
+ <section>
85
+
86
+ <header>
87
+
88
+
89
+
90
+
91
+
92
+ </header>
93
+
94
+ <article>
95
+ <div class="container-overview">
96
+
97
+
98
+ <div class="description">A module for simple logging with various logging levels.</div>
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+ <dl class="details">
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+ <p class="tag-source">
150
+ <a href="simple-logger.ts.html" class="button">View Source</a>
151
+ <span>
152
+ <a href="simple-logger.ts.html">simple-logger.ts</a>, <a href="simple-logger.ts.html#line2">line 2</a>
153
+ </span>
154
+ </p>
155
+
156
+ </dl>
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+ </div>
182
+
183
+
184
+
185
+
186
+
187
+
188
+ <h3 class="subsection-title">Classes</h3>
189
+
190
+ <dl>
191
+ <dt><a href="module-SimpleLogger.SimpleLogger.html">SimpleLogger</a></dt>
192
+ <dd></dd>
193
+ </dl>
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+ </article>
210
+
211
+ </section>
212
+
213
+
214
+
215
+
216
+ </div>
217
+
218
+ <footer class="footer">
219
+ <div class="content has-text-centered">
220
+ <p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.3</a></p>
221
+ <p class="sidebar-created-by">
222
+ <a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
223
+ <a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
224
+ </p>
225
+ </div>
226
+ </footer>
227
+
228
+ </div>
229
+ <div id="side-nav" class="side-nav">
230
+ </div>
231
+ </div>
232
+ <script src="scripts/app.min.js"></script>
233
+ <script>PR.prettyPrint();</script>
234
+ <script src="scripts/linenumber.js"> </script>
235
+
236
+
237
+ </body>
238
+ </html>
@@ -0,0 +1 @@
1
+ "use strict";$().ready(function(){});var sidebarIsVisible=!1,toggleSidebar=function(e){var a=!(0<arguments.length&&void 0!==e)||e;$("#sidebarNav").toggleClass("sticky",a),$("#stickyNavbarOverlay").toggleClass("active",a),$("#hamburger").toggleClass("is-active"),sidebarIsVisible=a};$().ready(function(){$("#hamburger").click(function(){toggleSidebar(!sidebarIsVisible)}),$("#stickyNavbarOverlay").click(function(){sidebarIsVisible&&toggleSidebar(!1)})});var OFFSET=150;$().ready(function(){var o=$("#side-nav"),c=[];if($(".vertical-section").length||o.hide(),$(".vertical-section").each(function(e,a){var i=$(a),t=i.find("> h1").text();if(t){o.append($("<h3/>").text(t));var s=$("<ul></ul>");i.find(".members h4.name").each(function(e,a){var i=$(a),t=i.find(".code-name").clone().children().remove().end().text(),n=i.find("a").attr("href"),r=$('<a href="'.concat(n,'" />')).text(t);s.append($("<li></li>").append(r)),c.push({link:r,offset:i.offset().top})}),o.append(s)}else i.find(".members h4.name").each(function(e,a){var i=$(a),t=i.find(".code-name").clone().children().remove().end().text(),n=i.find("a").attr("href"),r=$('<a href="'.concat(n,'" />')).text(t);o.append(r),c.push({link:r,offset:i.offset().top})})}),!$.trim(o.text()))return o.hide();function e(){for(var e=n.scrollTop(),a=!1,i=c.length-1;0<=i;i--){var t=c[i];t.link.removeClass("is-active"),e+OFFSET>=t.offset?a?t.link.addClass("is-past"):(t.link.addClass("is-active"),a=!0):t.link.removeClass("is-past")}}var n=$("#main-content-wrapper");n.on("scroll",e),e(),c.forEach(function(e){e.link.click(function(){n.animate({scrollTop:e.offset-OFFSET+1},500)})})}),$().ready(function(){$("#sidebarNav a").each(function(e,a){var i=$(a).attr("href");window.location.pathname.match("/"+i)&&($(a).addClass("active"),$("#sidebarNav").scrollTop($(a).offset().top-150))})});
@@ -0,0 +1,26 @@
1
+ /*global document */
2
+
3
+ (function() {
4
+ var source = document.getElementsByClassName('prettyprint source linenums');
5
+ var i = 0;
6
+ var lineNumber = 0;
7
+ var lineId;
8
+ var lines;
9
+ var totalLines;
10
+ var anchorHash;
11
+
12
+ if (source && source[0]) {
13
+ anchorHash = document.location.hash.substring(1);
14
+ lines = source[0].getElementsByTagName('li');
15
+ totalLines = lines.length;
16
+
17
+ for (; i < totalLines; i++) {
18
+ lineNumber++;
19
+ lineId = 'line' + lineNumber;
20
+ lines[i].id = lineId;
21
+ if (lineId === anchorHash) {
22
+ lines[i].className += ' selected';
23
+ }
24
+ }
25
+ }
26
+ })();
@@ -0,0 +1,39 @@
1
+ (function() {
2
+ const input = document.querySelector('#search')
3
+ const targets = [ ...document.querySelectorAll('#sidebarNav li')]
4
+ input.addEventListener('keyup', () => {
5
+ // loop over each targets and hide the not corresponding ones
6
+ targets.forEach(target => {
7
+ if (!target.innerText.toLowerCase().includes(input.value.toLowerCase())) {
8
+ target.style.display = 'none'
9
+
10
+ /**
11
+ * Detects an empty list
12
+ * Remove the list and the list's title if the list is not displayed
13
+ */
14
+ const list = [...target.parentNode.childNodes].filter( elem => elem.style.display !== 'none')
15
+
16
+ if (!list.length) {
17
+ target.parentNode.style.display = 'none'
18
+ target.parentNode.previousSibling.style.display = 'none'
19
+ }
20
+
21
+ /**
22
+ * Detects empty category
23
+ * Remove the entire category if no item is displayed
24
+ */
25
+ const category = [...target.parentNode.parentNode.childNodes]
26
+ .filter( elem => elem.tagName !== 'H2' && elem.style.display !== 'none')
27
+
28
+ if (!category.length) {
29
+ target.parentNode.parentNode.style.display = 'none'
30
+ }
31
+ } else {
32
+ target.parentNode.style.display = 'block'
33
+ target.parentNode.previousSibling.style.display = 'block'
34
+ target.parentNode.parentNode.style.display = 'block'
35
+ target.style.display = 'block'
36
+ }
37
+ })
38
+ })
39
+ })()
@@ -0,0 +1,303 @@
1
+
2
+
3
+ <!DOCTYPE html>
4
+ <html lang="en">
5
+
6
+ <head>
7
+
8
+ <meta charset="utf-8">
9
+ <meta name="viewport" content="width=device-width, initial-scale=1">
10
+ <title> simple-logger.ts</title>
11
+
12
+ <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
13
+ <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
14
+ <script src="./build/entry.js"></script>
15
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
16
+ <!--[if lt IE 9]>
17
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
18
+ <![endif]-->
19
+ <link href="https://fonts.googleapis.com/css?family=Roboto:100,400,700|Inconsolata,700" rel="stylesheet">
20
+ <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
21
+ <link type="text/css" rel="stylesheet" href="https://jmblog.github.io/color-themes-for-google-code-prettify/themes/tomorrow-night.min.css">
22
+ <link type="text/css" rel="stylesheet" href="styles/app.min.css">
23
+ <link type="text/css" rel="stylesheet" href="styles/iframe.css">
24
+ <link type="text/css" rel="stylesheet" href="">
25
+ <script async defer src="https://buttons.github.io/buttons.js"></script>
26
+
27
+
28
+ </head>
29
+
30
+
31
+
32
+ <body class="layout small-header">
33
+ <div id="stickyNavbarOverlay"></div>
34
+
35
+
36
+ <div class="top-nav">
37
+ <div class="inner">
38
+ <a id="hamburger" role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
39
+ <span aria-hidden="true"></span>
40
+ <span aria-hidden="true"></span>
41
+ <span aria-hidden="true"></span>
42
+ </a>
43
+ <div class="logo">
44
+
45
+
46
+ </div>
47
+ <div class="menu">
48
+
49
+ <div class="navigation">
50
+ <a
51
+ href="index.html"
52
+ class="link"
53
+ >
54
+ Documentation
55
+ </a>
56
+
57
+
58
+
59
+ </div>
60
+ </div>
61
+ </div>
62
+ </div>
63
+ <div id="main">
64
+ <div
65
+ class="sidebar "
66
+ id="sidebarNav"
67
+ >
68
+
69
+ <nav>
70
+
71
+ <h2><a href="index.html">Documentation</a></h2><div class="category"><h3>Modules</h3><ul><li><a href="module-SimpleLogger.html">SimpleLogger</a></li></ul><h3>Classes</h3><ul><li><a href="module-SimpleLogger.SimpleLogger.html">SimpleLogger</a></li></ul></div>
72
+
73
+ </nav>
74
+ </div>
75
+ <div class="core" id="main-content-wrapper">
76
+ <div class="content">
77
+ <header class="page-title">
78
+ <p>Source</p>
79
+ <h1>simple-logger.ts</h1>
80
+ </header>
81
+
82
+
83
+
84
+
85
+
86
+ <section>
87
+ <article>
88
+ <pre class="prettyprint source linenums"><code>/**
89
+ * A module for simple logging with various logging levels.
90
+ * @module SimpleLogger
91
+ */
92
+
93
+ import { getCallerName } from 'vvlad1973-utils';
94
+
95
+ export type ExternalLogger = {
96
+ trace?: LogFn;
97
+ debug?: LogFn;
98
+ info?: LogFn;
99
+ warn?: LogFn;
100
+ error?: LogFn;
101
+ fatal?: LogFn;
102
+ silent?: LogFn;
103
+ level?: string;
104
+ };
105
+
106
+ interface LogFn {
107
+ &lt;T extends object>(obj: T, msg?: string, ...args: any[]): void;
108
+ (obj: unknown, msg?: string, ...args: any[]): void;
109
+ (msg: string, ...args: any[]): void;
110
+ }
111
+
112
+ const loggerLevels = [
113
+ 'trace',
114
+ 'debug',
115
+ 'info',
116
+ 'warn',
117
+ 'error',
118
+ 'fatal',
119
+ 'silent',
120
+ ] as const;
121
+ type LoggerLevel = (typeof loggerLevels)[number];
122
+ /**
123
+ * A simple logger class with various logging levels.
124
+ * @class
125
+ * @param {string} [level='info'] - Initial logging level.
126
+ * @param {ExternalLogger | undefined | null} [externalLogger=null] - Optional external logger to use.
127
+ */
128
+ export class SimpleLogger {
129
+ #levels = loggerLevels;
130
+ #currentLevel: number = 2;
131
+ #logger: ExternalLogger | Console;
132
+
133
+ constructor(
134
+ level: LoggerLevel = 'info',
135
+ externalLogger: ExternalLogger | undefined | null = null
136
+ ) {
137
+ this.#logger =
138
+ externalLogger &amp;&amp; this.isValidLogger(externalLogger)
139
+ ? externalLogger
140
+ : (console as ExternalLogger);
141
+ this.setLevel(level);
142
+ }
143
+
144
+ trace: LogFn = (...args: any[]) => {};
145
+ debug: LogFn = (...args: any[]) => {};
146
+ info: LogFn = (...args: any[]) => {};
147
+ warn: LogFn = (...args: any[]) => {};
148
+ error: LogFn = (...args: any[]) => {};
149
+ fatal: LogFn = (...args: any[]) => {};
150
+ silent: LogFn = (...args: any[]) => {};
151
+
152
+ /**
153
+ * Updates the logging methods based on the current logging level.
154
+ * @private
155
+ */
156
+ private updateLoggingMethods() {
157
+ try {
158
+ const noop = (...args: any[]) => {};
159
+ this.trace =
160
+ this.#currentLevel &lt;= 0 ? this.getExternalLoggerMethod('trace') : noop;
161
+ this.debug =
162
+ this.#currentLevel &lt;= 1 ? this.getExternalLoggerMethod('debug') : noop;
163
+ this.info =
164
+ this.#currentLevel &lt;= 2 ? this.getExternalLoggerMethod('info') : noop;
165
+ this.warn =
166
+ this.#currentLevel &lt;= 3 ? this.getExternalLoggerMethod('warn') : noop;
167
+ this.error =
168
+ this.#currentLevel &lt;= 4 ? this.getExternalLoggerMethod('error') : noop;
169
+ this.fatal =
170
+ this.#currentLevel &lt;= 5
171
+ ? this.getExternalLoggerMethod('fatal', 'error')
172
+ : noop;
173
+ this.silent = noop;
174
+ } catch (error) {
175
+ console.error(error);
176
+ }
177
+ }
178
+
179
+ /**
180
+ * Checks if the provided logger is valid.
181
+ * @private
182
+ * @param {any} logger - The logger to check.
183
+ * @returns {boolean} - True if the logger is valid, otherwise false.
184
+ */
185
+ private isValidLogger(logger: any): boolean {
186
+ return loggerLevels.every((method) => {
187
+ return typeof logger[method] === 'function';
188
+ });
189
+ }
190
+
191
+ /**
192
+ * Gets the external logger method for a specific level.
193
+ * @param {string} method - The logger method to get.
194
+ * @param {string} [fallbackMethod] - The fallback logger method to get.
195
+ * @returns {LoggerMethod} - The logger method or a no-op function.
196
+ * @private
197
+ */
198
+ private getExternalLoggerMethod(
199
+ method: keyof ExternalLogger,
200
+ fallbackMethod?: keyof ExternalLogger
201
+ ): LogFn {
202
+ try {
203
+ const loggerMethod = (this.#logger as ExternalLogger)[method];
204
+
205
+ if (typeof loggerMethod === 'function') {
206
+ return loggerMethod.bind(this.#logger);
207
+ } else if (typeof fallbackMethod !== 'undefined') {
208
+ const fallbackLoggerMethod = (this.#logger as ExternalLogger)[
209
+ fallbackMethod
210
+ ];
211
+
212
+ if (typeof fallbackLoggerMethod === 'function') {
213
+ return fallbackLoggerMethod.bind(this.#logger);
214
+ }
215
+ }
216
+ return () => {};
217
+ } catch (error) {
218
+ console.error(error);
219
+ return () => {};
220
+ }
221
+ }
222
+
223
+ /**
224
+ * Sets the logging level.
225
+ * @param {string} level - The logging level to set.
226
+ */
227
+ public setLevel(level: string) {
228
+ const index = this.#levels.indexOf(level.toLowerCase() as LoggerLevel);
229
+ if (index !== -1) {
230
+ this.#currentLevel = index;
231
+
232
+ if (typeof (this.#logger as ExternalLogger).level === 'string') {
233
+ (this.#logger as ExternalLogger).level = level;
234
+ }
235
+ this.updateLoggingMethods();
236
+ }
237
+ }
238
+
239
+ /**
240
+ * Sets an external logger to be used.
241
+ * @param {ExternalLogger | undefined | null} logger - The external logger to set.
242
+ */
243
+ public setExternalLogger(logger: ExternalLogger | undefined | null) {
244
+ if (logger &amp;&amp; this.isValidLogger(logger)) {
245
+ this.#logger = logger;
246
+ // this.setLevel(this.#levels[this.#currentLevel]);
247
+ this.updateLoggingMethods();
248
+ } else if (!logger) {
249
+ this.#logger = console;
250
+ this.updateLoggingMethods();
251
+ }
252
+ }
253
+
254
+ /**
255
+ * Logs the start of a function.
256
+ * @param {string} [functionName] - Optional function name to log.
257
+ */
258
+ public logFunctionStart(functionName?: string) {
259
+ const callerName = functionName || getCallerName();
260
+ this.trace(`Function start: ${callerName}`);
261
+ }
262
+
263
+ /**
264
+ * Logs the end of a function.
265
+ * @param {string} [functionName] - Optional function name to log.
266
+ */
267
+ public logFunctionEnd(functionName?: string) {
268
+ const callerName = functionName || getCallerName();
269
+ this.trace(`Function end: ${callerName}`);
270
+ }
271
+ }
272
+
273
+ export default SimpleLogger;
274
+ </code></pre>
275
+ </article>
276
+ </section>
277
+
278
+
279
+
280
+
281
+ </div>
282
+
283
+ <footer class="footer">
284
+ <div class="content has-text-centered">
285
+ <p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.3</a></p>
286
+ <p class="sidebar-created-by">
287
+ <a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
288
+ <a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
289
+ </p>
290
+ </div>
291
+ </footer>
292
+
293
+ </div>
294
+ <div id="side-nav" class="side-nav">
295
+ </div>
296
+ </div>
297
+ <script src="scripts/app.min.js"></script>
298
+ <script>PR.prettyPrint();</script>
299
+ <script src="scripts/linenumber.js"> </script>
300
+
301
+
302
+ </body>
303
+ </html>