-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve pr reviews and add client logger with readme update
- Loading branch information
1 parent
e503599
commit df0fc65
Showing
8 changed files
with
111 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export interface ILoggerOptions { | ||
logLevel?: string, | ||
logFilePrefix?: string | ||
} | ||
export interface ILogger { | ||
private instance?: any; | ||
private logger?: WinstonLogger; // The Winston logger instance | ||
logLevel?: string; // The current logging level | ||
logFilePrefix?: string; | ||
// Method to get the logger instance | ||
getLogger(loggerOptions: ILoggerOptions): WinstonLogger; | ||
} | ||
|
||
export interface IClientLogMethods { | ||
error: (message: string, ...metadata: any[]) => void; | ||
warn: (message: string, ...metadata: any[]) => void; | ||
info: (message: string, ...metadata: any[]) => void; | ||
debug: (message: string, ...metadata: any[]) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,55 @@ | ||
import { createLogger, format, transports, Logger as WinstonLogger } from "winston"; | ||
|
||
import { IClientLogMethods, ILoggerOptions } from "index"; | ||
|
||
export default class ClientLogger { | ||
private static instance: ClientLogger; | ||
private logger: WinstonLogger; | ||
private logLevel: string; | ||
|
||
private constructor(logLevel: string = "info") { | ||
|
||
this.logLevel = logLevel; | ||
|
||
this.logger = createLogger({ | ||
level: this.logLevel, | ||
format: format.combine( | ||
format.colorize(), | ||
format.timestamp({ | ||
format: "DD/MMM/YYYY HH:mm:ss", | ||
}), | ||
format.printf(({ timestamp, level, message, ...metadata }) => { | ||
const msg = `[${timestamp}] "${level}" ${message}`; | ||
const metaString = Object.keys(metadata).length ? ` ${JSON.stringify(metadata)}` : ""; | ||
return msg + metaString; | ||
}) | ||
), | ||
transports: [ | ||
new transports.Console({ handleExceptions: true }) | ||
], | ||
}); | ||
|
||
this.logger.transports.forEach((transport) => { | ||
transport.on("error", (err) => { | ||
// Handle the error, log it, or notify as necessary | ||
console.error(`Logging transport error: Console`, err); | ||
}); | ||
}); | ||
|
||
private logLevels: string[]; | ||
logMethods: IClientLogMethods; | ||
static instance: any; | ||
|
||
constructor(loggerOptions: ILoggerOptions = { logLevel: "info", logFilePrefix: "log" }) { | ||
this.logLevel = loggerOptions.logLevel || 'info'; | ||
this.logMethods = { | ||
error: this.logWithLevel.bind(this, "error"), | ||
warn: this.logWithLevel.bind(this, "warn"), | ||
info: this.logWithLevel.bind(this, "info"), | ||
debug: this.logWithLevel.bind(this, "debug"), | ||
}; | ||
this.logLevels = ["error", "warn", "info", "debug"]; | ||
} | ||
|
||
private static getInstance(logLevel?: string) { | ||
static getInstance(loggerOptions?: ILoggerOptions) { | ||
if (!ClientLogger.instance) { | ||
ClientLogger.instance = new ClientLogger(logLevel); | ||
} | ||
ClientLogger.instance = new ClientLogger(loggerOptions); | ||
} | ||
return ClientLogger.instance; | ||
} | ||
|
||
public static getLogger(logLevel?: string) { | ||
const instance = ClientLogger.getInstance(logLevel); | ||
return instance.logger | ||
public static getLogger(loggerOptions?: ILoggerOptions): IClientLogMethods { | ||
const instance = this.getInstance(loggerOptions); | ||
return instance.logMethods; | ||
} | ||
|
||
} | ||
|
||
logWithLevel(level: string, message: string, ...metadata: any[]) { | ||
if (this.logLevels.indexOf(level) <= this.logLevels.indexOf(this.logLevel)) { | ||
const timestamp = new Date().toISOString(); | ||
const formattedMessage = `[${timestamp}] "${level.toUpperCase()}" ${message}`; | ||
const metaString = metadata.length ? ` ${JSON.stringify(metadata)}` : ""; | ||
|
||
// Override to console.log equivalent | ||
switch (level) { | ||
case "error": | ||
console.error(formattedMessage + metaString); | ||
break; | ||
case "warn": | ||
console.warn(formattedMessage + metaString); | ||
break; | ||
case "info": | ||
console.info(formattedMessage + metaString); | ||
break; | ||
case "debug": | ||
console.log(formattedMessage + metaString); | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
// src/index.ts | ||
export { default } from './server-logger'; | ||
// export { default as Logger} from './server-logger' | ||
// export { default as ClientLogger } from './client-logger' | ||
|
||
export {default} from './logger'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,16 @@ | ||
import { Logger as WinstonLogger } from 'winston'; | ||
|
||
export interface ILogger { | ||
logger: WinstonLogger; // The Winston logger instance | ||
logLevel?: string; // The current logging level | ||
logFilePrefix?: string; | ||
|
||
// Method to get the logger instance | ||
getLogger(logLevel?: string, logFilePrefix?: string): WinstonLogger; | ||
} | ||
|
||
import { ILogger } from 'index'; | ||
|
||
let Logger: ILogger; | ||
|
||
if (typeof window !== "undefined") { | ||
// Client-side logic | ||
console.log("inside client logger import") | ||
Logger = require('./client-logger').default; | ||
console.log("inside client logger import"); | ||
const { default: ClientLogger } = require('./client-logger'); | ||
Logger = ClientLogger; | ||
} else { | ||
// Server-side logic | ||
Logger = require('./server-logger').default; | ||
const { default: ServerLogger } = require('./server-logger'); | ||
Logger = ServerLogger; | ||
} | ||
|
||
export default Logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters