Skip to content

Commit 52ad731

Browse files
committed
feat: unify logs api
1 parent 7b1b309 commit 52ad731

File tree

10 files changed

+202
-137
lines changed

10 files changed

+202
-137
lines changed

docs/kit/logs.md

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,78 +29,100 @@ The Logs system allows plugins to emit structured log entries from both the serv
2929

3030
The `source` field is automatically set to `'server'` or `'client'` depending on where the log was emitted.
3131

32-
## Server-Side Usage
32+
## Usage
3333

34-
In your plugin's `devtools.setup`, use `context.logs` to emit log entries:
34+
Both server-side and client-side share the same `context.logs` API. All methods return Promises, but you don't need to `await` them for fire-and-forget usage.
35+
36+
### Fire-and-Forget
37+
38+
```ts
39+
// No await needed — just emit the log
40+
context.logs.add({
41+
message: 'Plugin initialized',
42+
level: 'info',
43+
})
44+
```
45+
46+
### With Handle
47+
48+
`await` the `add()` call to get a `DevToolsLogHandle` for subsequent updates:
49+
50+
```ts
51+
// Await to get a handle for later updates
52+
const handle = await context.logs.add({
53+
id: 'my-build',
54+
message: 'Building...',
55+
level: 'info',
56+
status: 'loading',
57+
})
58+
59+
// Later, update via the handle
60+
await handle.update({
61+
message: 'Build complete',
62+
level: 'success',
63+
status: 'idle',
64+
})
65+
66+
// Or dismiss it
67+
await handle.dismiss()
68+
```
69+
70+
### Server-Side Example
3571

3672
```ts
3773
export function myPlugin() {
3874
return {
3975
name: 'my-plugin',
4076
devtools: {
4177
setup(context) {
42-
// Simple log
78+
// Fire-and-forget
4379
context.logs.add({
4480
message: 'Plugin initialized',
4581
level: 'info',
4682
})
47-
48-
// Log with loading state, then update
49-
const entry = context.logs.add({
50-
id: 'my-build',
51-
message: 'Building...',
52-
level: 'info',
53-
status: 'loading',
54-
})
55-
56-
// Later, update via update()
57-
context.logs.update(entry.id, {
58-
message: 'Build complete',
59-
level: 'success',
60-
status: 'idle',
61-
})
6283
},
6384
},
6485
}
6586
}
6687
```
6788

68-
## Client-Side Usage
69-
70-
In dock action scripts, use `context.logs` — an async client that communicates via RPC:
89+
### Client-Side Example
7190

7291
```ts
7392
import type { DockClientScriptContext } from '@vitejs/devtools-kit/client'
7493

7594
export default async function (context: DockClientScriptContext) {
95+
// Await to get the handle
7696
const log = await context.logs.add({
7797
message: 'Running audit...',
7898
level: 'info',
7999
status: 'loading',
80100
notify: true,
81-
category: 'a11y',
82101
})
83102

84103
// ... do work ...
85104

86-
await log.update({
105+
// Update via handle — can also be fire-and-forget
106+
log.update({
87107
message: 'Audit complete — 3 issues found',
88108
level: 'warn',
89109
status: 'idle',
90110
})
91111
}
92112
```
93113

94-
## Log Handle (Client-Side)
114+
## Log Handle
95115

96-
`context.logs.add()` on the client returns a `DevToolsLogHandle` with:
116+
`context.logs.add()` returns a `Promise<DevToolsLogHandle>` with:
97117

98118
| Property/Method | Description |
99119
|-----------------|-------------|
100120
| `handle.id` | The log entry id |
101121
| `handle.entry` | The current `DevToolsLogEntry` data |
102-
| `handle.update(patch)` | Partially update the log entry |
103-
| `handle.dismiss()` | Remove the log entry |
122+
| `handle.update(patch)` | Partially update the log entry (returns `Promise`) |
123+
| `handle.dismiss()` | Remove the log entry (returns `Promise`) |
124+
125+
Both `handle.update()` and `handle.dismiss()` return Promises but can be used without `await` for fire-and-forget.
104126

105127
## Deduplication
106128

examples/plugin-a11y-checker/src/client/run-axe.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export default async function runA11yCheck(context: DockClientScriptContext): Pr
3737
const level = impactToLevel(violation.impact)
3838
const firstNode = violation.nodes[0]
3939

40-
await logs.add({
40+
// Fire-and-forget — no need to await when handle isn't needed
41+
logs.add({
4142
id: `a11y-violation-${violation.id}`,
4243
message: violation.description,
4344
level,

0 commit comments

Comments
 (0)