Skip to content

Commit b600cf8

Browse files
fix: Logger would not use the correct console function
This fix is required because the browser uses `console` differently from how node uses it.
1 parent 5414384 commit b600cf8

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

lib/logger.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { LogLevel } from '#lib/types/log-level';
66
import chalk from 'chalk';
77

88
/** The exact type of `console.xyz()`. */
9-
type LogFunction = typeof console.info;
9+
type LogFunction = typeof console.log;
1010

1111
/** The interface that any logger needs to implement */
12-
interface LogApi {
12+
export interface LogApi {
1313
/**
1414
* Prints to `stderr` with newline if `Logger.logLevel <= LogLevel.CRITICAL`. Multiple arguments can be passed, with
1515
* the first used as the primary message and all additional used as substitution values similar to printf (the
@@ -56,12 +56,6 @@ interface LogApi {
5656
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
5757
const DEV_NULL: LogFunction = (_message, ..._optionalParams) => {};
5858

59-
// eslint-disable-next-line no-console
60-
const STD_OUT: LogFunction = console.info;
61-
62-
// eslint-disable-next-line no-console
63-
const STD_ERR: LogFunction = console.error;
64-
6559
/**
6660
* The logger of the application. Has functions for all `LogLevel`s like `Logger.debug(...)`.
6761
*/
@@ -73,23 +67,17 @@ export class Logger {
7367
/** A function that ignores all it's arguments. */
7468
public static readonly DEV_NULL: LogFunction = DEV_NULL;
7569

76-
/** A function that prints as `console.log` to STD out. */
77-
public static readonly STD_OUT: LogFunction = STD_OUT;
78-
79-
/** A function that prints as `console.log` to STD err. */
80-
public static readonly STD_ERR: LogFunction = STD_ERR;
81-
8270
/**
8371
* The default function for each `LogLevel`.
8472
*/
8573
private static PRIVATE_DEFAULT_FUNCTIONS: Record<LogLevel, LogFunction> = {
8674
[LogLevel.SILENT]: DEV_NULL,
87-
[LogLevel.CRITICAL]: STD_ERR,
88-
[LogLevel.ERROR]: STD_ERR,
89-
[LogLevel.WARN]: STD_ERR,
90-
[LogLevel.INFO]: STD_OUT,
91-
[LogLevel.DEBUG]: STD_OUT,
92-
[LogLevel.TRACE]: STD_OUT,
75+
[LogLevel.CRITICAL]: console.error, // eslint-disable-line no-console
76+
[LogLevel.ERROR]: console.error, // eslint-disable-line no-console
77+
[LogLevel.WARN]: console.warn, // eslint-disable-line no-console
78+
[LogLevel.INFO]: console.info, // eslint-disable-line no-console
79+
[LogLevel.DEBUG]: console.debug, // eslint-disable-line no-console
80+
[LogLevel.TRACE]: console.trace, // eslint-disable-line no-console
9381
};
9482

9583
/**
@@ -143,8 +131,7 @@ export class Logger {
143131
const STEP = 1;
144132
for (let index = 0; index < LogLevel.options.length; index += STEP) {
145133
const level = LogLevel.fromNumber(index);
146-
if (LogLevel.of(level).isMoreVerboseThen(newLevel))
147-
Logger.privateFunctions[level] = Logger.DEV_NULL;
134+
if (LogLevel.of(level).isMoreVerboseThen(newLevel)) Logger.privateFunctions[level] = DEV_NULL;
148135
else Logger.privateFunctions[level] = Logger.DEFAULT_FUNCTIONS[level];
149136
}
150137
}
@@ -251,7 +238,7 @@ export class Logger {
251238
* the log level.
252239
*/
253240
// eslint-disable-next-line @typescript-eslint/member-ordering
254-
public static readonly API: LogApi = {
241+
public static readonly API: LogApi = Object.freeze({
255242
critical: (...args: unknown[]) => {
256243
Logger.critical(args);
257244
},
@@ -270,7 +257,7 @@ export class Logger {
270257
warn: (...args: unknown[]) => {
271258
Logger.warn(args);
272259
},
273-
};
260+
});
274261
}
275262

276263
Logger.logLevel = Logger.DEFAULT_LOG_LEVEL;

0 commit comments

Comments
 (0)