add custom logger

This commit is contained in:
Roland Schneider 2025-10-29 09:15:20 +01:00
parent 7bf514b2aa
commit 5c37de40c6
5 changed files with 51 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import { AuthModule } from './auth/auth.module';
import { User } from './entity/user';
import { UserGroup } from './entity/user-group';
import { UserRole } from './entity/user-role';
import { LoggerModule } from './logger/logger.module';
const moduleTypeOrm = TypeOrmModule.forRootAsync({
imports: [ConfigModule],
@ -28,7 +29,13 @@ const moduleTypeOrm = TypeOrmModule.forRootAsync({
});
@Module({
imports: [ConfigModule.forRoot(), moduleTypeOrm, UserModule, AuthModule],
imports: [
ConfigModule.forRoot(),
moduleTypeOrm,
UserModule,
AuthModule,
LoggerModule,
],
controllers: [AppController],
providers: [AppService],
})

View File

@ -0,0 +1,24 @@
import { ConsoleLogger, Injectable } from '@nestjs/common';
@Injectable()
export class DvbookingLoggerService extends ConsoleLogger {
log(message: string, context?: string) {
super.log(message, context);
}
error(message: string, trace?: string, context?: string) {
super.error(message, trace, context);
}
warn(message: string, context?: string) {
super.warn(message, context);
}
debug(message: string, context?: string) {
super.debug(message, context);
}
verbose(message: string, context?: string) {
super.verbose(message, context);
}
}

View File

@ -0,0 +1,9 @@
import { Global, Module } from '@nestjs/common';
import { DvbookingLoggerService } from './dvbooking-logger.service';
@Global()
@Module({
providers: [DvbookingLoggerService],
exports: [DvbookingLoggerService],
})
export class LoggerModule {}

View File

@ -1,8 +1,10 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DvbookingLoggerService } from './logger/dvbooking-logger.service';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useLogger(app.get(DvbookingLoggerService));
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();

View File

@ -4,19 +4,23 @@ import { Repository } from 'typeorm';
import { User } from '../entity/user';
import * as bcrypt from 'bcrypt';
import { FindOptionsRelations } from 'typeorm/find-options/FindOptionsRelations';
import { DvbookingLoggerService } from '../logger/dvbooking-logger.service';
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
private readonly logger: DvbookingLoggerService,
) {}
findAll(): Promise<User[]> {
this.logger.log('Finding all users', 'UserService');
return this.usersRepository.find();
}
findOne(id: number): Promise<User | null> {
this.logger.log(`Finding user with id: ${id}`, 'UserService');
return this.usersRepository.findOneBy({ id });
}
@ -24,10 +28,12 @@ export class UserService {
username: string,
relations: FindOptionsRelations<User>,
): Promise<User | null> {
this.logger.log(`Finding user with username: ${username}`, 'UserService');
return this.usersRepository.findOne({ where: { username }, relations });
}
async create(user: Partial<User>): Promise<User> {
this.logger.log('Creating a new user', 'UserService');
if (user.password) {
user.password = await bcrypt.hash(user.password, 12);
}
@ -36,6 +42,7 @@ export class UserService {
}
async update(id: number, user: Partial<User>): Promise<User | null> {
this.logger.log(`Updating user with id: ${id}`, 'UserService');
if (user.password) {
user.password = await bcrypt.hash(user.password, 12);
}
@ -44,6 +51,7 @@ export class UserService {
}
async remove(id: number): Promise<void> {
this.logger.log(`Removing user with id: ${id}`, 'UserService');
await this.usersRepository.delete(id);
}
}