Skip to content

Make instance logLevel setter able to process string input #3076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,23 @@ export class Logger {
* The log level of the given Logger instance.
*/
private _logLevel = defaultLogLevel;

get logLevel(): LogLevel {
return this._logLevel;
}

set logLevel(val: LogLevel) {
if (!(val in LogLevel)) {
throw new TypeError('Invalid value assigned to `logLevel`');
throw new TypeError(`Invalid value "${val}" assigned to \`logLevel\``);
}
this._logLevel = val;
}

// Workaround for setter/getter having to be the same type.
setLogLevel(val: LogLevel | LogLevelString): void {
this._logLevel = typeof val === 'string' ? levelStringToEnum[val] : val;
}

/**
* The main (internal) log handler for the Logger instance.
* Can be set to a new function in internal package code but not by user.
Expand Down Expand Up @@ -205,9 +212,8 @@ export class Logger {
}

export function setLogLevel(level: LogLevelString | LogLevel): void {
const newLevel = typeof level === 'string' ? levelStringToEnum[level] : level;
instances.forEach(inst => {
inst.logLevel = newLevel;
inst.setLogLevel(level);
});
}

Expand Down