-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
33 lines (29 loc) · 1.2 KB
/
logger.js
File metadata and controls
33 lines (29 loc) · 1.2 KB
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
const setupLogger = require('dera-logger');
require('dotenv').config();
const isDebug = (process.env.DEBUG === 'true'); // Simple debug flag
const isLogEnabled = (process.env.LOG === 'true'); // Simple log flag
const options = {
logDirectory: 'logs',
timestampFormat: 'HH:mm:ss',
fileDatePattern: 'YYYY-MM-DD',
zippedArchive: false,
maxLogFileSize: null,
maxFiles: '14d',
addConsoleInNonProduction: true,
transports: [
{ filename: 'combined', level: 'silly', source: 'app' },
{ filename: 'error', level: 'warn', source: 'app' }
]
};
const logger = setupLogger(options);
// Evaluate function args lazily to avoid building strings if logs are disabled
const evalArg = (a) => (typeof a === 'function' ? a() : a);
const mapArgs = (args) => args.map(evalArg);
const shouldDebug = isDebug && isLogEnabled;
// Wrap logger calls to respect debug mode
module.exports = {
debug: (...args) => { if (shouldDebug) logger.debug(...mapArgs(args), 'app'); },
info: (...args) => { if (isLogEnabled) logger.info(...mapArgs(args), 'app'); },
warn: (...args) => { if (isLogEnabled) logger.warn(...mapArgs(args), 'app'); },
error: (...args) => { if (isLogEnabled) logger.error(...mapArgs(args), 'app'); },
};