-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.ts
51 lines (43 loc) · 1.21 KB
/
logger.ts
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
import chalk from 'chalk';
/**
* Debug mode state from environment variables
* Can be controlled via NEXT_PUBLIC_DEBUG_MODE environment variable
*/
export const isDebugMode =
process.env.NEXT_PUBLIC_DEBUG_MODE === 'true' ||
(typeof window !== 'undefined' &&
localStorage.getItem('debug-mode') === 'true');
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
interface DebugOptions {
module?: string;
level?: LogLevel;
}
/**
* Debug logging function
* @param message The log message
* @param data Optional data object to log
* @param options Logging options
*/
export function logger(
message: string,
data?: unknown,
options: DebugOptions = {},
): void {
if (!isDebugMode) return;
const { module = 'APP', level = 'debug' } = options;
const timestamp = new Date().toISOString();
const prefix = `[Vity-App] [${timestamp}] [${module}] [${level.toUpperCase()}]`;
const logFn = console[level] || console.log;
const colorFn = {
debug: chalk.blue,
info: chalk.green,
warn: chalk.yellow,
error: chalk.red,
}[level] || chalk.white;
if (data !== undefined) {
logFn(colorFn(`${prefix} ${message}`));
logFn(data);
} else {
logFn(colorFn(`${prefix} ${message}`));
}
}