add custom logger
This commit is contained in:
parent
7bf514b2aa
commit
5c37de40c6
@ -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],
|
||||
})
|
||||
|
||||
24
src/logger/dvbooking-logger.service.ts
Normal file
24
src/logger/dvbooking-logger.service.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
9
src/logger/logger.module.ts
Normal file
9
src/logger/logger.module.ts
Normal 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 {}
|
||||
@ -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();
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user