File

src/shared/utils/logger/session-log-store.service.ts

Index

Properties
  • Private Readonly mode
Methods

Constructor

constructor(logRepository: Repository, configService: ConfigService)
Parameters :
Name Type Optional
logRepository Repository<SessionLogEntry> No
configService ConfigService No

Methods

Async append
append(sessionId: string, level: SessionLogLevel, message: string, stage?: string, detail?: Record)

Append a log entry to the database if the store mode allows it.

Parameters :
Name Type Optional
sessionId string No
level SessionLogLevel No
message string No
stage string Yes
detail Record<string | unknown> Yes
Returns : Promise<void>
deleteBySessionId
deleteBySessionId(sessionId: string)

Delete all log entries for a session.

Parameters :
Name Type Optional
sessionId string No
Returns : Promise<void>
findBySessionId
findBySessionId(sessionId: string)

Retrieve all log entries for a session, ordered by timestamp.

Parameters :
Name Type Optional
sessionId string No
Returns : Promise<SessionLogEntry[]>

Properties

Private Readonly mode
Type : SessionStoreMode
import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import {
    SessionLogEntry,
    SessionLogLevel,
} from "../../../session/entities/session-log-entry.entity";

export type SessionStoreMode = "off" | "errors" | "all" | "verbose";

@Injectable()
export class SessionLogStoreService {
    private readonly mode: SessionStoreMode;

    constructor(
        @InjectRepository(SessionLogEntry)
        private readonly logRepository: Repository<SessionLogEntry>,
        private readonly configService: ConfigService,
    ) {
        this.mode =
            this.configService.getOrThrow<SessionStoreMode>(
                "LOG_SESSION_STORE",
            );
    }

    /**
     * Append a log entry to the database if the store mode allows it.
     */
    async append(
        sessionId: string,
        level: SessionLogLevel,
        message: string,
        stage?: string,
        detail?: Record<string, unknown>,
    ): Promise<void> {
        if (!sessionId) return;
        if (this.mode === "off") return;
        if (this.mode === "errors" && level === "info") return;
        // "all" and "verbose" store everything; verbose callers pass richer detail

        await this.logRepository.save({
            sessionId,
            level,
            message,
            stage,
            detail,
        });
    }

    /**
     * Retrieve all log entries for a session, ordered by timestamp.
     */
    findBySessionId(sessionId: string): Promise<SessionLogEntry[]> {
        return this.logRepository.find({
            where: { sessionId },
            order: { timestamp: "ASC" },
        });
    }

    /**
     * Delete all log entries for a session.
     */
    deleteBySessionId(sessionId: string): Promise<void> {
        return this.logRepository.delete({ sessionId }).then(() => undefined);
    }
}

results matching ""

    No results matching ""