forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
23 lines (21 loc) · 824 Bytes
/
Copy pathlogger.ts
File metadata and controls
23 lines (21 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* eslint-disable turbo/no-undeclared-env-vars */
import { default as chalk } from 'chalk';
const getRandomChalkBgColor = () => {
const colors = ['bgRed', 'bgGreen', 'bgYellow', 'bgBlue', 'bgMagenta', 'bgCyan', 'bgWhite'];
return colors[Math.floor(Math.random() * colors.length)];
};
type CreateLoggerOptions = { prefix: string; color?: string };
export const createLogger = (opts: CreateLoggerOptions) => {
const { color, prefix } = opts;
const prefixBgColor = color || getRandomChalkBgColor();
return {
info: (msg: string) => {
if (process.env.DEBUG) {
console.info(`${chalk[prefixBgColor](`[${prefix}]`)} ${msg}`);
}
},
child: (childOpts: CreateLoggerOptions) => {
return createLogger({ prefix: `${prefix} :: ${childOpts.prefix}`, color: prefixBgColor });
},
};
};