-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
417 additions
and
300 deletions.
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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
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,22 @@ | ||
{ | ||
"name": "@ebo-agent/shared", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "./dist/index.js", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.build.json", | ||
"lint": "eslint .", | ||
"lint:fix": "pnpm lint --fix", | ||
"format": "prettier --check .", | ||
"format:fix": "prettier --write .", | ||
"test": "vitest run", | ||
"coverage": "vitest run --coverage" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"winston": "3.13.1" | ||
} | ||
} |
Empty file.
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,3 @@ | ||
export * from "./constants.js"; | ||
export * from "./logger.js"; | ||
export * from "./types/index.js"; |
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,59 @@ | ||
import { createLogger, format, transports, Logger as WinstonLogger } from "winston"; | ||
|
||
import { ILogger, LogLevel } from "./index.js"; | ||
|
||
export class Logger implements ILogger { | ||
private logger: WinstonLogger; | ||
private static instance: Logger | null; | ||
|
||
private constructor(private level: LogLevel) { | ||
this.logger = createLogger({ | ||
level: this.level, | ||
format: format.combine( | ||
format.colorize(), | ||
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), | ||
format.errors({ stack: true }), | ||
format.printf(({ level, message, timestamp, stack }) => { | ||
return `${timestamp} ${level}: ${stack || message}`; | ||
}), | ||
), | ||
transports: [new transports.Console()], | ||
}); | ||
} | ||
/** | ||
* Returns the instance of the Logger class. | ||
* @param level The log level to be used by the logger. | ||
* @returns The instance of the Logger class. | ||
*/ | ||
public static getInstance(level?: LogLevel): Logger { | ||
if (!Logger.instance) { | ||
if (!level) { | ||
throw new Error("Initial configuration is required for the first instantiation."); | ||
} | ||
Logger.instance = new Logger(level); | ||
} else { | ||
Logger.instance.warn( | ||
`Logger instance already exists. Returning the existing instance with log level ${Logger.instance.level}.`, | ||
); | ||
} | ||
|
||
return Logger.instance; | ||
} | ||
|
||
info(message: string) { | ||
this.logger.info(message); | ||
} | ||
error(error: Error | string): void { | ||
if (error instanceof Error) { | ||
this.logger.error(error); | ||
} else { | ||
this.logger.error(new Error(error)); | ||
} | ||
} | ||
warn(message: string) { | ||
this.logger.warn(message); | ||
} | ||
debug(message: string) { | ||
this.logger.debug(message); | ||
} | ||
} |
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 @@ | ||
export * from "./logger.js"; |
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,11 @@ | ||
/** | ||
* Generic logger interface. | ||
*/ | ||
export interface ILogger { | ||
error: (error: Error | string) => void; | ||
info: (message: string) => void; | ||
warn: (message: string) => void; | ||
debug: (message: string) => void; | ||
} | ||
|
||
export type LogLevel = "error" | "warn" | "info" | "debug"; |
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,24 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { Logger } from "../src/logger.js"; | ||
|
||
describe("Logger Singleton", () => { | ||
it("creates a logger instance with the given log level", () => { | ||
const logger = Logger.getInstance("info"); | ||
expect(logger).toBeInstanceOf(Logger); | ||
expect(() => Logger.getInstance()).not.toThrow(); | ||
}); | ||
|
||
it("throws an error if no log level is provided on first instantiation", () => { | ||
Logger["instance"] = null; | ||
expect(() => Logger.getInstance()).toThrow( | ||
new Error("Initial configuration is required for the first instantiation."), | ||
); | ||
}); | ||
|
||
it("returns the same instance if called multiple times", () => { | ||
const logger1 = Logger.getInstance("info"); | ||
const logger2 = Logger.getInstance("warn"); | ||
expect(logger1).toBe(logger2); | ||
}); | ||
}); |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.build.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"outDir": "dist" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "build", "tests", "vitest.config.ts"] | ||
} |
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,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*"] | ||
} |
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,22 @@ | ||
import path from "path"; | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true, // Use Vitest's global API without importing it in each file | ||
environment: "node", // Use the Node.js environment | ||
include: ["tests/**/*.spec.ts"], // Include test files | ||
exclude: ["node_modules", "dist"], // Exclude certain directories | ||
coverage: { | ||
provider: "v8", | ||
reporter: ["text", "json", "html"], // Coverage reporters | ||
exclude: ["node_modules", "dist", "src/**/*.d.ts"], // Files to exclude from coverage | ||
}, | ||
}, | ||
resolve: { | ||
alias: { | ||
// Setup path alias based on tsconfig paths | ||
"@": path.resolve(__dirname, "src"), | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.