forked from FoundKeyGang/FoundKey
add log level environment variable
To avoid a circular dependency this requires moving the log level definitions. Also to avoid a circular dependency the env.ts file cannot use a logger and instead uses plain `console.log`.
This commit is contained in:
parent
6b9a3259f5
commit
38c2d86983
2 changed files with 37 additions and 27 deletions
|
@ -1,4 +1,12 @@
|
||||||
const envOption = {
|
export const LOG_LEVELS = {
|
||||||
|
error: 5,
|
||||||
|
warning: 4,
|
||||||
|
success: 3,
|
||||||
|
info: 2,
|
||||||
|
debug: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const envOption = {
|
||||||
onlyQueue: false,
|
onlyQueue: false,
|
||||||
onlyServer: false,
|
onlyServer: false,
|
||||||
noDaemons: false,
|
noDaemons: false,
|
||||||
|
@ -7,14 +15,23 @@ const envOption = {
|
||||||
withLogTime: false,
|
withLogTime: false,
|
||||||
quiet: false,
|
quiet: false,
|
||||||
slow: false,
|
slow: false,
|
||||||
|
logLevel: LOG_LEVELS.info,
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const key of Object.keys(envOption) as (keyof typeof envOption)[]) {
|
for (const key of Object.keys(envOption) as (keyof typeof envOption)[]) {
|
||||||
if (process.env['MK_' + key.replace(/[A-Z]/g, letter => `_${letter}`).toUpperCase()]) envOption[key] = true;
|
const value = process.env['MK_' + key.replace(/[A-Z]/g, letter => `_${letter}`).toUpperCase()];
|
||||||
|
if (value) {
|
||||||
|
if (key === 'logLevel') {
|
||||||
|
if (value.toLowerCase() in LOG_LEVELS) {
|
||||||
|
envOption.logLevel = LOG_LEVELS[value.toLowerCase()];
|
||||||
|
}
|
||||||
|
console.log('Unknown log level ' + JSON.stringify(value.toLowerCase()) + ', defaulting to "info"');
|
||||||
|
} else {
|
||||||
|
envOption[key] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'test') envOption.disableClustering = true;
|
if (process.env.NODE_ENV === 'test') envOption.disableClustering = true;
|
||||||
if (process.env.NODE_ENV === 'test') envOption.quiet = true;
|
if (process.env.NODE_ENV === 'test') envOption.quiet = true;
|
||||||
if (process.env.NODE_ENV === 'test') envOption.noDaemons = true;
|
if (process.env.NODE_ENV === 'test') envOption.noDaemons = true;
|
||||||
|
|
||||||
export { envOption };
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import chalk from 'chalk';
|
||||||
import convertColor from 'color-convert';
|
import convertColor from 'color-convert';
|
||||||
import { format as dateFormat } from 'date-fns';
|
import { format as dateFormat } from 'date-fns';
|
||||||
import config from '@/config/index.js';
|
import config from '@/config/index.js';
|
||||||
import { envOption } from '@/env.js';
|
import { envOption, LOG_LEVELS } from '@/env.js';
|
||||||
import type { KEYWORD } from 'color-convert/conversions.js';
|
import type { KEYWORD } from 'color-convert/conversions.js';
|
||||||
|
|
||||||
type Domain = {
|
type Domain = {
|
||||||
|
@ -11,14 +11,7 @@ type Domain = {
|
||||||
color?: KEYWORD;
|
color?: KEYWORD;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const LEVELS = {
|
export type Level = LOG_LEVELS[keyof LOG_LEVELS];
|
||||||
error: 5,
|
|
||||||
warning: 4,
|
|
||||||
success: 3,
|
|
||||||
info: 2,
|
|
||||||
debug: 1,
|
|
||||||
};
|
|
||||||
export type Level = LEVELS[keyof LEVELS];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that facilitates recording log messages to the console.
|
* Class that facilitates recording log messages to the console.
|
||||||
|
@ -38,7 +31,7 @@ export default class Logger {
|
||||||
* @param color Log message color
|
* @param color Log message color
|
||||||
* @param store Whether to store messages
|
* @param store Whether to store messages
|
||||||
*/
|
*/
|
||||||
constructor(domain: string, color?: KEYWORD, store = true, minLevel: Level = LEVELS.info) {
|
constructor(domain: string, color?: KEYWORD, store = true, minLevel: Level = LOG_LEVELS.info) {
|
||||||
this.domain = {
|
this.domain = {
|
||||||
name: domain,
|
name: domain,
|
||||||
color,
|
color,
|
||||||
|
@ -54,7 +47,7 @@ export default class Logger {
|
||||||
* @param store Whether to store messages
|
* @param store Whether to store messages
|
||||||
* @returns A Logger instance whose parent logger is this instance.
|
* @returns A Logger instance whose parent logger is this instance.
|
||||||
*/
|
*/
|
||||||
public createSubLogger(domain: string, color?: KEYWORD, store = true, minLevel: Level = LEVELS.info): Logger {
|
public createSubLogger(domain: string, color?: KEYWORD, store = true, minLevel: Level = LOG_LEVELS.info): Logger {
|
||||||
const logger = new Logger(domain, color, store, minLevel);
|
const logger = new Logger(domain, color, store, minLevel);
|
||||||
logger.parentLogger = this;
|
logger.parentLogger = this;
|
||||||
return logger;
|
return logger;
|
||||||
|
@ -89,7 +82,7 @@ export default class Logger {
|
||||||
let levelDisplay;
|
let levelDisplay;
|
||||||
let messageDisplay;
|
let messageDisplay;
|
||||||
switch (level) {
|
switch (level) {
|
||||||
case LEVELS.error:
|
case LOG_LEVELS.error:
|
||||||
if (important) {
|
if (important) {
|
||||||
levelDisplay = chalk.bgRed.white('ERR ');
|
levelDisplay = chalk.bgRed.white('ERR ');
|
||||||
} else {
|
} else {
|
||||||
|
@ -97,11 +90,11 @@ export default class Logger {
|
||||||
}
|
}
|
||||||
messageDisplay = chalk.red(message);
|
messageDisplay = chalk.red(message);
|
||||||
break;
|
break;
|
||||||
case LEVELS.warning:
|
case LOG_LEVELS.warning:
|
||||||
levelDisplay = chalk.yellow('WARN');
|
levelDisplay = chalk.yellow('WARN');
|
||||||
messageDisplay = chalk.yellow(message);
|
messageDisplay = chalk.yellow(message);
|
||||||
break;
|
break;
|
||||||
case LEVELS.success:
|
case LOG_LEVELS.success:
|
||||||
if (important) {
|
if (important) {
|
||||||
levelDisplay = chalk.bgGreen.white('DONE');
|
levelDisplay = chalk.bgGreen.white('DONE');
|
||||||
} else {
|
} else {
|
||||||
|
@ -109,11 +102,11 @@ export default class Logger {
|
||||||
}
|
}
|
||||||
messageDisplay = chalk.green(message);
|
messageDisplay = chalk.green(message);
|
||||||
break;
|
break;
|
||||||
case LEVELS.info:
|
case LOG_LEVELS.info:
|
||||||
levelDisplay = chalk.blue('INFO');
|
levelDisplay = chalk.blue('INFO');
|
||||||
messageDisplay = message;
|
messageDisplay = message;
|
||||||
break;
|
break;
|
||||||
case LEVELS.debug: default:
|
case LOG_LEVELS.debug: default:
|
||||||
levelDisplay = chalk.gray('VERB');
|
levelDisplay = chalk.gray('VERB');
|
||||||
messageDisplay = chalk.gray(message);
|
messageDisplay = chalk.gray(message);
|
||||||
break;
|
break;
|
||||||
|
@ -133,11 +126,11 @@ export default class Logger {
|
||||||
*/
|
*/
|
||||||
public error(err: string | Error, important = false): void {
|
public error(err: string | Error, important = false): void {
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
this.log(LEVELS.error, err.toString(), important);
|
this.log(LOG_LEVELS.error, err.toString(), important);
|
||||||
} else if (typeof err === 'object') {
|
} else if (typeof err === 'object') {
|
||||||
this.log(LEVELS.error, `${(err as any).message || (err as any).name || err}`, important);
|
this.log(LOG_LEVELS.error, `${(err as any).message || (err as any).name || err}`, important);
|
||||||
} else {
|
} else {
|
||||||
this.log(LEVELS.error, `${err}`, important);
|
this.log(LOG_LEVELS.error, `${err}`, important);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +141,7 @@ export default class Logger {
|
||||||
* @param important Whether this warning is important
|
* @param important Whether this warning is important
|
||||||
*/
|
*/
|
||||||
public warn(message: string, important = false): void {
|
public warn(message: string, important = false): void {
|
||||||
this.log(LEVELS.warning, message, important);
|
this.log(LOG_LEVELS.warning, message, important);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,7 +151,7 @@ export default class Logger {
|
||||||
* @param important Whether this success message is important
|
* @param important Whether this success message is important
|
||||||
*/
|
*/
|
||||||
public succ(message: string, important = false): void {
|
public succ(message: string, important = false): void {
|
||||||
this.log(LEVELS.success, message, important);
|
this.log(LOG_LEVELS.success, message, important);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -168,7 +161,7 @@ export default class Logger {
|
||||||
* @param important Whether this debug message is important
|
* @param important Whether this debug message is important
|
||||||
*/
|
*/
|
||||||
public debug(message: string, important = false): void {
|
public debug(message: string, important = false): void {
|
||||||
this.log(LEVELS.debug, message, important);
|
this.log(LOG_LEVELS.debug, message, important);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -178,6 +171,6 @@ export default class Logger {
|
||||||
* @param important Whether this info message is important
|
* @param important Whether this info message is important
|
||||||
*/
|
*/
|
||||||
public info(message: string, important = false): void {
|
public info(message: string, important = false): void {
|
||||||
this.log(LEVELS.info, message, important);
|
this.log(LOG_LEVELS.info, message, important);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue