Skip to content

Commit

Permalink
resolve pr reviews and add client logger with readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurabhkmr98 committed Dec 12, 2024
1 parent e503599 commit df0fc65
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 72 deletions.
12 changes: 11 additions & 1 deletion packages/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ import PlaneLogger from "@plane/logger";
Use this for general application logs.

```typescript
const logger: Logger = PlaneLogger.getLogger("info", "log-file-prefix")
const loggerOptions: ILoggerOptions = { logLevel:"info", logFilePrefix: "log-file-prefix" }

import ClientLogger from "@plane/logger/client"
const logger = ClientLogger.getLogger(loggerOptions);
logger.log("test logs on web")


import ServerLogger from "@plane/logger/server"
const logger = ServerLogger.getLogger(loggerOptions);
logger.log("test logs on server")

logger.info("This is an info log");
logger.warn("This is a warning");
logger.error("This is an error");
Expand Down
19 changes: 19 additions & 0 deletions packages/logger/index.d.ts
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;
}
12 changes: 11 additions & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
"version": "1.0.0",
"description": "Logger shared across multiple apps internally",
"private": true,
"main": "./dist/index.js",
"main": "./dist/server-logger.js",
"browser": "./dist/client-logger.js",
"type":"module",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist/**"
],
"exports": {
".": {
"require": "./dist/server-logger.js",
"import": "./dist/index.js",
"browser": "./dist/client-logger.js"
},
"./client": "./dist/client-logger.js",
"./server": "./dist/server-logger.js"
},
"scripts": {
"build": "tsup ./src/index.ts --format esm,cjs --dts --external react --minify",
"lint": "eslint src --ext .ts,.tsx",
Expand Down
86 changes: 44 additions & 42 deletions packages/logger/src/client-logger.ts
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;
}
}
}
}
5 changes: 4 additions & 1 deletion packages/logger/src/index.ts
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';
20 changes: 6 additions & 14 deletions packages/logger/src/logger.ts
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;
23 changes: 12 additions & 11 deletions packages/logger/src/server-logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createLogger, format, transports, Logger as WinstonLogger } from "winston";
import { createLogger, format, LoggerOptions, transports, Logger as WinstonLogger } from "winston";
import winstonRotate from "winston-daily-rotate-file";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import fs from 'fs';
import { ILoggerOptions } from "index";

// Get current directory
const __filename = fileURLToPath(import.meta.url);
Expand All @@ -18,14 +19,14 @@ if (!fs.existsSync(logDirectory)) {


export default class Logger {
private static instance: Logger;
static instance?: Logger;
private logger: WinstonLogger;
private logLevel: string;
private logFilePrefix: string;
private logLevel?: string;
private logFilePrefix?: string;

private constructor(logLevel: string = "info", logFilePrefix: string = "plane-log") {
this.logLevel = logLevel;
this.logFilePrefix = logFilePrefix;
private constructor(loggerOptions: ILoggerOptions = { logLevel: "info", logFilePrefix: "plane-log" }) {
this.logLevel = loggerOptions.logLevel;
this.logFilePrefix = loggerOptions.logFilePrefix;

this.logger = createLogger({
level: this.logLevel,
Expand Down Expand Up @@ -71,15 +72,15 @@ export default class Logger {
}
}

private static getInstance(logLevel?: string, logFilePrefix?: string) {
private static getInstance(loggerOptions?: ILoggerOptions) {
if (!Logger.instance) {
Logger.instance = new Logger(logLevel, logFilePrefix);
Logger.instance = new Logger(loggerOptions);
}
return Logger.instance;
}

public static getLogger(logLevel?: string, logFilePrefix?: string) {
const instance = Logger.getInstance(logLevel, logFilePrefix);
public static getLogger(loggerOptions?: ILoggerOptions) {
const instance = Logger.getInstance(loggerOptions);
return instance.logger
}

Expand Down
6 changes: 4 additions & 2 deletions packages/logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"extends": "@plane/typescript-config/base.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "bundler",
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"outDir": "./dist",
"rootDir": "./src",
"baseUrl": ".",
Expand Down

0 comments on commit df0fc65

Please sign in to comment.