launchdarkly / node-server-sdk

LaunchDarkly Server-side SDK for Node
Other
79 stars 65 forks source link

Logger configuration throws error 'Received put event' #267

Closed apptrench closed 1 year ago

apptrench commented 1 year ago

Describe the bug I am configuring logger when initializing LaunchDarkly in my node.js application. This is to avoid logs sent to console.error by default. After the configuration, I am observing an extra error log printed 'Received put event'

To reproduce Configure LaunchDarkly by using the code -

    const options: ld.LDOptions = {
      logger: {
        error: (...args: never[]) => console.error(...args),
        warn: (...args: never[]) => console.warn(...args),
        info: (...args: never[]) => console.info(...args),
        debug: (...args: never[]) => console.error(...args),
      },
    };

LaunchDarkly.init('sdk-key', options);

This prints the following log messages -

Initializing stream processor to receive feature flag updates
Opened LaunchDarkly stream connection
Received put event

Expected behavior There should be two log statements -

Initializing stream processor to receive feature flag updates
Opened LaunchDarkly stream connection

Logs If applicable, add any log output related to your problem.

SDK version 6.4.3

Language version, developer tools Node.js with TypeScript

OS/platform AWS Lambda

kinyoklion commented 1 year ago

Hello @apptrench,

When using the default logger it is configured with a log level. Items below this level will not be logged. For instance the debugging level is used for debugging problems, and you generally would not want that level enabled, and it would not be enabled by default. You can either implement basic level support in your logger, or simply not log debug messages. It is also useful to tag the messages with the log level they were called with (the method name), so you can determine their origin.

Thanks, Ryan

kinyoklion commented 1 year ago

You can also use the basic logger itself, and change the destination.

ld.basicLogger({
  level: 'info' // the lowest level you want, the default basic logger uses 'info'.
  destination: <replaces console.error>
});

Some examples are here: https://github.com/launchdarkly/node-server-sdk/blob/0e810b90bbb3c759986b989df0518d7f4af10d84/index.d.ts#L1382

Thanks, Ryan

apptrench commented 1 year ago

I got your point. But I see the extra log statement is an error log which gets printed only if I do the configuration.

kinyoklion commented 1 year ago

@apptrench Your sample code used console.error for the debug level.

kinyoklion commented 1 year ago
    const options: ld.LDOptions = {
      logger: {
        error: (...args: never[]) => console.error(...args),
        warn: (...args: never[]) => console.warn(...args),
        info: (...args: never[]) => console.info(...args),
        debug: (...args: never[]) => console.error(...args), // This line here.
      },
    };

LaunchDarkly.init('sdk-key', options);
apptrench commented 1 year ago

@kinyoklion , Thanks for pointing this error. I would like to use options with 'logger' as I want to format the output. Is there way to set the log level with this option?

kinyoklion commented 1 year ago

@apptrench If you want control over the formatting, then the direction you have gone is reasonable. How the levels work will be in whatever implementation you provide though. Being as many people use logging systems that have their own configuration we don't assume very much.

The logger you pass can be any object, so your can write a class with some configuration you need. The basicLogger is in loggers.js and could be used for reference.

I cannot provide a timeline, but in a future version of the SDK the basicLogger will also take a formatter function. This will allow you to control, destination, formatting, and log level without having to write a custom implementation.

Thanks, Ryan

apptrench commented 1 year ago

For now, this works for me -

const options: ld.LDOptions = {
      logger: {
        error: (...args: never[]) => console.error(...args),
        warn: (...args: never[]) => console.warn(...args),
        info: (...args: never[]) => console.info(...args),
        debug: (...args: never[]) => {}, // This line here.
      },
    };

LaunchDarkly.init('sdk-key', options);

Thanks @kinyoklion