Skip to content

Commit

Permalink
feat: moving to a LogLevel argument for DefaultLogger defaulted to warn
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Long <[email protected]>
  • Loading branch information
kevinmlong committed Dec 21, 2024
1 parent 3277412 commit 587bf1a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
8 changes: 5 additions & 3 deletions packages/shared/src/hooks/logging-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import type { OpenFeatureError } from '../errors';
import type { BaseHook } from './hook';
import type { BeforeHookContext, HookContext, HookHints } from './hooks';
import type { FlagValue, EvaluationDetails } from '../evaluation';
import type { Logger } from '../logger';
import { DefaultLogger, LogLevel, SafeLogger } from '../logger';

import { DefaultLogger, SafeLogger } from '../logger';

type LoggerPayload = Record<string, unknown>;

Expand All @@ -20,10 +21,11 @@ const VALUE_KEY = 'value';

export class LoggingHook implements BaseHook {
readonly includeEvaluationContext: boolean = false;
readonly logger = new SafeLogger(new DefaultLogger(true, true));
readonly logger: Logger;

constructor(includeEvaluationContext: boolean = false) {
constructor(includeEvaluationContext: boolean = false, logger?: Logger) {
this.includeEvaluationContext = !!includeEvaluationContext;
this.logger = logger || new SafeLogger(new DefaultLogger(LogLevel.DEBUG));
}

before(hookContext: BeforeHookContext): void {
Expand Down
23 changes: 13 additions & 10 deletions packages/shared/src/logger/default-logger.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
/* eslint-disable @typescript-eslint/no-empty-function */

import type { Logger } from './logger';
import type { Logger} from './logger';
import { LogLevel } from './logger';

export class DefaultLogger implements Logger {

private readonly showInfo : boolean = false;
private readonly showDebug : boolean = false;
private readonly logLevel : LogLevel;

constructor(showInfo: boolean = false, showDebug: boolean = false){
this.showInfo = showInfo;
this.showDebug = showDebug;
constructor(logLevel: LogLevel = LogLevel.WARN){
this.logLevel = logLevel;
}

error(...args: unknown[]): void {
console.error(...args);
if(this.logLevel >= LogLevel.ERROR) {
console.error(...args);
}
}

warn(...args: unknown[]): void {
console.warn(...args);
if(this.logLevel >= LogLevel.WARN) {
console.warn(...args);
}
}

info(...args: unknown[]): void {
if(this.showInfo) {
if(this.logLevel >= LogLevel.INFO) {
console.info(...args);
}
}

debug(...args: unknown[]): void {
if(this.showDebug) {
if(this.logLevel === LogLevel.DEBUG) {
console.debug(...args);
}
}
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ export interface ManageLogger<T> {
*/
setLogger(logger: Logger): T;
}

export enum LogLevel {
ERROR = 1,
WARN = 2,
INFO = 3,
DEBUG = 4,
}

0 comments on commit 587bf1a

Please sign in to comment.