client-sdk-js icon indicating copy to clipboard operation
client-sdk-js copied to clipboard

Support different log levels for console vs external logging services

Open yangwooseong opened this issue 3 months ago • 2 comments

Describe the problem

Currently, setLogExtension and console output share the same log level configuration. When you set a log level using setLogLevel(), both console output and log extensions use the identical filtering criteria.

This creates a limitation where you cannot send detailed debug logs to external services while keeping console output clean (info level)

Describe the proposed solution

I'm unsure about the optimal implementation approach. One idea I have that doesn't trigger breaking change is to add a parameter (customLevel) to setLogExtension and modify the needLog conditional with an OR condition. I'd appreciate your thoughts on whether this approach would be appropriate.

// logger.ts
export function setLogExtension(extension: LogExtension, logger?: StructuredLogger, customLevel?: LogLevel) {
  const loggers = logger ? [logger] : livekitLoggers;

  loggers.forEach((logR) => {
    const originalFactory = logR.methodFactory;

    logR.methodFactory = (methodName, configLevel, loggerName) => {
      const rawMethod = originalFactory(methodName, configLevel, loggerName);

      const logLevel = LogLevel[methodName as LogLevelString];

      const needConsoleLog = logLevel >= configLevel && logLevel < LogLevel.silent;
      const needExtensionLog = customLevel !== undefined 
        ? (logLevel >= customLevel && logLevel < LogLevel.silent)  // extensionLog depending on customLevel
        : needConsoleLog;

      return (msg, context?: [msg: string, context: object]) => {
        if (needConsoleLog) {
          if (context) rawMethod(msg, context);
          else rawMethod(msg);
        }

        if (needExtensionLog) {
          extension(logLevel, msg, context);
        }
      };
    };
    logR.setLevel(logR.getLevel());
  });
}

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

yangwooseong avatar Sep 16 '25 08:09 yangwooseong

Thanks for the suggestion, I think your approach makes sense.

Would you want to open a PR for this?

lukasIO avatar Sep 22 '25 09:09 lukasIO

yes I will open a PR, probably in a couple of weeks.

yangwooseong avatar Sep 23 '25 07:09 yangwooseong