diff --git a/src/hooks/useLogging.ts b/src/hooks/useLogging.ts index c686fa0..d772565 100644 --- a/src/hooks/useLogging.ts +++ b/src/hooks/useLogging.ts @@ -1,6 +1,11 @@ import { useRef } from 'react'; 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] => { const { current: logging } = useRef(new Logging(namespace)); diff --git a/src/index.ts b/src/index.ts index 4cc90d0..a5b8b8b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export * from './hooks'; +export * from './library'; diff --git a/src/library/Logging.ts b/src/library/Logging.ts index 6ad2930..6657229 100644 --- a/src/library/Logging.ts +++ b/src/library/Logging.ts @@ -1,33 +1,23 @@ -class Logging { +const DEBUG = 'DEBUG'; +const INFO = 'INFO'; +const WARN = 'WARN'; +const ERROR = 'ERROR'; + +export class Logging { private namespace = 'Default'; constructor(namespace?: string) { if (namespace) this.namespace = namespace; } - public info = (message: any) => { - if (typeof message === 'string') { - console.log(`[${this.getDate()}] [${this.namespace}] [INFO] ${message}`); - } else { - console.log(`[${this.getDate()}] [${this.namespace}] [INFO]`, message); - } + public log = (key: string, obj: any, ...objs: any[]) => { + console.info(`[${this.getDate()}] [${this.namespace}] [${key}] %c${obj}`, ...objs); }; - public warn = (message: any) => { - if (typeof message === 'string') { - console.log(`[${this.getDate()}] [${this.namespace}] [WARN] ${message}`); - } else { - 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 debug = (obj: any, ...objs: any[]) => this.log(DEBUG, obj, objs); + public info = (obj: any, ...objs: any[]) => this.log(INFO, obj, objs); + public warn = (obj: any, ...objs: any[]) => this.log(WARN, obj, objs); + public error = (obj: any, ...objs: any[]) => this.log(ERROR, obj, objs); public getDate = () => { return new Date().toISOString(); diff --git a/src/library/index.ts b/src/library/index.ts new file mode 100644 index 0000000..23067a8 --- /dev/null +++ b/src/library/index.ts @@ -0,0 +1 @@ +export * from './Logging';