Skip to content

Commit

Permalink
Merge pull request #1134 from AletheiaFact/winston-logging-implementa…
Browse files Browse the repository at this point in the history
…tion

Implementation of winston logging library
  • Loading branch information
thesocialdev authored Apr 2, 2024
2 parents b3e5c1c + 34a56a9 commit 111d09e
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 50 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified .yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"socket.io": "^4.7.2",
"storybook": "^7.4.5",
"styled-components": "^5.3.0",
"winston": "^3.13.0",
"ws": "^8.13.0",
"xstate": "^4.32.1",
"y-protocols": "^1.0.5",
Expand Down
29 changes: 0 additions & 29 deletions server/logger.ts

This file was deleted.

8 changes: 5 additions & 3 deletions server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { join } from "path";
import { NestExpressApplication } from "@nestjs/platform-express";
import { ValidationPipe, Logger } from "@nestjs/common";
import { ValidationPipe } from "@nestjs/common";
import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger";
import loadConfig from "./configLoader";
import * as dotenv from "dotenv";
import { WinstonLogger } from "./winstonLogger";
const cookieParser = require("cookie-parser");
const mongoose = require("mongoose");
dotenv.config();
Expand All @@ -25,11 +26,13 @@ async function initApp() {
allowedHeaders: ["accept", "x-requested-with", "content-type"],
};

const logger = new Logger();
const logger = new WinstonLogger();

const app = await NestFactory.create<NestExpressApplication>(
AppModule.register(options),
{
bodyParser: false,
logger: logger,
cors: corsOptions,
}
);
Expand Down Expand Up @@ -68,7 +71,6 @@ async function initApp() {
});
await app.listen(options.port);
logger.log(
"info",
`${options.name} with PID ${process.pid} listening on ${
options.interface || "*"
}:${options.port}`
Expand Down
4 changes: 2 additions & 2 deletions server/personality/personality.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ClaimReviewModule } from "../claim-review/claim-review.module";
import { ClaimRevisionModule } from "../claim/claim-revision/claim-revision.module";
import { HistoryModule } from "../history/history.module";
import { PersonalityController } from "./personality.controller";
import Logger from "../logger";
import { WinstonLogger } from "../winstonLogger";
import { ViewModule } from "../view/view.module";
import { ConfigModule } from "@nestjs/config";
import { CaptchaModule } from "../captcha/captcha.module";
Expand All @@ -34,7 +34,7 @@ const PersonalityModel = MongooseModule.forFeature([
CaptchaModule,
],
exports: [PersonalityService],
providers: [UtilService, PersonalityService, Logger],
providers: [UtilService, PersonalityService, WinstonLogger],
controllers: [PersonalityController],
})
export class PersonalityModule {}
18 changes: 9 additions & 9 deletions server/seed.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { Logger } from "@nestjs/common";
import { NestExpressApplication } from "@nestjs/platform-express";
import { EmailService } from "./email/email.service";
import { ConfigService } from "@nestjs/config";
import { UsersService } from "./users/users.service";
import { UtilService } from "./util";
import loadConfig from "./configLoader";
import { WinstonLogger } from "./winstonLogger";

async function initApp() {
const options = loadConfig();

const logger = new Logger();
const logger = new WinstonLogger();
const app = await NestFactory.create<NestExpressApplication>(
AppModule.register(options)
);

logger.log("info", `AppModule loaded`);
logger.log(`AppModule loaded`);
const emailService = app.get(EmailService);
const configService = app.get(ConfigService);
const userService = app.get(UsersService);
Expand All @@ -28,7 +28,7 @@ async function initApp() {
return userService
.register({ ...userData, password })
.then(async (user) => {
logger.log("info", `${userData.email} seeded`);
logger.log(`${userData.email} seeded`);
if (userData.sendAuthDetails && !disableSMTP) {
const emailResponse = await emailService.sendEmail(
userData.email,
Expand All @@ -37,14 +37,14 @@ async function initApp() {
{ ...userData, password },
"./templates/userSeed.html"
);
logger.log("info", emailResponse);
logger.log("info", `E-mail sent to ${userData.email}`);
logger.log(emailResponse);
logger.log(`E-mail sent to ${userData.email}`);
}
return user;
})
.catch((e) => {
logger.log("error", e);
logger.log("info", `Error while seeding ${userData.email}`);
logger.error("error", e);
logger.log(`Error while seeding ${userData.email}`);
return null;
});
};
Expand All @@ -59,7 +59,7 @@ async function initApp() {
})
);

logger.log("info", "Seed is finished");
logger.log("Seed is finished");
await app.close();
}

Expand Down
5 changes: 2 additions & 3 deletions server/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { NestExpressApplication } from "@nestjs/platform-express";
import { NestFactory } from "@nestjs/core";
import { WebsocketModule } from "./yjs-websocket/websocket.module";
import { WsAdapter } from "@nestjs/platform-ws";
import { Logger } from "@nestjs/common";
import loadConfig from "./configLoader";
import { WinstonLogger } from "./winstonLogger";

async function initApp() {
const options = loadConfig();

const logger = new Logger();
const logger = new WinstonLogger();
const app = await NestFactory.create<NestExpressApplication>(
WebsocketModule.register(options)
);
Expand All @@ -18,7 +18,6 @@ async function initApp() {
await app.listen(options.port);

logger.log(
"info",
`${options.name} with PID ${process.pid} listening on ${
options.interface || "*"
}:${options.port}`
Expand Down
36 changes: 36 additions & 0 deletions server/winstonLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { LoggerService } from "@nestjs/common";
import * as winston from "winston";

export class WinstonLogger implements LoggerService {
private logger: winston.Logger;

constructor() {
this.logger = winston.createLogger({
transports: [
new winston.transports.Console({
format: winston.format.combine(winston.format.json()),
}),
],
});
}

log(message: string, context?: string) {
this.logger.info(message, { context });
}

error(message: string, trace: string, context?: string) {
this.logger.error(message, { trace, context });
}

warn(message: string, context?: string) {
this.logger.warn(message, { context });
}

debug(message: string, context?: string) {
this.logger.debug(message, { context });
}

verbose(message: string, context?: string) {
this.logger.verbose(message, { context });
}
}
Loading

0 comments on commit 111d09e

Please sign in to comment.