Skip to content

Commit

Permalink
fix: globalThis fallback (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-depaula committed Nov 22, 2021
1 parent 5fdee63 commit 367b8ee
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/loggerSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ declare namespace globalThis {
let VIRTUOSO_LOG_LEVEL: LogLevel | undefined
}

// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace window {
let VIRTUOSO_LOG_LEVEL: LogLevel | undefined
}

export enum LogLevel {
DEBUG,
INFO,
Expand All @@ -26,11 +31,13 @@ const CONSOLE_METHOD_MAP = {
[LogLevel.ERROR]: 'error',
} as const

const getGlobalThis = () => (typeof globalThis === 'undefined' ? window : globalThis)

export const loggerSystem = u.system(
() => {
const logLevel = u.statefulStream<LogLevel>(LogLevel.ERROR)
const log = u.statefulStream<Log>((label: string, message: any, level: LogLevel = LogLevel.INFO) => {
const currentLevel = (globalThis || window)['VIRTUOSO_LOG_LEVEL'] ?? u.getValue(logLevel)
const currentLevel = getGlobalThis()['VIRTUOSO_LOG_LEVEL'] ?? u.getValue(logLevel)
if (level >= currentLevel) {
console[CONSOLE_METHOD_MAP[level]]('%creact-virtuoso: %c%s %o', 'color: #0253b3; font-weight: bold', 'color: black', label, message)
}
Expand Down

0 comments on commit 367b8ee

Please sign in to comment.