Electrolite
Electrolite

Reputation: 179

NestJS' Logger.error() won't output anything when Nest is started from a Docker container

I did an error filter for my Nest application that outputs my error object with logger.error(). The issue is that logger.error() is not working. I tried to call Logger from multiple locations, nothing worked. The strange part is that other options like verbose and warn work just fine.

Take this code for example:

    import { Logger } from '@nestjs/common'
    export class InstiController{
        constructor(private instiService: InstiService) {}
    
        private logger = new Logger('InstiController')
    
        @Get()
        foo(@GetLoggedUser() user: Usuarios) {
            this.logger.error('This is an error')
            this.logger.warn('This is a warning')
            this.logger.verbose('This is a verbose')
            return this.instiService.listUserInsti(user)
        }
    }

Output:

enter image description here

UPDATE: When I start Nest directly (outside a Docker container) error outputs correctly: enter image description here

Upvotes: 1

Views: 4073

Answers (3)

Electrolite
Electrolite

Reputation: 179

@Mic Fung solutions should work in theory however it still didn't work for me. I noticed my other computer (using Docker Desktop 3.3.2 and docker engine 20.10.5 was displaying error logs just fine).

So, I assumed the issue was specific to 3.3.3 and updated Docker Desktop to 3.4.0. Now my issue is fixed. Error messages display again and it didn't require any new configuration

Upvotes: 0

Mic Fung
Mic Fung

Reputation: 5692

The default docker GUI logging level is info so the error log is not showing.

To override the logging level, go to

  1. setting
  2. Docker Engine
  3. add "logging": "fatal"
  4. Apply & Restart
  5. Remove the affected container
  6. Rebuild it via Dockerfile or docker-compose

The error log should be able to show.

P.S. The logging level in docker GUI is ("debug"|"info"|"warn"|"error"|"fatal") (default "info")

enter image description here

Image from Docker GUI after setting logging level: https://i.sstatic.net/v64OS.png


Way 02:

add log level setting though command in docker-compose.yml.

Then, remove the container and rebuild it with docker-compose up -d

...
services:
      xxx:
        build:
            context: .
            target: development
            dockerfile: ./Dockerfile
        command:
            - "--log.level=FATAL"
....

Upvotes: 2

user10057478
user10057478

Reputation:

You can try setting it manually while bootstrapping.

const app = await NestFactory.create(AppModule, {
  logger: ['error', 'warn'],
});

Values in the array can be any combination of 'log', 'error', 'warn', 'debug', and 'verbose'.

Upvotes: 0

Related Questions