Skip to content

Commit

Permalink
Add interface for logger, add logger factories
Browse files Browse the repository at this point in the history
  • Loading branch information
madnoberson committed Sep 6, 2024
1 parent 22b3f1e commit 4bfc5f7
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/application/commandProcessors/createUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Logger } from "pino";

import {
InvalidUserNameError,
InvalidEmailError,
Expand All @@ -15,6 +13,7 @@ import {
UserTelegramIsAlreadyTakenError,
UserGateway,
TransactionManager,
Logger,
} from "src/application/common";
import { CreateUserCommand } from "src/application/commands";

Expand Down Expand Up @@ -114,8 +113,8 @@ class CreateUserLoggingProcessor {

async process(command: CreateUserCommand): Promise<void> {
this.logger.debug(
{command: command},
"'Create user' command processing started",
{command: command},
)

try {
Expand Down Expand Up @@ -154,8 +153,8 @@ class CreateUserLoggingProcessor {
)
} else {
this.logger.error(
{"traceback": error.stack},
"Unexpected error occurred",
{"traceback": error.stack},
)
}
throw error
Expand Down
1 change: 1 addition & 0 deletions src/application/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./errors";
export * from "./valueObjects";
export * from "./commandProcessors";

export { type Logger } from "./logger";
export { type TransactionManager } from "./transactionManager";
6 changes: 6 additions & 0 deletions src/application/common/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Logger {
debug(message: string, obj?: object): void;
info(message: string, obj?: object): void;
warning(message: string, obj?: object): void;
error(message: string, obj?: object): void;
}
3 changes: 3 additions & 0 deletions src/infrastructure/logging/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./pino";

export { RealLogger } from "./logger";
22 changes: 22 additions & 0 deletions src/infrastructure/logging/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Logger as PinoLogger } from "pino";


export class RealLogger {
constructor(protected readonly pinoLogger: PinoLogger) {}

debug(message: string, obj?: object): void {
this.pinoLogger.debug(obj, message)
}

info(message: string, obj?: object): void {
this.pinoLogger.info(obj, message)
}

warning(message: string, obj?: object): void {
this.pinoLogger.warn(obj, message)
}

error(message: string, obj?: object): void {
this.pinoLogger.error(obj,message)
}
}
16 changes: 16 additions & 0 deletions src/infrastructure/logging/pino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Logger as PinoLogger, pino } from "pino";

import { OperationId } from "src/application";


export function pinoRootLoggerFactory(): PinoLogger {
return pino()
}


export function pinoLoggerFactory(
rootPinoLogger: PinoLogger,
operationId: OperationId,
): PinoLogger {
return rootPinoLogger.child({"operation_id": operationId})
}
21 changes: 18 additions & 3 deletions src/presentation/cli/handlers/createUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command, OptionValues } from "commander";
import { pino } from "pino";
import { v7 as uuid7 } from "uuid";

import {
UserId,
Expand All @@ -8,12 +8,20 @@ import {
TelegramValidator,
CreateUser,
} from "src/domain";
import { createUserFactory } from "src/application";
import {
OperationId,
createUserFactory,
} from "src/application";
import {
UserMapper,
postgresConfigFromEnv,
kyselyDatabaseFactory,
} from "src/infrastructure/database";
import {
pinoRootLoggerFactory,
pinoLoggerFactory,
RealLogger,
} from "src/infrastructure/logging";


export const createUserCommand = new Command("create-user")
Expand All @@ -38,11 +46,18 @@ async function createUser(options: OptionValues): Promise<void> {
telegramValidator: new TelegramValidator(),
})

const pinoRootLogger = pinoRootLoggerFactory()
const pinoLogger = pinoLoggerFactory(
pinoRootLogger,
new OperationId(uuid7()),
)
const realLogger = new RealLogger(pinoLogger)

const commandProcessor = createUserFactory({
createUser: createUser,
userGateway: userMapper,
txManager: {commit: async () => {}},
logger: pino(),
logger: realLogger,
})
await commandProcessor.process({
id: new UserId(options.id),
Expand Down

0 comments on commit 4bfc5f7

Please sign in to comment.