-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (49 loc) · 1.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { freestyle, defaultBars } from './rap.js';
const levels = {
none: 0,
error: 1,
info: 2,
debug: 3,
trace: 4,
};
const isNode = typeof process !== 'undefined' && process.env;
let currentLevel = levels[isNode ? process.env.LOG_LEVEL?.toLowerCase() : 'none'] || levels.none;
export function setLogLevel(level) {
if (typeof level === 'string' && levels[level.toLowerCase()] !== undefined) {
currentLevel = levels[level.toLowerCase()];
} else {
console.warn(`Invalid log level "${level}", defaulting to "${Object.keys(levels)[currentLevel]}"`);
}
}
export function getCurrentLogLevel() {
return Object.keys(levels).find(key => levels[key] === currentLevel);
}
const originalConsole = { ...console };
console.info = (message, ...args) => {
if (currentLevel >= levels.info) originalConsole.log(`INFO - ${message}`, ...args);
};
console.error = (message, ...args) => {
if (currentLevel >= levels.error) originalConsole.error(`ERROR - ${message}`, ...args);
};
console.debug = (message, ...args) => {
if (currentLevel >= levels.debug) originalConsole.debug(`DEBUG - ${message}`, ...args);
};
console.trace = (message, ...args) => {
if (currentLevel >= levels.trace) originalConsole.trace(`TRACE - ${message}`, ...args);
};
console.rap = () => {
const bars = freestyle(defaultBars);
for (const bar of bars) {
console.debug(bar);
}
};
// Export for ESM environments
export { levels };
// CommonJS compatibility check
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
setLogLevel,
getCurrentLogLevel,
levels,
};
}