Skip to content

Commit

Permalink
Set logExtension on all livekit loggers if not specified (#1061)
Browse files Browse the repository at this point in the history
* Set logExtension on all livekit loggers if not specified

* initialize all lk loggers

* Create afraid-mails-reflect.md
  • Loading branch information
lukasIO authored Mar 5, 2024
1 parent b2d40d5 commit fb18f6c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-mails-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Set logExtension on all livekit loggers if not specified
39 changes: 21 additions & 18 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export enum LoggerNames {

type LogLevelString = keyof typeof LogLevel;

export type StructuredLogger = {
export type StructuredLogger = log.Logger & {
trace: (msg: string, context?: object) => void;
debug: (msg: string, context?: object) => void;
info: (msg: string, context?: object) => void;
Expand All @@ -36,6 +36,7 @@ export type StructuredLogger = {
};

let livekitLogger = log.getLogger('livekit');
const livekitLoggers = Object.values(LoggerNames).map((name) => log.getLogger(name));

livekitLogger.setDefaultLevel(LogLevel.info);

Expand All @@ -54,9 +55,7 @@ export function setLogLevel(level: LogLevel | LogLevelString, loggerName?: Logge
if (loggerName) {
log.getLogger(loggerName).setLevel(level);
}
for (const logger of Object.entries(log.getLoggers())
.filter(([logrName]) => logrName.startsWith('livekit'))
.map(([, logr]) => logr)) {
for (const logger of livekitLoggers) {
logger.setLevel(level);
}
}
Expand All @@ -67,24 +66,28 @@ export type LogExtension = (level: LogLevel, msg: string, context?: object) => v
* use this to hook into the logging function to allow sending internal livekit logs to third party services
* if set, the browser logs will lose their stacktrace information (see https://github.com/pimterry/loglevel#writing-plugins)
*/
export function setLogExtension(extension: LogExtension, logger = livekitLogger) {
const originalFactory = logger.methodFactory;
export function setLogExtension(extension: LogExtension, logger?: StructuredLogger) {
const loggers = logger ? [logger] : livekitLoggers;

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

const logLevel = LogLevel[methodName as LogLevelString];
const needLog = logLevel >= configLevel && logLevel < LogLevel.silent;
logR.methodFactory = (methodName, configLevel, loggerName) => {
const rawMethod = originalFactory(methodName, configLevel, loggerName);

return (msg, context?: [msg: string, context: object]) => {
if (context) rawMethod(msg, context);
else rawMethod(msg);
if (needLog) {
extension(logLevel, msg, context);
}
const logLevel = LogLevel[methodName as LogLevelString];
const needLog = logLevel >= configLevel && logLevel < LogLevel.silent;

return (msg, context?: [msg: string, context: object]) => {
if (context) rawMethod(msg, context);
else rawMethod(msg);
if (needLog) {
extension(logLevel, msg, context);
}
};
};
};
logger.setLevel(logger.getLevel()); // Be sure to call setLevel method in order to apply plugin
logR.setLevel(logR.getLevel());
});
}

export const workerLogger = log.getLogger('lk-e2ee') as StructuredLogger;

0 comments on commit fb18f6c

Please sign in to comment.