diff --git a/src/hooks/useLogging.ts b/src/hooks/useLogging.ts index d772565..a565d8d 100644 --- a/src/hooks/useLogging.ts +++ b/src/hooks/useLogging.ts @@ -1,13 +1,13 @@ import { useRef } from 'react'; -import Logging from '../library/Logging'; +import Logging, { ILoggingOptions } 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)); +export const useLogging = (options?: ILoggingOptions): [Logging] => { + const { current: logging } = useRef(new Logging(options)); return [logging]; }; diff --git a/src/library/Logging.ts b/src/library/Logging.ts index 6657229..5c4a0e0 100644 --- a/src/library/Logging.ts +++ b/src/library/Logging.ts @@ -1,31 +1,53 @@ -const DEBUG = 'DEBUG'; -const INFO = 'INFO'; -const WARN = 'WARN'; -const ERROR = 'ERROR'; +export const DEBUG = 'DEBUG'; +export const INFO = 'INFO'; +export const WARN = 'WARN'; +export const ERROR = 'ERROR'; + +export interface ILoggingOptions { + namespace?: string; + debug?: boolean; + applyStyles?: boolean; + styles?: { [key: string]: string }; +} export class Logging { - private namespace = 'Default'; + private namespace: string; + private applyStyles: boolean; + private environment: string; - constructor(namespace?: string) { - if (namespace) this.namespace = namespace; + private styles: { [key: string]: string } = { + DEBUG: 'display: inline-block; background-color: Blue; color: white; font-weight: bold; padding: 3px 7px 3px 7px; border-radius: 3px;', + INFO: 'display: inline-block; background-color: Green; color: black; font-weight: bold; padding: 3px 7px 3px 7px; border-radius: 3px;', + WARN: 'display: inline-block; background-color: gold; color: black; font-weight: bold; padding: 3px 7px 3px 7px; border-radius: 3px;', + ERROR: 'display: inline-block; background-color: Red; color: black; font-weight: bold; padding: 3px 7px 3px 7px; border-radius: 3px;' + }; + + constructor(options?: ILoggingOptions) { + this.namespace = options?.namespace || 'Default'; + this.applyStyles = options?.applyStyles || false; + this.environment = process.env.NODE_ENV || 'development'; + + if (options?.styles && this.applyStyles) { + for (let key in options.styles) { + if (this.styles[key]) this.styles[key] = options.styles[key]; + } + } } public log = (key: string, obj: any, ...objs: any[]) => { - console.info(`[${this.getDate()}] [${this.namespace}] [${key}] %c${obj}`, ...objs); + if (this.environment === 'production') return; + + if (this.applyStyles) { + console.log(`[${new Date().toISOString()}] [${this.namespace}] %c[${key}] ${obj}`, this.styles[key], ...objs); + } else { + console.log(`[${new Date().toISOString()}] [${this.namespace}] [${key}] ${obj}`, ...objs); + } }; - 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(); - }; - - public setNamespace = (namespace: string) => { - this.namespace = namespace; - }; + 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); } export default Logging;