Simplifying the logging for now

This commit is contained in:
saman 2021-12-03 16:02:00 -05:00
parent 8d790378b9
commit 3a3a6eab00
4 changed files with 19 additions and 22 deletions

View File

@ -1,6 +1,11 @@
import { useRef } from 'react'; import { useRef } from 'react';
import Logging from '../library/Logging'; import Logging from '../library/Logging';
/**
* A hook for a simple logging library.
* @param namespace string that will be displayed after the timestamp and before the log level as [namespace].
* @returns object of type Logging
*/
export const useLogging = (namespace?: string): [Logging] => { export const useLogging = (namespace?: string): [Logging] => {
const { current: logging } = useRef(new Logging(namespace)); const { current: logging } = useRef(new Logging(namespace));

View File

@ -1 +1,2 @@
export * from './hooks'; export * from './hooks';
export * from './library';

View File

@ -1,33 +1,23 @@
class Logging { const DEBUG = 'DEBUG';
const INFO = 'INFO';
const WARN = 'WARN';
const ERROR = 'ERROR';
export class Logging {
private namespace = 'Default'; private namespace = 'Default';
constructor(namespace?: string) { constructor(namespace?: string) {
if (namespace) this.namespace = namespace; if (namespace) this.namespace = namespace;
} }
public info = (message: any) => { public log = (key: string, obj: any, ...objs: any[]) => {
if (typeof message === 'string') { console.info(`[${this.getDate()}] [${this.namespace}] [${key}] %c${obj}`, ...objs);
console.log(`[${this.getDate()}] [${this.namespace}] [INFO] ${message}`);
} else {
console.log(`[${this.getDate()}] [${this.namespace}] [INFO]`, message);
}
}; };
public warn = (message: any) => { public debug = (obj: any, ...objs: any[]) => this.log(DEBUG, obj, objs);
if (typeof message === 'string') { public info = (obj: any, ...objs: any[]) => this.log(INFO, obj, objs);
console.log(`[${this.getDate()}] [${this.namespace}] [WARN] ${message}`); public warn = (obj: any, ...objs: any[]) => this.log(WARN, obj, objs);
} else { public error = (obj: any, ...objs: any[]) => this.log(ERROR, obj, objs);
console.log(`[${this.getDate()}] [${this.namespace}] [WARN]`, message);
}
};
public error = (message: any) => {
if (typeof message === 'string') {
console.log(`[${this.getDate()}] [${this.namespace}] [ERROR] ${message}`);
} else {
console.log(`[${this.getDate()}] [${this.namespace}] [ERROR]`, message);
}
};
public getDate = () => { public getDate = () => {
return new Date().toISOString(); return new Date().toISOString();

1
src/library/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './Logging';