File size: 752 Bytes
e5f68c9
 
 
 
39af208
e5f68c9
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Logger with environment variable control
// LOG_LEVEL: debug, info, warn, error, none (default: info)

const LOG_LEVELS = { debug: 0, info: 1, warn: 2, error: 3, none: 4 };
const currentLevel = LOG_LEVELS[process.env.LOG_LEVEL?.toLowerCase()] ?? LOG_LEVELS.info;

const ts = () => new Date().toISOString();

const log = {
  debug: (...args) => currentLevel <= LOG_LEVELS.debug && console.log(`[${ts()}] [DEBUG]`, ...args),
  info: (...args) => currentLevel <= LOG_LEVELS.info && console.log(`[${ts()}] [INFO]`, ...args),
  warn: (...args) => currentLevel <= LOG_LEVELS.warn && console.warn(`[${ts()}] [WARN]`, ...args),
  error: (...args) => currentLevel <= LOG_LEVELS.error && console.error(`[${ts()}] [ERROR]`, ...args),
};

module.exports = log;