feat: logger improvements
- Timestamp is gray - removed colorette dependency - introduction of LOGGER_FILTERS
This commit is contained in:
parent
9b57fb280b
commit
81dea6cf90
3 changed files with 28 additions and 31 deletions
|
@ -35,7 +35,6 @@
|
||||||
"@mantine/modals": "^5.9.2",
|
"@mantine/modals": "^5.9.2",
|
||||||
"@mantine/next": "^5.9.2",
|
"@mantine/next": "^5.9.2",
|
||||||
"@mantine/notifications": "^5.9.2",
|
"@mantine/notifications": "^5.9.2",
|
||||||
"@mantine/nprogress": "^5.9.2",
|
|
||||||
"@mantine/prism": "^5.9.2",
|
"@mantine/prism": "^5.9.2",
|
||||||
"@prisma/client": "^4.8.1",
|
"@prisma/client": "^4.8.1",
|
||||||
"@prisma/internals": "^4.8.1",
|
"@prisma/internals": "^4.8.1",
|
||||||
|
@ -43,7 +42,6 @@
|
||||||
"@sapphire/shapeshift": "^3.7.1",
|
"@sapphire/shapeshift": "^3.7.1",
|
||||||
"@tanstack/react-query": "^4.19.1",
|
"@tanstack/react-query": "^4.19.1",
|
||||||
"argon2": "^0.30.2",
|
"argon2": "^0.30.2",
|
||||||
"colorette": "^2.0.19",
|
|
||||||
"cookie": "^0.5.0",
|
"cookie": "^0.5.0",
|
||||||
"dayjs": "^1.11.7",
|
"dayjs": "^1.11.7",
|
||||||
"dotenv": "^16.0.3",
|
"dotenv": "^16.0.3",
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
import { blueBright, cyan, red, yellow } from 'colorette';
|
const COLORS = {
|
||||||
|
blueBright: (str: string) => `\x1b[34m${str}\x1b[0m`,
|
||||||
|
cyan: (str: string) => `\x1b[36m${str}\x1b[0m`,
|
||||||
|
red: (str: string) => `\x1b[31m${str}\x1b[0m`,
|
||||||
|
yellow: (str: string) => `\x1b[33m${str}\x1b[0m`,
|
||||||
|
gray: (str: string) => `\x1b[90m${str}\x1b[0m`,
|
||||||
|
};
|
||||||
|
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
export enum LoggerLevel {
|
export enum LoggerLevel {
|
||||||
|
@ -10,6 +17,10 @@ export enum LoggerLevel {
|
||||||
export default class Logger {
|
export default class Logger {
|
||||||
public name: string;
|
public name: string;
|
||||||
|
|
||||||
|
static filters(): string[] {
|
||||||
|
return (process.env.LOGGER_FILTERS ?? '').split(',');
|
||||||
|
}
|
||||||
|
|
||||||
static get(klass: any) {
|
static get(klass: any) {
|
||||||
if (typeof klass !== 'function') if (typeof klass !== 'string') throw new Error('not string/function');
|
if (typeof klass !== 'function') if (typeof klass !== 'string') throw new Error('not string/function');
|
||||||
|
|
||||||
|
@ -26,13 +37,23 @@ export default class Logger {
|
||||||
return new Logger(`${this.name}::${name}`);
|
return new Logger(`${this.name}::${name}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
show(): boolean {
|
||||||
|
if (!Logger.filters().length) return true;
|
||||||
|
|
||||||
|
return Logger.filters().includes(this.name);
|
||||||
|
}
|
||||||
|
|
||||||
info(...args: any[]): this {
|
info(...args: any[]): this {
|
||||||
|
if (!this.show()) return this;
|
||||||
|
|
||||||
process.stdout.write(this.formatMessage(LoggerLevel.INFO, this.name, args.join(' ')));
|
process.stdout.write(this.formatMessage(LoggerLevel.INFO, this.name, args.join(' ')));
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
error(...args: any[]): this {
|
error(...args: any[]): this {
|
||||||
|
if (!this.show()) return this;
|
||||||
|
|
||||||
process.stdout.write(
|
process.stdout.write(
|
||||||
this.formatMessage(LoggerLevel.ERROR, this.name, args.map((error) => error.stack ?? error).join(' '))
|
this.formatMessage(LoggerLevel.ERROR, this.name, args.map((error) => error.stack ?? error).join(' '))
|
||||||
);
|
);
|
||||||
|
@ -41,7 +62,8 @@ export default class Logger {
|
||||||
}
|
}
|
||||||
|
|
||||||
debug(...args: any[]): this {
|
debug(...args: any[]): this {
|
||||||
if (!process.env.DEBUG) return;
|
if (!process.env.DEBUG) return this;
|
||||||
|
if (!this.show()) return this;
|
||||||
|
|
||||||
process.stdout.write(this.formatMessage(LoggerLevel.DEBUG, this.name, args.join(' ')));
|
process.stdout.write(this.formatMessage(LoggerLevel.DEBUG, this.name, args.join(' ')));
|
||||||
|
|
||||||
|
@ -50,17 +72,17 @@ export default class Logger {
|
||||||
|
|
||||||
formatMessage(level: LoggerLevel, name: string, message: string) {
|
formatMessage(level: LoggerLevel, name: string, message: string) {
|
||||||
const time = dayjs().format('YYYY-MM-DD hh:mm:ss,SSS A');
|
const time = dayjs().format('YYYY-MM-DD hh:mm:ss,SSS A');
|
||||||
return `${time} ${this.formatLevel(level)} [${blueBright(name)}] ${message}\n`;
|
return `${COLORS.gray(time)} ${this.formatLevel(level)} [${COLORS.blueBright(name)}] ${message}\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
formatLevel(level: LoggerLevel) {
|
formatLevel(level: LoggerLevel) {
|
||||||
switch (level) {
|
switch (level) {
|
||||||
case LoggerLevel.INFO:
|
case LoggerLevel.INFO:
|
||||||
return cyan('info ');
|
return COLORS.cyan('info ');
|
||||||
case LoggerLevel.ERROR:
|
case LoggerLevel.ERROR:
|
||||||
return red('error');
|
return COLORS.red('error');
|
||||||
case LoggerLevel.DEBUG:
|
case LoggerLevel.DEBUG:
|
||||||
return yellow('debug');
|
return COLORS.yellow('debug');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
23
yarn.lock
23
yarn.lock
|
@ -1548,20 +1548,6 @@ __metadata:
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@mantine/nprogress@npm:^5.9.2":
|
|
||||||
version: 5.9.3
|
|
||||||
resolution: "@mantine/nprogress@npm:5.9.3"
|
|
||||||
dependencies:
|
|
||||||
"@mantine/utils": 5.9.3
|
|
||||||
peerDependencies:
|
|
||||||
"@mantine/core": 5.9.3
|
|
||||||
"@mantine/hooks": 5.9.3
|
|
||||||
react: ">=16.8.0"
|
|
||||||
react-dom: ">=16.8.0"
|
|
||||||
checksum: 2dfb96e32fcd92a0d9aaee6868025307a0982cea777cc129d2036bffe255953fa35483f6ca0c8f5accbc13606ac7294e9b8102c88eb3071776931b5aab75ce2c
|
|
||||||
languageName: node
|
|
||||||
linkType: hard
|
|
||||||
|
|
||||||
"@mantine/prism@npm:^5.9.2":
|
"@mantine/prism@npm:^5.9.2":
|
||||||
version: 5.9.3
|
version: 5.9.3
|
||||||
resolution: "@mantine/prism@npm:5.9.3"
|
resolution: "@mantine/prism@npm:5.9.3"
|
||||||
|
@ -3757,13 +3743,6 @@ __metadata:
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"colorette@npm:^2.0.19":
|
|
||||||
version: 2.0.19
|
|
||||||
resolution: "colorette@npm:2.0.19"
|
|
||||||
checksum: 888cf5493f781e5fcf54ce4d49e9d7d698f96ea2b2ef67906834bb319a392c667f9ec69f4a10e268d2946d13a9503d2d19b3abaaaf174e3451bfe91fb9d82427
|
|
||||||
languageName: node
|
|
||||||
linkType: hard
|
|
||||||
|
|
||||||
"combined-stream@npm:^1.0.8":
|
"combined-stream@npm:^1.0.8":
|
||||||
version: 1.0.8
|
version: 1.0.8
|
||||||
resolution: "combined-stream@npm:1.0.8"
|
resolution: "combined-stream@npm:1.0.8"
|
||||||
|
@ -11556,7 +11535,6 @@ __metadata:
|
||||||
"@mantine/modals": ^5.9.2
|
"@mantine/modals": ^5.9.2
|
||||||
"@mantine/next": ^5.9.2
|
"@mantine/next": ^5.9.2
|
||||||
"@mantine/notifications": ^5.9.2
|
"@mantine/notifications": ^5.9.2
|
||||||
"@mantine/nprogress": ^5.9.2
|
|
||||||
"@mantine/prism": ^5.9.2
|
"@mantine/prism": ^5.9.2
|
||||||
"@prisma/client": ^4.8.1
|
"@prisma/client": ^4.8.1
|
||||||
"@prisma/internals": ^4.8.1
|
"@prisma/internals": ^4.8.1
|
||||||
|
@ -11572,7 +11550,6 @@ __metadata:
|
||||||
"@types/react": ^18.0.26
|
"@types/react": ^18.0.26
|
||||||
"@types/sharp": ^0.31.0
|
"@types/sharp": ^0.31.0
|
||||||
argon2: ^0.30.2
|
argon2: ^0.30.2
|
||||||
colorette: ^2.0.19
|
|
||||||
cookie: ^0.5.0
|
cookie: ^0.5.0
|
||||||
cross-env: ^7.0.3
|
cross-env: ^7.0.3
|
||||||
dayjs: ^1.11.7
|
dayjs: ^1.11.7
|
||||||
|
|
Loading…
Reference in a new issue