Skip to content

Commit

Permalink
refactor(be): logType -> logLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Oct 18, 2024
1 parent a3817a3 commit d12fe1d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Warnings:
- You are about to drop the column `type` on the `Log` table. All the data in the column will be lost.
- Added the required column `level` to the `Log` table without a default value. This is not possible if the table is not empty.
*/
-- CreateEnum
CREATE TYPE "LogLevel" AS ENUM ('log', 'error', 'warn', 'debug', 'verbose', 'fatal');

-- AlterTable
ALTER TABLE "Log" DROP COLUMN "type",
ADD COLUMN "level" "LogLevel" NOT NULL;

-- DropEnum
DROP TYPE "LogType";
12 changes: 8 additions & 4 deletions apps/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@ model PlatformsOnRoutes {

model Log {
id BigInt @id @default(autoincrement())
type LogType
level LogLevel
message String
trace Json?
createdAt DateTime @default(now())
}

enum LogType {
INFO
ERROR
enum LogLevel {
log
error
warn
debug
verbose
fatal
}

enum VehicleType {
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/enums/log.enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { LogType } from "@prisma/client";
export { LogLevel } from "@prisma/client";

export enum StopSyncTrigger {
CRON = "CRON",
Expand Down
6 changes: 3 additions & 3 deletions apps/backend/src/modules/import/import.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from "@nestjs/common";
import { unique } from "radash";

import { LogMessage, LogType, StopSyncTrigger } from "src/enums/log.enum";
import { LogLevel, LogMessage, StopSyncTrigger } from "src/enums/log.enum";
import { metroLine } from "src/enums/metro.enum";
import {
pidPlatformsSchema,
Expand Down Expand Up @@ -207,15 +207,15 @@ export class ImportService {
})),
});

await this.logger.createLog(LogType.INFO, LogMessage.IMPORT_STOPS, {
await this.logger.createLog(LogLevel.log, LogMessage.IMPORT_STOPS, {
trigger,
duration: Date.now() - start,
stops: stopsData.stopGroups.length,
platforms: platforms.length,
});
} catch (error) {
await this.logger.createLog(
LogType.ERROR,
LogLevel.error,
LogMessage.IMPORT_STOPS,
{
trigger,
Expand Down
82 changes: 44 additions & 38 deletions apps/backend/src/modules/logger/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ConsoleLogger, Injectable, Scope } from "@nestjs/common";
import { Prisma } from "@prisma/client";

import { Environment } from "src/enums/environment.enum";
import { LogMessage, LogType, RestLogStatus } from "src/enums/log.enum";
import { LogLevel, LogMessage, RestLogStatus } from "src/enums/log.enum";
import { PrismaService } from "src/modules/prisma/prisma.service";

@Injectable({ scope: Scope.TRANSIENT })
Expand All @@ -14,67 +14,73 @@ export class LoggerService extends ConsoleLogger {
}

async error(message: string, trace: string): Promise<void> {
if (process.env.NODE_ENV === Environment.TEST) return;

try {
await this.prisma.log.create({
data: {
type: LogType.ERROR,
message,
trace,
},
});
} catch (error) {
super.error("Failed to log error", error);
}
await this.createLogHandler(LogLevel.error, message, trace);

super.error(message, trace);
}

async createLog(
type: LogType,
type: LogLevel,
message: LogMessage,
trace:
| Prisma.NullableJsonNullValueInput
| Prisma.InputJsonValue = Prisma.JsonNull,
): Promise<void> {
if (process.env.NODE_ENV === Environment.TEST) return;

try {
await this.prisma.log.create({
data: {
type,
message,
trace,
},
});
} catch (error) {
super.error("Failed to log error", error);
}
return this.createLogHandler(type, message, trace);
}

async createRestLog(
endpoint: string,
duration: number,
querySearch: Record<string, string | string[]> | null = null,
): Promise<void> {
if (process.env.NODE_ENV === Environment.TEST) return;

await this.createLog(LogType.INFO, LogMessage.REST, {
endpoint,
duration,
status: RestLogStatus.SUCCESS,
querySearch,
});
return this.createRestLogHandler(endpoint, true, duration, querySearch);
}

async createRestErrorLog(
endpoint,
querySearch: Record<string, string | string[]> | null = null,
): Promise<void> {
await this.createLog(LogType.INFO, LogMessage.REST, {
return this.createRestLogHandler(endpoint, false, null, querySearch);
}

private async createLogHandler(
level: LogLevel,
message: string,
trace:
| Prisma.NullableJsonNullValueInput
| Prisma.InputJsonValue = Prisma.JsonNull,
) {
if (process.env.NODE_ENV === Environment.TEST) return;

await this.prisma.log
.create({
data: {
level,
message,
trace,
},
})
.catch((error) => {
super.error("Failed to log error", error);
});
}

private async createRestLogHandler(
endpoint: string,
success: boolean,
duration: number | null,
querySearch: Record<string, string | string[]> | null = null,
): Promise<void> {
const status = success
? RestLogStatus.SUCCESS
: RestLogStatus.INVALID_REQUEST;
const durationObject = duration ? { duration } : {};

return this.createLog(LogLevel.log, LogMessage.REST, {
endpoint,
status: RestLogStatus.INVALID_REQUEST,
status,
...durationObject,
querySearch,
});
}
Expand Down

0 comments on commit d12fe1d

Please sign in to comment.