-
Notifications
You must be signed in to change notification settings - Fork 268
Improve logging foundation #1907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3680f26
Improve logging foundation
lukasIO a77a1ad
lint
lukasIO 112c2cf
chore: replace explicit logContext passing with bound version, add in…
lukasIO ab71528
remove display keys
lukasIO 4b96b06
update logger tests
lukasIO 823d108
prettier
lukasIO a771797
Create tough-stingrays-scream.md
lukasIO 2e69a05
update logging.md
lukasIO c37147f
Merge branch 'lukas/logging-improvements' of github.com:livekit/clien…
lukasIO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "livekit-client": patch | ||
| --- | ||
|
|
||
| chore: improve logging foundation for implicit context retrieval |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Logging conventions | ||
|
|
||
| This SDK uses [loglevel](https://github.com/pimterry/loglevel) via a thin | ||
| wrapper in [`src/logger.ts`](src/logger.ts). Each subsystem gets its own | ||
| named logger (see `LoggerNames`) so users can raise verbosity per area via | ||
| `setLogLevel(level, loggerName)`. | ||
|
|
||
| ## Level rubric | ||
|
|
||
| - **error** — failure surfaced to the user / unrecoverable. Gave up | ||
| reconnecting, publish rejected by server, decode permanently failed. | ||
| - **warn** — recoverable anomaly or automatic retry. ICE restart, signal | ||
| reconnect starting, token refresh retryable failure, unexpected-but- | ||
| handled server message. | ||
| - **info** — exactly one log per meaningful lifecycle transition: | ||
| `connecting` / `connected` / `reconnecting (attempt N)` / `reconnected` / | ||
| `disconnected (reason)`, track `published` / `unpublished` / | ||
| `subscribed` / `unsubscribed`, permission changes, region switched, | ||
| e2ee enabled/disabled, signal (re)connected, major engine state | ||
| transitions. Nothing that can fire more than about once per second | ||
| under normal use. | ||
| - **debug** — everything else: individual signal messages, per-ICE- | ||
| candidate, SDP, DTX/simulcast/codec negotiation, data channel | ||
| lifecycle, reconnection internal states, timing details. | ||
| - **trace** — reserved for deliberate deep dives; unused by default. | ||
|
|
||
| ## Structured context | ||
|
|
||
| Each class passes a structured `logContext` object to every log call so | ||
| consumers wired up via `setLogExtension` receive full metadata for | ||
| ingestion. | ||
|
|
||
| ### Binding context to a logger | ||
|
|
||
| Prefer creating the named logger with a context provider once, then | ||
| passing only call-site-specific extras: | ||
|
|
||
| ```ts | ||
| // in a class constructor | ||
| this.log = getLogger(LoggerNames.Engine, () => this.logContext); | ||
|
|
||
| // at call sites | ||
| this.log.debug('got ICE candidate from peer', { candidate, target }); | ||
| // devtools: got ICE candidate from peer, { room: 'foo', participant: 'alice', ..., candidate, target } | ||
| // setLogExtension: { room: 'foo', participant: 'alice', ..., candidate, target } | ||
| ``` | ||
|
|
||
| The context provider is invoked on every call, so updates to `logContext` | ||
| are reflected automatically. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import * as loglevel from 'loglevel'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { | ||
| type LogExtension, | ||
| LogLevel, | ||
| LoggerNames, | ||
| type StructuredLogger, | ||
| getLogger, | ||
| setLogExtension, | ||
| setLogLevel, | ||
| } from './logger'; | ||
|
|
||
| describe('getLogger with context provider', () => { | ||
| afterEach(() => { | ||
| setLogLevel(LogLevel.info); | ||
| }); | ||
|
|
||
| const hookBase = (name: LoggerNames, extension: LogExtension) => { | ||
| const base = loglevel.getLogger(name) as StructuredLogger; | ||
| setLogExtension(extension, base); | ||
| }; | ||
|
|
||
| it('omits the prefix when the bound context has no display keys', () => { | ||
| const extension = vi.fn<LogExtension>(); | ||
| hookBase(LoggerNames.Room, extension); | ||
| setLogLevel(LogLevel.info, LoggerNames.Room); | ||
|
|
||
| const log = getLogger(LoggerNames.Room, () => ({ irrelevant: 'x' })); | ||
| log.info('plain'); | ||
|
|
||
| expect(extension).toHaveBeenCalledWith(LogLevel.info, 'plain', { irrelevant: 'x' }); | ||
| }); | ||
|
|
||
| it('reflects dynamic changes to the bound context on every call', () => { | ||
| const extension = vi.fn<LogExtension>(); | ||
| hookBase(LoggerNames.Engine, extension); | ||
| setLogLevel(LogLevel.info, LoggerNames.Engine); | ||
|
|
||
| let current: Record<string, string> = { room: 'r1' }; | ||
| const log = getLogger(LoggerNames.Engine, () => current); | ||
|
|
||
| log.info('first'); | ||
| current = { room: 'r2', participant: 'bob' }; | ||
| log.info('second'); | ||
|
|
||
| const infos = extension.mock.calls.filter((c) => c[0] === LogLevel.info); | ||
| expect(infos[0][2]).toEqual({ room: 'r1' }); | ||
| expect(infos[1][2]).toEqual({ room: 'r2', participant: 'bob' }); | ||
| }); | ||
|
|
||
| it('returns an unwrapped logger when no context provider is supplied', () => { | ||
| const extension = vi.fn<LogExtension>(); | ||
| hookBase(LoggerNames.Signal, extension); | ||
| setLogLevel(LogLevel.info, LoggerNames.Signal); | ||
|
|
||
| const log = getLogger(LoggerNames.Signal); | ||
| log.info('raw'); | ||
|
|
||
| expect(extension).toHaveBeenCalledWith(LogLevel.info, 'raw', undefined); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: It might be worth mentioning here what you told me in our 1:1 that
loglevel's trace logs include stack traces, and that's the main reason this level isn't being used more extensively.