Skip to content

Commit

Permalink
refactor: refactor logger (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin authored Feb 25, 2025
1 parent 172b183 commit 1f75b8d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
16 changes: 4 additions & 12 deletions apps/api/src/app/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,13 @@ import {
usdServiceSymbol,
} from '@cowprotocol/services';
import ms from 'ms';
import pino from 'pino';
import { Logger, logger } from '../logger';

const DEFAULT_CACHE_VALUE_SECONDS = ms('2min') / 1000; // 2min cache time by default for values
const DEFAULT_CACHE_NULL_SECONDS = ms('30min') / 1000; // 30min cache time by default for NULL values (when the repository isn't known)

const CACHE_TOKEN_INFO_SECONDS = ms('24h') / 1000; // 24h

// Configure the logger
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
formatters: {
level: (label) => ({ level: label }),
},
});

function getErc20Repository(cacheRepository: CacheRepository): Erc20Repository {
return new Erc20RepositoryCache(
new Erc20RepositoryViem(viemClients),
Expand All @@ -66,7 +58,7 @@ function getErc20Repository(cacheRepository: CacheRepository): Erc20Repository {
);
}

function getCacheRepository(_apiContainer: Container): CacheRepository {
function getCacheRepository(): CacheRepository {
if (redisClient) {
return new CacheRepositoryRedis(redisClient);
}
Expand Down Expand Up @@ -150,10 +142,10 @@ function getApiContainer(): Container {
const apiContainer = new Container();

// Bind logger
apiContainer.bind<pino.Logger>('Logger').toConstantValue(logger);
apiContainer.bind<Logger>('Logger').toConstantValue(logger);

// Repositories
const cacheRepository = getCacheRepository(apiContainer);
const cacheRepository = getCacheRepository();
const erc20Repository = getErc20Repository(cacheRepository);
const simulationRepository = getSimulationRepository();
const tokenHolderRepository = getTokenHolderRepository(cacheRepository);
Expand Down
17 changes: 17 additions & 0 deletions apps/api/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pino from 'pino';

export type Logger = pino.Logger;

const loggerConfigEnv =
process.env.NODE_ENV === 'production'
? {}
: {
transport: {
target: 'pino-pretty',
},
};

export const logger = pino({
...loggerConfigEnv,
level: process.env.LOG_LEVEL ?? 'info',
});
16 changes: 1 addition & 15 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import Fastify from 'fastify';
import { app } from './app/app';
import pino from 'pino';

const loggerConfigEnv =
process.env.NODE_ENV === 'production'
? {}
: {
transport: {
target: 'pino-pretty',
},
};

const logger = pino({
...loggerConfigEnv,
level: process.env.LOG_LEVEL ?? 'info',
});
import { logger } from './logger';

const host = process.env.HOST ?? 'localhost';
const port = process.env.PORT ? Number(process.env.PORT) : 3001;
Expand Down

0 comments on commit 1f75b8d

Please sign in to comment.