-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add BrowserLogger with configurable log levels
- Loading branch information
Ben Willenbring
committed
Sep 7, 2023
1 parent
42b22d0
commit 663b81b
Showing
9 changed files
with
84 additions
and
13 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useConfigurationContext } from "../context/ConfigurationContext"; | ||
import { BrowserLogger } from "@shared/logger/dist/browserLogger"; | ||
|
||
export function useLogger() { | ||
const { logLevel } = useConfigurationContext(); | ||
return new BrowserLogger({ level: logLevel }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { LogLevel } from "@shared/configuration"; | ||
|
||
export type LogFn = (message?: any, ...optionalParams: any[]) => void; | ||
|
||
export interface Logger { | ||
debug: LogFn; | ||
info: LogFn; | ||
warn: LogFn; | ||
error: LogFn; | ||
} | ||
|
||
export interface BrowserLoggerOptions { | ||
level?: LogLevel; | ||
} | ||
|
||
const NO_OP: LogFn = (_message?: any, ..._optionalParams: any[]) => {}; | ||
|
||
export class BrowserLogger implements Logger { | ||
readonly debug: LogFn; | ||
readonly info: LogFn; | ||
readonly warn: LogFn; | ||
readonly error: LogFn; | ||
|
||
constructor(options?: BrowserLoggerOptions) { | ||
const { level } = options ?? {}; | ||
|
||
this.error = console.error.bind(console); | ||
if (level === "error") { | ||
this.warn = NO_OP; | ||
this.info = NO_OP; | ||
this.debug = NO_OP; | ||
return; | ||
} | ||
|
||
this.warn = console.warn.bind(console); | ||
if (level === "warn") { | ||
this.info = NO_OP; | ||
this.debug = NO_OP; | ||
return; | ||
} | ||
|
||
this.info = console.info.bind(console); | ||
if (level === "info") { | ||
this.debug = NO_OP; | ||
return; | ||
} | ||
|
||
this.debug = console.debug.bind(console); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./logger"; | ||
export * from "./BrowserLogger"; |