Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: logger #32

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/api/src/api.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { MiddlewareConsumer, Module, NestModule } from "@nestjs/common";

import { LoggerModule } from "@zkchainhub/shared";

import { RequestLoggerMiddleware } from "./common/middleware/request.middleware";
import { MetricsController } from "./metrics/metrics.controller";

Expand All @@ -8,7 +10,7 @@ import { MetricsController } from "./metrics/metrics.controller";
* Here we import all required modules and register the controllers for the ZKchainHub API.
*/
@Module({
imports: [],
imports: [LoggerModule],
controllers: [MetricsController],
providers: [],
})
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { NestFactory } from "@nestjs/core";
import { setupOpenApiConfiguration } from "apps/api/src/docs";
import { WINSTON_MODULE_NEST_PROVIDER } from "nest-winston";

import { ApiModule } from "./api.module";

async function bootstrap() {
const app = await NestFactory.create(ApiModule);
app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));

setupOpenApiConfiguration(app);

Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/metrics/metrics.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { Test, TestingModule } from "@nestjs/testing";
import { WINSTON_MODULE_PROVIDER } from "nest-winston";
import { Logger } from "winston";

import { MetricsController } from "./metrics.controller";
import { getEcosystemInfo, getZKChainInfo } from "./mocks/metrics.mock";

export const mockLogger: Partial<Logger> = {
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
};

describe("MetricsController", () => {
let controller: MetricsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: WINSTON_MODULE_PROVIDER,
useValue: mockLogger,
},
],
controllers: [MetricsController],
}).compile();

Expand Down
5 changes: 4 additions & 1 deletion apps/api/src/metrics/metrics.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Controller, Get, Param } from "@nestjs/common";
import { Controller, Get, Inject, Param } from "@nestjs/common";
import { ApiResponse, ApiTags } from "@nestjs/swagger";
import { WINSTON_MODULE_PROVIDER } from "nest-winston";
import { Logger } from "winston";

import { ParsePositiveIntPipe } from "../common/pipes/parsePositiveInt.pipe";
import { ZKChainInfo } from "./dto/response";
Expand All @@ -11,6 +13,7 @@ import { getEcosystemInfo, getZKChainInfo } from "./mocks/metrics.mock";
* Controller for handling metrics related endpoints.
*/
export class MetricsController {
constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) {}
/**
* Retrieves the ecosystem information.
* @returns {Promise<EcosystemInfo>} The ecosystem information.
Expand Down
3 changes: 3 additions & 0 deletions libs/providers/src/providers.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Module } from "@nestjs/common";

import { LoggerModule } from "@zkchainhub/shared";

import { EvmProviderService } from "./providers";
import { ZKChainProviderService } from "./providers/zkChainProvider.service";

Expand All @@ -8,6 +10,7 @@ import { ZKChainProviderService } from "./providers/zkChainProvider.service";
* This module exports Services for interacting with EVM-based blockchains.
*/
@Module({
imports: [LoggerModule],
providers: [EvmProviderService, ZKChainProviderService],
exports: [EvmProviderService, ZKChainProviderService],
})
Expand Down
15 changes: 14 additions & 1 deletion libs/providers/src/providers/evmProvider.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createMock } from "@golevelup/ts-jest";
import { Test, TestingModule } from "@nestjs/testing";
import { parseAbi } from "abitype";
import { WINSTON_MODULE_PROVIDER } from "nest-winston";
import * as viem from "viem";
import { localhost } from "viem/chains";
import { Logger } from "winston";

import { DataDecodeException } from "@zkchainhub/providers/exceptions";
import {
Expand All @@ -20,6 +22,13 @@ jest.mock("viem", () => ({
http: jest.fn(),
}));

export const mockLogger: Partial<Logger> = {
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
};

describe("EvmProviderService", () => {
let viemProvider: EvmProviderService;
const testAbi = parseAbi([
Expand All @@ -31,12 +40,16 @@ describe("EvmProviderService", () => {
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
providers: [
{
provide: WINSTON_MODULE_PROVIDER,
useValue: mockLogger,
},
{
provide: EvmProviderService,
useFactory: () => {
const rpcUrl = "http://localhost:8545";
const chain = localhost;
return new EvmProviderService(rpcUrl, chain);
return new EvmProviderService(rpcUrl, chain, mockLogger as Logger);
},
},
],
Expand Down
4 changes: 3 additions & 1 deletion libs/providers/src/providers/evmProvider.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from "@nestjs/common";
import { Inject, Injectable, LoggerService } from "@nestjs/common";
import { AbiParameter } from "abitype";
import { WINSTON_MODULE_NEST_PROVIDER } from "nest-winston";
import {
Abi,
Address,
Expand Down Expand Up @@ -32,6 +33,7 @@ export class EvmProviderService {
constructor(
rpcUrl: string,
readonly chain: Chain,
@Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService,
) {
this.client = createPublicClient({
chain,
Expand Down
15 changes: 14 additions & 1 deletion libs/providers/src/providers/zkChainProvider.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
import { Test, TestingModule } from "@nestjs/testing";
import { WINSTON_MODULE_PROVIDER } from "nest-winston";
import { GetBlockReturnType } from "viem";
import { localhost } from "viem/chains";
import { GetL1BatchDetailsReturnType } from "viem/zksync";
import { Logger } from "winston";

import { InvalidArgumentException } from "@zkchainhub/providers/exceptions";

import { ZKChainProviderService } from "./zkChainProvider.service";

export const mockLogger: Partial<Logger> = {
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
};

describe("ZKChainProviderService", () => {
let zkProvider: ZKChainProviderService;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
providers: [
{
provide: WINSTON_MODULE_PROVIDER,
useValue: mockLogger,
},
{
provide: ZKChainProviderService,
useFactory: () => {
const rpcUrl = "http://localhost:8545";
const chain = localhost;
return new ZKChainProviderService(rpcUrl, chain);
return new ZKChainProviderService(rpcUrl, chain, mockLogger as Logger);
},
},
],
Expand Down
11 changes: 8 additions & 3 deletions libs/providers/src/providers/zkChainProvider.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from "@nestjs/common";
import { Inject, Injectable, LoggerService } from "@nestjs/common";
import { WINSTON_MODULE_NEST_PROVIDER } from "nest-winston";
import { Chain, Client, createClient, http, HttpTransport } from "viem";
import { GetL1BatchDetailsReturnType, PublicActionsL2, publicActionsL2 } from "viem/zksync";

Expand All @@ -12,8 +13,12 @@ import { EvmProviderService } from "@zkchainhub/providers/providers/evmProvider.
export class ZKChainProviderService extends EvmProviderService {
private zkClient: Client<HttpTransport, Chain, undefined, undefined, PublicActionsL2>;

constructor(rpcUrl: string, chain: Chain) {
super(rpcUrl, chain);
constructor(
rpcUrl: string,
chain: Chain,
@Inject(WINSTON_MODULE_NEST_PROVIDER) logger: LoggerService,
) {
super(rpcUrl, chain, logger);
this.zkClient = createClient({ chain, transport: http(rpcUrl) }).extend(publicActionsL2());
}

Expand Down
Empty file added libs/shared/src/constants.ts
Empty file.
2 changes: 2 additions & 0 deletions libs/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./types";
export * from "./logger";
export * from "./constants";
1 change: 1 addition & 0 deletions libs/shared/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./logger.module";
28 changes: 28 additions & 0 deletions libs/shared/src/logger/logger.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Module } from "@nestjs/common";
import { WinstonModule } from "nest-winston";
import * as winston from "winston";

@Module({
imports: [
WinstonModule.forRoot({
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp({
format: "YYYY-MM-DD HH:mm:ss",
}),
winston.format.errors({ stack: true }),
winston.format.colorize(),
winston.format.printf(
({ timestamp, level, message, stack }: Record<string, string>) => {
return `${timestamp} ${level}: ${stack ?? message ?? ""}`;
},
),
),
}),
],
}),
],
exports: [WinstonModule],
})
export class LoggerModule {}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
"@nestjs/platform-express": "10.0.0",
"@nestjs/swagger": "7.4.0",
"abitype": "1.0.5",
"nest-winston": "1.9.7",
"reflect-metadata": "0.1.13",
"rxjs": "7.8.1",
"viem": "2.17.5"
"viem": "2.17.5",
"winston": "3.13.1"
},
"devDependencies": {
"@commitlint/config-conventional": "19.2.2",
Expand All @@ -44,10 +46,10 @@
"@types/jest": "29.5.2",
"@types/node": "20.3.1",
"@types/supertest": "6.0.0",
"@typescript-eslint/eslint-plugin": "7.0.0",
"@typescript-eslint/parser": "7.0.0",
"@typescript-eslint/eslint-plugin": "7.18.0",
"@typescript-eslint/parser": "7.18.0",
"commitlint": "19.3.0",
"eslint": "8.42.0",
"eslint": "8.56.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-prettier": "5.0.0",
"husky": "9.0.11",
Expand Down
Loading