Skip to content

Commit 6f5def5

Browse files
authored
Configurable log levels (#39)
2 parents f346da5 + 3f9226b commit 6f5def5

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

browser-extension/src/lib/config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ const MODES = ['PROD', 'PLAYGROUNDS_PR'] as const
22

33
export type ModeType = (typeof MODES)[number]
44

5+
const LOG_LEVELS = ['DEBUG', 'INFO', 'WARN', 'ERROR'] as const
6+
7+
export type LogLevel = (typeof LOG_LEVELS)[number]
8+
59
export const CONFIG = {
610
ADDED_OVERTYPE_CLASS: 'gitcasso-overtype',
7-
DEBUG: true, // enabled debug logging
811
EXTENSION_NAME: 'gitcasso', // decorates logs
12+
LOG_LEVEL: 'DEBUG' satisfies LogLevel,
913
MODE: 'PROD' satisfies ModeType,
1014
} as const
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CONFIG } from './config'
1+
import { CONFIG, type LogLevel } from './config'
22

33
/**
44
* Simple logging utilities for the extension
@@ -9,12 +9,25 @@ const prefix = `[${CONFIG.EXTENSION_NAME}]`
99
// No-op function for disabled logging
1010
const noop = () => {}
1111

12+
// Log level hierarchy - index represents priority
13+
const LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {
14+
DEBUG: 0,
15+
ERROR: 3,
16+
INFO: 1,
17+
WARN: 2,
18+
}
19+
20+
// Helper function to check if a log level is enabled
21+
const shouldLog = (level: LogLevel): boolean => {
22+
return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[CONFIG.LOG_LEVEL]
23+
}
24+
1225
// Export simple logging functions
1326
export const logger = {
14-
debug: CONFIG.DEBUG ? console.log.bind(console, prefix) : noop,
15-
error: console.error.bind(console, prefix),
16-
info: CONFIG.DEBUG ? console.log.bind(console, prefix) : noop,
17-
time: CONFIG.DEBUG ? console.time.bind(console) : noop,
18-
timeEnd: CONFIG.DEBUG ? console.timeEnd.bind(console) : noop,
19-
warn: console.warn.bind(console, prefix),
27+
debug: shouldLog('DEBUG') ? console.log.bind(console, prefix) : noop,
28+
error: shouldLog('ERROR') ? console.error.bind(console, prefix) : noop,
29+
info: shouldLog('INFO') ? console.log.bind(console, prefix) : noop,
30+
time: shouldLog('INFO') ? console.time.bind(console) : noop,
31+
timeEnd: shouldLog('INFO') ? console.timeEnd.bind(console) : noop,
32+
warn: shouldLog('WARN') ? console.warn.bind(console, prefix) : noop,
2033
}

0 commit comments

Comments
 (0)