Skip to content

Commit

Permalink
feat: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
0xkenj1 committed Jul 29, 2024
1 parent c2dc309 commit 95ed302
Show file tree
Hide file tree
Showing 16 changed files with 417 additions and 300 deletions.
File renamed without changes.
3 changes: 0 additions & 3 deletions packages/blocknumber/.prettierignore

This file was deleted.

3 changes: 2 additions & 1 deletion packages/blocknumber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"viem": "2.17.10"
"viem": "2.17.10",
"@ebo-agent/shared": "workspace:*"
}
}
12 changes: 7 additions & 5 deletions packages/blocknumber/src/providers/evmBlockNumberProvider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ILogger } from "@ebo-agent/shared";
import { Block, PublicClient } from "viem";

import {
Expand Down Expand Up @@ -46,6 +47,7 @@ export class EvmBlockNumberProvider implements BlockNumberProvider {
constructor(
client: PublicClient,
searchConfig: { blocksLookback?: bigint; deltaMultiplier?: bigint },
private logger: ILogger,
) {
this.client = client;
this.searchConfig = {
Expand All @@ -64,7 +66,7 @@ export class EvmBlockNumberProvider implements BlockNumberProvider {

this.validateBlockNumber(upperBoundBlock);

logger.info(
this.logger.info(
`Working with latest block (number: ${upperBoundBlock.number}, timestamp: ${upperBoundBlock.timestamp})...`,
);

Expand Down Expand Up @@ -134,14 +136,14 @@ export class EvmBlockNumberProvider implements BlockNumberProvider {

const baseStep = (lastBlock.number - candidateBlockNumber) * deltaMultiplier;

logger.info("Calculating lower bound for binary search...");
this.logger.info("Calculating lower bound for binary search...");

let searchCount = 0n;
while (candidateBlockNumber >= 0) {
const candidate = await this.client.getBlock({ blockNumber: candidateBlockNumber });

if (candidate.timestamp < timestamp) {
logger.info(`Estimated lower bound at block ${candidate.number}.`);
this.logger.info(`Estimated lower bound at block ${candidate.number}.`);

return candidate;
}
Expand All @@ -167,15 +169,15 @@ export class EvmBlockNumberProvider implements BlockNumberProvider {
* @returns the estimated block time
*/
private async estimateBlockTime(lastBlock: BlockWithNumber, blocksLookback: bigint) {
logger.info("Estimating block time...");
this.logger.info("Estimating block time...");

const pastBlock = await this.client.getBlock({
blockNumber: lastBlock.number - BigInt(blocksLookback),
});

const estimatedBlockTime = (lastBlock.timestamp - pastBlock.timestamp) / blocksLookback;

logger.info(`Estimated block time: ${estimatedBlockTime}.`);
this.logger.info(`Estimated block time: ${estimatedBlockTime}.`);

return estimatedBlockTime;
}
Expand Down
22 changes: 22 additions & 0 deletions packages/shared/package.json
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.
3 changes: 3 additions & 0 deletions packages/shared/src/index.ts
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";
59 changes: 59 additions & 0 deletions packages/shared/src/logger.ts
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);
}
}
1 change: 1 addition & 0 deletions packages/shared/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./logger.js";
11 changes: 11 additions & 0 deletions packages/shared/src/types/logger.ts
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";
24 changes: 24 additions & 0 deletions packages/shared/tests/logger.spec.ts
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);
});
});
9 changes: 9 additions & 0 deletions packages/shared/tsconfig.build.json
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"]
}
4 changes: 4 additions & 0 deletions packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
}
22 changes: 22 additions & 0 deletions packages/shared/vitest.config.ts
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"),
},
},
});
Loading

0 comments on commit 95ed302

Please sign in to comment.