Giter Club home page Giter Club logo

Comments (12)

gremo avatar gremo commented on May 23, 2024

Hi @rubiin , please post the full configuration that you are using. Also, try first to update the winston package. It has been updated recently, maybe it's their fault.

from nest-winston.

rubiin avatar rubiin commented on May 23, 2024

iam importing like this in app.module

	WinstonModule.forRoot({
			transports: [new winston.transports.Console({
				level: 'info',
				handleExceptions: false,
				format: winston.format.combine(
					winston.format.timestamp(),
					utilities.format.nestLike(),
				),

and in a controller of my usermodule , i am injecting as

@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger

and using the logger as this.logger.log('')

from nest-winston.

rubiin avatar rubiin commented on May 23, 2024

@gremo its latest

from nest-winston.

gremo avatar gremo commented on May 23, 2024

Sorry forgot to reply. Honestly, I don't know what's is going on here. Are you able to create a repository (minimal example) for this issue?

from nest-winston.

marcelom56 avatar marcelom56 commented on May 23, 2024

@gremo and @rubiin, I had the same problem than you. I solved passing the context (in the first argument) to log function.

this.logger.log('Hello!!');  // exception  Cannot create property 'Symbol(level)' on string 'Hello!!'
this.logger.log('info', 'Hello!!');  // it works!

from nest-winston.

gremo avatar gremo commented on May 23, 2024

@marcelom56 @rubiin if you are using WINSTON_MODULE_PROVIDER injection token, like this example:

import { Controller, Inject } from '@nestjs/common';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';

@Controller('cats')
export class CatsController {
  constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) { }
}

... it means you are using the winston package "directly". Looking at the package README, I can't see any evidence of using logger.log without specify any level, that might cause the error reported.

//
// Logging
//
logger.log({
  level: 'info',
  message: 'Hello distributed log files!'
});

logger.log('info', 'test message %s', 'my string');

TLTR: always specify a level when using logger.log as provved by @marcelom56 .

I'm going to close this issue as it seems not related to the nest-winston package. Feel free to comment further if I've misunderstood the problem.

from nest-winston.

rubiin avatar rubiin commented on May 23, 2024

sure that helps

from nest-winston.

opensas avatar opensas commented on May 23, 2024

I stumbled upon the same problem.

I found this issue at winston repo:

Leaving this here for others who come to this thread. The winstonlogger object does have a .log() method. However, unlike console.log(), it requires two arguments, not just one. If you forget the first argument, the log message category, you will get the Symbol(level) error.

source

This is how I'm using it:

import { Controller, Get, Inject, Logger } from '@nestjs/common';
import { AppService } from './app.service';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger
  ) {}

  @Get()
  getHello(): string {
    this.logger.log('hello!!!')   // TypeError: Cannot create property 'Symbol(level)' on string 'hello!!!'
    this.logger.log('info', 'hello!!!')   // ok
    return this.appService.getHello();
  }
}

It seems inconsistent with nestjs Logger (vscode tooltip displays: (method) Logger.log(message: any, context?: string): void)

Perhaps we should override winston log method to make it work like nestjs. Here is a suggested workaround. I think that logger.log(...) should in fact call winston.info(...).

It's rather frustrating to get stuck at such a trivial example, and I'm suprised there aren't more people reporting it


kept doing a couple more tests

following the steps described in Use as the main Nest logger I get the same error, and following Use as the main Nest logger (also for bootstrapping) is the only case which seems to work ok.

from nest-winston.

opensas avatar opensas commented on May 23, 2024

Moreover, from winston doc, every example given with logger.log receives the log level as first parameter, and in the using loggin levels section says:

Setting the level for your logging message can be accomplished in one of two ways. You can pass a string representing the logging level to the log() method or use the level specified methods defined on every winston Logger.

from nest-winston.

empz avatar empz commented on May 23, 2024

I'm sorry but this shouldn't be closed.

If by injecting the Winston logger by doing the following, it seems we end up with the actually Winston logger instance (and not a logger through nest logger interface).

@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger

So I think the injection should be:

@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: WinstonLogger

And the typings should be fixed.

import { Logger } from 'winston';
import { LoggerService } from '@nestjs/common';
export declare class WinstonLogger implements LoggerService {
    private readonly logger;
    private context?;
    constructor(logger: Logger);
    setContext(context: string): void;
    log(level: string, message: any, context?: string): any; // Adding level as a first argument
    error(message: any, trace?: string, context?: string): any;
    warn(message: any, context?: string): any;
    debug?(message: any, context?: string): any;
    verbose?(message: any, context?: string): any;
}

from nest-winston.

rubiin avatar rubiin commented on May 23, 2024

yes that should be done

from nest-winston.

gremo avatar gremo commented on May 23, 2024

@empz I've updated the README, please can you check it out and see if something is wrong or unclean (especially about the injection token)?

Typings are correct. If you look at the LoggerService from Nest, you can see that the log function doesn't define any level. You are probably talking about the log from winston package.

In addition, I'm working on providing samples which should help people with the token/typings. 👍

from nest-winston.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.