92 lines
2.6 KiB
TypeScript
92 lines
2.6 KiB
TypeScript
import { DEBUG_LOG } from '$env/static/private';
|
|
import { env } from '$env/dynamic/private';
|
|
import { isTruthy } from '$lib/truthyString';
|
|
const { DEV } = import.meta.env;
|
|
|
|
export const enableVerboseLog = isTruthy(env.VERBOSE);
|
|
export const debugLogEnv = isTruthy(DEBUG_LOG);
|
|
|
|
/**
|
|
* @deprecated Use the new {@link Logger} class instead.
|
|
*/
|
|
export const log = {
|
|
verbose: (...params: any[]) => {
|
|
if (!enableVerboseLog) {
|
|
return;
|
|
}
|
|
console.debug(new Date().toISOString(), ...params);
|
|
},
|
|
debug: (...params: any[]) => {
|
|
if (!log.isDebugEnabled()) {
|
|
return;
|
|
}
|
|
console.debug(new Date().toISOString(), ...params);
|
|
},
|
|
log: (...params: any[]) => {
|
|
console.log(new Date().toISOString(), ...params);
|
|
},
|
|
info: (...params: any[]) => {
|
|
console.info(new Date().toISOString(), ...params);
|
|
},
|
|
warn: (...params: any[]) => {
|
|
console.warn(new Date().toISOString(), ...params);
|
|
},
|
|
error: (...params: any[]) => {
|
|
console.error(new Date().toISOString(), ...params);
|
|
},
|
|
isDebugEnabled: (): boolean => {
|
|
return DEV;
|
|
}
|
|
};
|
|
|
|
export class Logger {
|
|
public constructor(private name: string) {}
|
|
|
|
public static isDebugEnabled(): boolean {
|
|
return debugLogEnv || DEV || enableVerboseLog;
|
|
}
|
|
public verbose(...params: any[]) {
|
|
if (!enableVerboseLog) {
|
|
return;
|
|
}
|
|
console.debug(new Date().toISOString(), '- [VRBSE]', `- ${this.name} -`, ...params);
|
|
}
|
|
public debug(...params: any[]) {
|
|
if (!Logger.isDebugEnabled()) {
|
|
return;
|
|
}
|
|
console.debug(new Date().toISOString(), '- [DEBUG]', `- ${this.name} -`, ...params);
|
|
}
|
|
public log(...params: any[]) {
|
|
console.log(new Date().toISOString(), '- [ LOG ]', `- ${this.name} -`, ...params);
|
|
}
|
|
public info(...params: any[]) {
|
|
console.info(new Date().toISOString(), '- [INFO ]', `- ${this.name} -`, ...params);
|
|
}
|
|
public warn(...params: any[]) {
|
|
console.warn(new Date().toISOString(), '- [WARN ]', `- ${this.name} -`, ...params);
|
|
}
|
|
public error(...params: any[]) {
|
|
console.error(new Date().toISOString(), '- [ERROR]', `- ${this.name} -`, ...params);
|
|
}
|
|
|
|
public static error(...params: any[]) {
|
|
console.error(new Date().toISOString(), ...params);
|
|
}
|
|
public static debug(...params: any[]) {
|
|
if (!Logger.isDebugEnabled()) {
|
|
return;
|
|
}
|
|
console.debug(new Date().toISOString(), ...params);
|
|
}
|
|
public static log(...params: any[]) {
|
|
console.log(new Date().toISOString(), ...params);
|
|
}
|
|
public static info(...params: any[]) {
|
|
console.info(new Date().toISOString(), ...params);
|
|
}
|
|
public static warn(...params: any[]) {
|
|
console.warn(new Date().toISOString(), ...params);
|
|
}
|
|
}
|