tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.54k stars 216 forks source link

Log level option is ignored #483

Closed rlnt closed 2 years ago

rlnt commented 2 years ago

Actual behaviour: When I set the log level to warn or to error, it will still log info messages.

Expected behaviour: It should only log the messages on the defined log level.

Error log: /

Server configuration

Further information When I use these option to set up my client:

client = new Client({
    connection: { reconnect: config.general.auto_reconnect },
    channels: config.general.channels,
    identity: {
        username: config.login.username,
        password: config.login.token
    },
    options: {
        debug: false,
        messagesLogLevel: 'warn'
    },
    logger: {
        info: (msg: string): void => logger.info(msg),
        warn: (msg: string): void => logger.warn(msg),
        error: (msg: string): void => logger.error(msg)
    }
});

I have this output (the success messages are done manually, the rest is from the logger): As you can see, it still logs info messages even though the log level is set to warn.

For now, as a workaround, I just ignore the info logger function so it won't do anything on info log messages, like this:

client = new Client({
    connection: { reconnect: config.general.auto_reconnect },
    channels: config.general.channels,
    identity: {
        username: config.login.username,
        password: config.login.token
    },
    options: {
        debug: false,
        messagesLogLevel: 'error'
    },
    logger: {
        info: (): void => {
            /* ignore */
        },
        warn: (msg: string): void => logger.warn(msg),
        error: (msg: string): void => logger.error(msg)
    }
});
char-khan commented 2 years ago

I think it is expected behaviour.

According to guide, messagesLogLevel is

Sets the log level of chat messages. Useful for custom logging of chat messages while keeping more general events.

It only affects chat messages.

rlnt commented 2 years ago

Weird option but alright, thank you. How can I set the actual log level then?

char-khan commented 2 years ago

As far as I know, you can't set log level except debug option (that sets 'info' level). So you need to implement your own logger as you have shown.

AlcaDesign commented 2 years ago

You set the level on the logger object.

client.log.setLevel('warn');
rlnt commented 2 years ago

The client has no log field though or they are missing on the type declarations.

rlnt commented 2 years ago

The client object looks like this, there is no setLevel() function:

{
  opts: {
    connection: { reconnect: true },
    channels: [ '#########' ],
    identity: {
      username: '########',
      password: '###############'
    },
    logger: {
      info: [Function: info],
      warn: [Function: warn],
      error: [Function: error]
    },
    options: {}
  },
  clientId: null,
  _globalDefaultChannel: '#tmijs',
  _skipMembership: false,
  _skipUpdatingEmotesets: false,
  _updateEmotesetsTimer: null,
  _updateEmotesetsTimerDelay: 60000,
  maxReconnectAttempts: Infinity,
  maxReconnectInterval: 30000,
  reconnect: true,
  reconnectDecay: 1.5,
  reconnectInterval: 1000,
  reconnecting: false,
  reconnections: 0,
  reconnectTimer: 1000,
  secure: true,
  emotes: '',
  emotesets: {},
  channels: [],
  currentLatency: 0,
  globaluserstate: {},
  lastJoined: '',
  latency: 2021-09-10T15:41:34.878Z,
  moderators: {},
  pingLoop: null,
  pingTimeout: null,
  reason: '',
  username: '',
  userstate: {},
  wasCloseCalled: false,
  ws: null,
  log: {
    info: [Function: info],
    warn: [Function: warn],
    error: [Function: error]
  },
  _events: {
    reconnect: [AsyncFunction (anonymous)],
    message: [AsyncFunction (anonymous)],
    connected: [Function: a] { listener: [AsyncFunction (anonymous)] }
  },
  _maxListeners: 0
}
AlcaDesign commented 2 years ago

It's not in the types because I didn't write them and also the logger can be user defined. You could use an existing library Winston. The logger object you're passing in doesn't have a setLevel method so of course it won't appear when logging the client instance, but it does show your logger object at the log key. Again, look at the reference implementation linked above. This object is set to client.log and includes a setLevel method.