Simplifying the logging for now
This commit is contained in:
parent
8d790378b9
commit
3a3a6eab00
@ -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));
|
||||
|
||||
|
@ -1 +1,2 @@
|
||||
export * from './hooks';
|
||||
export * from './library';
|
||||
|
@ -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();
|
||||
|
1
src/library/index.ts
Normal file
1
src/library/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './Logging';
|
Loading…
x
Reference in New Issue
Block a user