Skip to content

Commit

Permalink
fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mhd-hi committed Jul 26, 2024
1 parent f0ea992 commit 0f0d180
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/common/utils/pdf/fileUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import fs from 'fs';
import path from 'path';
Expand All @@ -7,6 +7,8 @@ import path from 'path';
export class FileUtil {
constructor(private configService: ConfigService) {}

private logger = new Logger(FileUtil.name);

public writeDataToFile<T>(data: T, fileName: string): Promise<string | null> {
const pdfOutputPath =
this.configService.get<string>('pdfOutputPath') ??
Expand All @@ -31,12 +33,13 @@ export class FileUtil {
JSON.stringify(data, urlDecodeReplacer, 2),
(err) => {
if (err) {
console.error('Error encountered while writing file: ', err);
this.logger.error('Error encountered while writing file: ', err);
reject(err);
} else {
console.log(
this.logger.log(
`File "${fileName}" successfully written to "${filePath}"`,
);

resolve(filePath);
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { Output, Page, Text } from 'pdf2json';
import { firstValueFrom } from 'rxjs';

Expand All @@ -16,6 +16,8 @@ export class HoraireCoursService {

constructor(private httpService: HttpService) {}

private logger = new Logger(HoraireCoursService.name);

public async parsePdfFromUrl(pdfUrl: string) {
try {
const response = await firstValueFrom(
Expand Down Expand Up @@ -72,8 +74,7 @@ export class HoraireCoursService {

return serializedCourses;
} catch (err) {
console.error('Error parsing pdf data: ' + err);
console.log(err);
this.logger.error('Error parsing pdf data: ' + err);
throw new Error('Error processing PDF data: ' + err);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/course-instance/course-instance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PrismaService } from '../prisma/prisma.service';
export class CourseInstanceService {
constructor(private readonly prisma: PrismaService) {}

private logger = new Logger('CourseInstance service');
private logger = new Logger(CourseInstanceService.name);

public getCourseInstance(
courseInstanceWhereUniqueInput: Prisma.CourseInstanceWhereUniqueInput,
Expand All @@ -21,6 +21,7 @@ export class CourseInstanceService {

public async getAllCourseInstances(): Promise<CourseInstance[]> {
this.logger.log('courseInstances');

const courseInstances = await this.prisma.courseInstance.findMany();
return courseInstances;
}
Expand All @@ -29,6 +30,7 @@ export class CourseInstanceService {
sessionIds: string[],
): Promise<CourseInstance[]> {
this.logger.log('getCourseInstancesBySessions', JSON.stringify(sessionIds));

return this.prisma.courseInstance.findMany({
where: {
sessionId: {
Expand Down
2 changes: 1 addition & 1 deletion src/course-prerequisite/course-prerequisite.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PrismaService } from '../prisma/prisma.service';
export class CoursePrerequisiteService {
constructor(private readonly prisma: PrismaService) {}

private logger = new Logger('CoursePrerequisite service');
private logger = new Logger(CoursePrerequisiteService.name);

public async getPrerequisites(courseId: string) {
this.logger.log('coursePrerequisiteById');
Expand Down
3 changes: 2 additions & 1 deletion src/course/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { PrismaService } from '../prisma/prisma.service';
export class CourseService {
constructor(private readonly prisma: PrismaService) {}

private logger = new Logger('Course service');
private logger = new Logger(CourseService.name);

public async getCourse(
courseWhereUniqueInput: Prisma.CourseWhereUniqueInput,
): Promise<Course | null> {
this.logger.log('courseById', courseWhereUniqueInput);

const course = await this.prisma.course.findUnique({
where: courseWhereUniqueInput,
});
Expand Down
2 changes: 1 addition & 1 deletion src/program/program.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PrismaService } from '../prisma/prisma.service';
export class ProgramService {
constructor(private readonly prisma: PrismaService) {}

private logger = new Logger('ProgramService');
private logger = new Logger(ProgramService.name);

public async getProgram(
programWhereUniqueInput: Prisma.ProgramWhereUniqueInput,
Expand Down
6 changes: 5 additions & 1 deletion src/session/session.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { Prisma, Session, Trimester } from '@prisma/client';

import { PrismaService } from '../prisma/prisma.service';
Expand All @@ -7,7 +7,11 @@ import { PrismaService } from '../prisma/prisma.service';
export class SessionService {
constructor(private readonly prisma: PrismaService) {}

private logger = new Logger(SessionService.name);

public async getSession(id: string): Promise<Session | null> {
this.logger.log('getSession', id);

return this.prisma.session.findUnique({
where: { id },
});
Expand Down

0 comments on commit 0f0d180

Please sign in to comment.