Implement basic logging

This commit is contained in:
Adam Cooper 2023-12-16 12:19:53 -05:00
parent 5ca0a076e2
commit cd7bcf77e4
7 changed files with 16 additions and 19 deletions

View file

@ -1,5 +1,4 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; // DEBUG: Is this necessary? Docs unclear. import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; // DEBUG: Is this necessary? Docs unclear.
import { DevtoolsModule } from '@nestjs/devtools-integration'; import { DevtoolsModule } from '@nestjs/devtools-integration';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
@ -7,8 +6,6 @@ import { AppService } from './app.service';
import { Cat } from './cats/cats.entity'; import { Cat } from './cats/cats.entity';
import { CatsModule } from './cats/cats.module'; import { CatsModule } from './cats/cats.module';
console.debug(`[AppModule] Starting...`)
let dbConfig: TypeOrmModuleOptions; let dbConfig: TypeOrmModuleOptions;
if ( process.env.NODE_ENV === 'production' ) { if ( process.env.NODE_ENV === 'production' ) {

View file

@ -1,8 +1,11 @@
import { Injectable } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
@Injectable() @Injectable()
export class AppService { export class AppService {
private readonly logger = new Logger(AppService.name);
getHello(): string { getHello(): string {
this.logger.log("[getHello] Starting...");
return 'Hello World!'; return 'Hello World!';
} }
} }

View file

@ -4,13 +4,10 @@ import { Cat } from "./cats.entity";
@Controller('cats') @Controller('cats')
export class CatsController { export class CatsController {
constructor(private catsService: CatsService) { constructor(private catsService: CatsService) {}
console.debug("[cats controller] Starting...");
}
@Get() @Get()
findAll(): Promise<Cat[]> { findAll(): Promise<Cat[]> {
console.debug("[ctrl findAll] Starting...");
return this.catsService.findAll(); return this.catsService.findAll();
} }

View file

@ -1,7 +1,5 @@
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm"; import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
console.debug('[cats entity] Starting...')
@Entity() @Entity()
export class Cat { export class Cat {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()

View file

@ -4,8 +4,6 @@ import { CatsController } from "./cats.controller";
import { CatsService } from "./cats.service"; import { CatsService } from "./cats.service";
import { Cat } from "./cats.entity"; import { Cat } from "./cats.entity";
console.debug("[cats module] Starting...");
@Module({ @Module({
imports: [TypeOrmModule.forFeature([Cat])], imports: [TypeOrmModule.forFeature([Cat])],
providers: [CatsService], providers: [CatsService],

View file

@ -1,4 +1,4 @@
import { Injectable } from "@nestjs/common"; import { Injectable, Logger } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm"; import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm"; import { Repository } from "typeorm";
import { Cat } from "./cats.entity"; import { Cat } from "./cats.entity";
@ -8,20 +8,22 @@ export class CatsService {
constructor( constructor(
@InjectRepository(Cat) @InjectRepository(Cat)
private catsRepository: Repository<Cat>, private catsRepository: Repository<Cat>,
) { ) {}
console.debug("[cats service] Starting...");
} private readonly logger = new Logger(CatsService.name);
findAll(): Promise<Cat[]> { findAll(): Promise<Cat[]> {
console.log("[svc findAll] Starting...") this.logger.log("[findAll] Starting...");
return this.catsRepository.find(); return this.catsRepository.find();
} }
findOne(id: number): Promise<Cat | null> { findOne(id: number): Promise<Cat | null> {
this.logger.log("[findOne] Starting...");
return this.catsRepository.findOneBy({ id }); return this.catsRepository.findOneBy({ id });
} }
async remove(id: number): Promise<void> { async remove(id: number): Promise<void> {
this.logger.log("[remove] Starting...");
await this.catsRepository.delete(id) await this.catsRepository.delete(id)
} }
} }

View file

@ -2,8 +2,10 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
console.debug("[bootstrap] Starting...") const app = await NestFactory.create(AppModule, {
const app = await NestFactory.create(AppModule, { snapshot: true }); snapshot: true,
logger: ['verbose']
});
await app.listen(3000); await app.listen(3000);
} }
bootstrap(); bootstrap();