Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 7d92c83

Browse files
committed
added more comments
1 parent 727b42c commit 7d92c83

File tree

1 file changed

+154
-69
lines changed

1 file changed

+154
-69
lines changed

packages/logger/src/logger.ts

Lines changed: 154 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ export class Logger {
66
// Key/Name identifier of the Logger
77
public key?: LoggerKey;
88

9-
// Whether the Logger is active and can log
9+
// Whether the Logger is active and can log something
1010
public isActive: boolean;
11+
1112
// Registered Logger Categories (type of log messages)
1213
public loggerCategories: { [key: string]: LoggerCategoryInterface } = {};
13-
// Registered watcher callback
14+
15+
// Registered watcher callbacks
1416
public watchers: {
1517
[key: string]: LoggerWatcherConfigInterface;
1618
} = {};
@@ -22,23 +24,22 @@ export class Logger {
2224
* @public
2325
* @param config - Configuration object
2426
*/
25-
constructor(config: LoggerConfig = {}) {
26-
let _config = typeof config === 'function' ? config(this) : config;
27-
_config = defineConfig(_config, {
27+
constructor(config: CreateLoggerConfigInterface = {}) {
28+
config = defineConfig(config, {
2829
prefix: '',
2930
allowedTags: [],
3031
canUseCustomStyles: true,
3132
active: true,
3233
level: 0,
3334
timestamp: false,
3435
});
35-
this.isActive = _config.active as any;
36+
this.isActive = config.active as any;
3637
this.config = {
37-
timestamp: _config.timestamp as any,
38-
prefix: _config.prefix as any,
39-
canUseCustomStyles: _config.canUseCustomStyles as any,
40-
level: _config.level as any,
41-
allowedTags: _config.allowedTags as any,
38+
timestamp: config.timestamp as any,
39+
prefix: config.prefix as any,
40+
canUseCustomStyles: config.canUseCustomStyles as any,
41+
level: config.level as any,
42+
allowedTags: config.allowedTags as any,
4243
};
4344

4445
// Assign default Logger categories to the Logger
@@ -81,7 +82,7 @@ export class Logger {
8182
}
8283

8384
/**
84-
* Default log level of the Logger.
85+
* Default log levels of the Logger.
8586
*
8687
* The log level determines which type of logs can be logged.
8788
*
@@ -102,48 +103,53 @@ export class Logger {
102103
/**
103104
* Lays the foundation for a conditional log.
104105
*
106+
* logger.if.something.do_whatever (e.g. logger.if.tag('jeff').log('hello jeff'))
107+
*
105108
* @public
106109
*/
107110
public get if() {
108111
return {
109112
/**
110-
* Only executes the following action
111-
* if all given tags are allowed to be logged.
113+
* Only performs the following action (tag().followingAction)
114+
* if all specified tags are allowed to be logged.
112115
*
113-
* @internal
114-
* @param tags - Tags to be allowed.
116+
* @public
117+
* @param tags - Tags required to perform the following action.
115118
*/
116119
tag: (tags: string[]) => this.tag(tags),
117120
};
118121
}
119122

120123
/**
121-
* Only executes the following action
122-
* if all given tags are allowed to be logged.
124+
* Only performs the following action (tag().followingAction)
125+
* if all specified tags are allowed to be logged.
123126
*
124127
* @internal
125-
* @param tags - Tags to be allowed.
128+
* @param tags - Tags required to perform the following action.
126129
*/
127130
private tag(tags: string[]): DefaultLogMethodsInterface {
128131
return this.logIfCondition(includesArray(this.config.allowedTags, tags));
129132
}
130133

131134
/**
132-
* Returns an array of the default log methods if the specified condition is true.
135+
* Returns an object containing the default log methods
136+
* (log, debug, table, info, success, warn error)
137+
* if the specified condition is true.
133138
*
134-
* @param condition
139+
* @internal
140+
* @param condition - Condition
135141
* @private
136142
*/
137143
private logIfCondition(condition: boolean): DefaultLogMethodsInterface {
138144
const defaultLoggerCategories = Object.keys(
139145
Logger.level
140146
).map((loggerCategory) => loggerCategory.toLowerCase());
141147

142-
// Build object representing taggable log methods
148+
// Build object based on the default log categories
143149
const finalObject: DefaultLogMethodsInterface = {} as any;
144150
for (const loggerCategory of defaultLoggerCategories) {
145151
finalObject[loggerCategory] = condition
146-
? (...data) => this[loggerCategory](...data)
152+
? (...data) => this[loggerCategory](...data) // Wrapping into method to preserve the this scope of the Logger
147153
: () => {
148154
/* do nothing */
149155
};
@@ -246,20 +252,20 @@ export class Logger {
246252
* Prints a log message based on the specified logger category.
247253
*
248254
* @public
249-
* @param loggerCategory - Logger category to be logged.
255+
* @param loggerCategory - Key/Name identifier of the Logger category.
250256
* @param data - Data to be logged.
251257
*/
252258
public custom(loggerCategory: string, ...data: any[]) {
253259
this.invokeConsole(data, loggerCategory, 'log');
254260
}
255261

256262
/**
257-
* Internal method for logging the data
258-
* into the console based on the specified logger category.
263+
* Internal method for logging the specified data
264+
* into the 'console' based on the provided logger category.
259265
*
260266
* @internal
261267
* @param data - Data to be logged.
262-
* @param loggerCategoryKey - Key/Name of the Logger Category.
268+
* @param loggerCategoryKey - Key/Name identifier of the Logger category.
263269
* @param consoleLogType - What type of log to be logged (console[consoleLogType]).
264270
*/
265271
private invokeConsole(
@@ -284,7 +290,7 @@ export class Logger {
284290
return prefix;
285291
};
286292

287-
// Add built log prefix to the to log data
293+
// Add built log prefix to the data array
288294
if (typeof data[0] === 'string')
289295
data[0] = buildPrefix().concat(' ').concat(data[0]);
290296
else data.unshift(buildPrefix());
@@ -300,9 +306,12 @@ export class Logger {
300306
// Init custom styling provided by the Logger category
301307
if (this.config.canUseCustomStyles && loggerCategory.customStyle) {
302308
const newLogs: any[] = [];
303-
let didStyle = false; // Because only one string part of a log can be styled
309+
let didStyle = false;
304310
for (const log of data) {
305-
if (!didStyle && typeof log === 'string') {
311+
if (
312+
!didStyle && // Because only one string part of a log can be styled
313+
typeof log === 'string'
314+
) {
306315
newLogs.push(`%c${log}`);
307316
newLogs.push(loggerCategory.customStyle);
308317
didStyle = true;
@@ -330,12 +339,13 @@ export class Logger {
330339
* @public
331340
* @param loggerCategory - Logger category to be added to the Logger.
332341
*/
333-
public createLoggerCategory(loggerCategory: LoggerCategoryInterface) {
342+
public createLoggerCategory(loggerCategory: CreateLoggerCategoryInterface) {
334343
loggerCategory = {
335344
prefix: '',
345+
level: 0,
336346
...loggerCategory,
337347
};
338-
this.loggerCategories[loggerCategory.key] = loggerCategory;
348+
this.loggerCategories[loggerCategory.key] = loggerCategory as any;
339349
}
340350

341351
/**
@@ -390,54 +400,67 @@ export class Logger {
390400
export type LoggerCategoryKey = string | number;
391401
export type LoggerKey = string | number;
392402

393-
/**
394-
* @param key - Key/Name of Logger Category
395-
* @param customStyle - Css Styles that get applied to the Logs
396-
* @param prefix - Prefix that gets written before each Log of this Category
397-
* @param level - Until which Level this Logger Category gets logged
398-
*/
399-
export interface LoggerCategoryInterface {
400-
key: LoggerCategoryKey;
401-
customStyle?: string;
402-
prefix?: string;
403-
level: number;
404-
}
405-
406-
/**
407-
* @param prefix - Prefix that gets written before each log of this Logger
408-
* @param canUseCustomStyles - If custom Styles can be applied to the Logs
409-
* @param level - Handles which Logger Categories can be Logged
410-
* @param timestamp - Timestamp that ges written before each log of this Logger
411-
*/
412403
export interface LoggerConfigInterface {
404+
/**
405+
* Prefix to be added before each log message.
406+
* @default ''
407+
*/
413408
prefix: string;
409+
/**
410+
* Whether custom styles can be applied to the log messages.
411+
* @default true
412+
*/
414413
canUseCustomStyles: boolean;
414+
/**
415+
* Handles which Logger categories (log message types) can be logged.
416+
* @default 0
417+
*/
415418
level: number;
419+
/**
420+
* Whether to append the current timestamp before each log message.
421+
* @default false
422+
*/
416423
timestamp: boolean;
424+
/**
425+
* Array of tags that must be included in a conditional log to be logged.
426+
* @default []
427+
*/
417428
allowedTags: LoggerKey[];
418429
}
419430

420-
/**
421-
* @param prefix - Prefix that gets written before each log of this Logger
422-
* @param allowedTags - Only Logs that, contains the allowed Tags or have no Tag get logged
423-
* @param canUseCustomStyles - If custom Styles can be applied to the Logs
424-
* @param active - If Logger is active
425-
* @param level - Handles which Logger Categories can be Logged
426-
* @param timestamp - Timestamp that ges written before each log of this Logger
427-
*/
428431
export interface CreateLoggerConfigInterface {
432+
/**
433+
* Whether the Logger is allowed to log a message.
434+
* @default true
435+
*/
436+
active?: boolean;
437+
/**
438+
* Prefix to be added before each log message.
439+
* @default ''
440+
*/
429441
prefix?: string;
430-
allowedTags?: LoggerKey[];
442+
/**
443+
* Whether custom styles can be applied to the log messages.
444+
* @default true
445+
*/
431446
canUseCustomStyles?: boolean;
432-
active?: boolean;
447+
/**
448+
* Handles which Logger categories (log message types) can be logged.
449+
* @default 0
450+
*/
433451
level?: number;
452+
/**
453+
* Whether to append the current timestamp before each log message.
454+
* @default false
455+
*/
434456
timestamp?: boolean;
457+
/**
458+
* Array of tags that must be included in a conditional log to be logged.
459+
* @default []
460+
*/
461+
allowedTags?: LoggerKey[];
435462
}
436463

437-
export type LoggerConfig =
438-
| CreateLoggerConfigInterface
439-
| ((logger: Logger) => CreateLoggerConfigInterface);
440-
441464
export type ConsoleLogType =
442465
| 'log'
443466
| 'warn'
@@ -446,22 +469,84 @@ export type ConsoleLogType =
446469
| 'info'
447470
| 'debug';
448471

472+
/**
473+
* @param key - Key/Name of Logger Category
474+
* @param customStyle - Css Styles that get applied to the Logs
475+
* @param prefix - Prefix that gets written before each Log of this Category
476+
* @param level - Until which Level this Logger Category gets logged
477+
*/
478+
export interface LoggerCategoryInterface {
479+
/**
480+
* Key/Name identifier of the Logger category.
481+
* @default undefined
482+
*/
483+
key: LoggerCategoryKey;
484+
/**
485+
* CSS Styles to be applied to the log messages of this category.
486+
* @default ''
487+
*/
488+
customStyle?: string;
489+
/**
490+
* Prefix to be written before each Log message of this category.
491+
* @default ''
492+
*/
493+
prefix: string;
494+
/**
495+
* From which level the log messages of this category can be logged.
496+
* @default 0
497+
*/
498+
level: number;
499+
}
500+
501+
export interface CreateLoggerCategoryInterface {
502+
/**
503+
* Key/Name identifier of the Logger category.
504+
* @default undefined
505+
*/
506+
key: LoggerCategoryKey;
507+
/**
508+
* CSS Styles to be applied to the log messages of this category.
509+
* @default ''
510+
*/
511+
customStyle?: string;
512+
/**
513+
* Prefix to be written before each Log message of this category.
514+
* @default ''
515+
*/
516+
prefix?: string;
517+
/**
518+
* From which level the log messages of this category can be logged.
519+
* @default 0
520+
*/
521+
level?: number;
522+
}
523+
449524
export type LoggerWatcherCallback = (
450525
loggerCategory: LoggerCategoryInterface,
451526
data: any[]
452527
) => void;
453528

454-
/**
455-
* @param callback - Callback Function that gets called if something gets Logged
456-
* @param level - At which level the watcher is called
457-
*/
458529
export interface LoggerWatcherConfigInterface {
530+
/**
531+
* Callback method to be executed on each log action.
532+
*/
459533
callback: LoggerWatcherCallback;
534+
/**
535+
* From which level the watcher callback should be fired.
536+
*/
460537
level: number;
461538
}
462539

463540
export interface WatcherMethodConfigInterface {
541+
/**
542+
* Key/Name identifier of the watcher callback.
543+
* @default generated id
544+
*/
464545
key?: string;
546+
/**
547+
* From which level the watcher callback should be fired.
548+
* @default 0
549+
*/
465550
level?: number;
466551
}
467552

0 commit comments

Comments
 (0)