Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing winston logger instance to forRoot doesn't work #614

Closed
Hareloo opened this issue Jan 22, 2023 · 2 comments
Closed

Passing winston logger instance to forRoot doesn't work #614

Hareloo opened this issue Jan 22, 2023 · 2 comments
Labels

Comments

@Hareloo
Copy link

Hareloo commented Jan 22, 2023

I have the following logger.ts file:

import winston from 'winston';

// Define the levels that can be used for logging
const levels = {
    error: 0,
    warn: 1,
    info: 2,
    http: 3,
    debug: 4,
};
  
// // In debug mode show all logs, otherwise show only warn and error
// const level = () => {
//     const env = process.env.NODE_ENV || 'development'
//     const isDevelopment = env === 'development'
//     return 'all'
//   }

// Define the color each level will be logged with
const colors = {
    error: 'red',
    warn: 'yellow',
    info: 'green',
    http: 'magenta',
    debug: 'white',
};

// Tell winston to use the colors we defined
winston.addColors(colors);

// Define the format in which we want to log
const format = winston.format.combine(
    winston.format.timestamp(),
    winston.format.colorize(),
    winston.format.json()
);

// Tell winston to log to console and to a file
const transports = [
    new winston.transports.Console(),
    new winston.transports.File({
        filename: './logs/auth.log',
        handleExceptions: true,
        maxsize: 5242880,
        maxFiles: 5,
        format: format
    })
];

const logger = winston.createLogger({
    level: 'debug',
    levels,
    format,
    transports
});

export default logger;

I then passed it to the forRoot method like so:

import logger from './lib/logger.js';
import { Module } from '@nestjs/common';
import { WinstonModule } from 'nest-winston';
import { AppController } from './app.controller.js';
import { AppService } from './app.service.js';
import { RabbitMqModule } from './rabbit-mq/rabbit-mq.module.js';

@Module({
  imports: [RabbitMqModule,
    WinstonModule.forRoot({
      instance: logger
    })],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }

It doesn't work, the transports don't get registered...
I think the problem is within the method createWinstonProviders, it always uses the factory of () => createLogger(...) instead of returning the instance if it was defined...

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the slate label Jun 27, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jul 5, 2023

This issue was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant