Skip to content

Commit

Permalink
chore(log): sync level and levelName in BaseHandler (denoland#4393)
Browse files Browse the repository at this point in the history
  • Loading branch information
timreichen authored Feb 26, 2024
1 parent ce719de commit 0f62d4c
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions log/base_handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { getLevelByName, LevelName } from "./levels.ts";
import { getLevelByName, getLevelName, LevelName, LogLevel } from "./levels.ts";
import type { LogRecord } from "./logger.ts";

export type FormatterFunction = (logRecord: LogRecord) => string;
Expand All @@ -11,14 +11,33 @@ export interface BaseHandlerOptions {
}

export class BaseHandler {
level: number;
levelName: LevelName;
#levelName: LevelName;
#level: LogLevel;
formatter: FormatterFunction;

constructor(levelName: LevelName, options: BaseHandlerOptions = {}) {
this.level = getLevelByName(levelName);
this.levelName = levelName;
this.formatter = options.formatter || DEFAULT_FORMATTER;
constructor(
levelName: LevelName,
{ formatter = DEFAULT_FORMATTER }: BaseHandlerOptions = {},
) {
this.#levelName = levelName;
this.#level = getLevelByName(levelName);
this.formatter = formatter;
}

get level() {
return this.#level;
}
set level(level: LogLevel) {
this.#level = level;
this.#levelName = getLevelName(level);
}

get levelName() {
return this.#levelName;
}
set levelName(levelName: LevelName) {
this.#levelName = levelName;
this.#level = getLevelByName(levelName);
}

handle(logRecord: LogRecord) {
Expand Down

0 comments on commit 0f62d4c

Please sign in to comment.