Skip to content

Commit 4516b0a

Browse files
committed
docs: clarify logger documentation
1 parent bdd7ebe commit 4516b0a

7 files changed

Lines changed: 115 additions & 40 deletions

File tree

README.md

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# knex-tiny-logger
22

3-
> Zero-config query logging for Knex. Tiny by default, flexible when needed.
4-
53
[![](https://img.shields.io/npm/v/knex-tiny-logger.svg?style=flat-square)](https://npmjs.com/package/knex-tiny-logger)
64

5+
> Zero-config query logging for Knex. Tiny by default, flexible when needed.
6+
77
## Install
88

99
```bash
@@ -39,9 +39,14 @@ const knex = knexTinyLogger(
3939
)
4040
```
4141

42-
That is the default path: plain output, no extra runtime dependencies.
42+
By default, `knexTinyLogger` uses `defaultLogger`: plain string logs, no extra runtime dependencies.
4343

44-
## String Logs
44+
```text
45+
SQL (3.421 ms) select 1 as id
46+
SQL ERROR (2.104 ms) select * from missing_table
47+
```
48+
49+
## Default Logger
4550

4651
```ts
4752
import knexTinyLogger, { defaultLogger } from 'knex-tiny-logger'
@@ -51,7 +56,9 @@ knexTinyLogger(knex, {
5156
})
5257
```
5358

54-
String loggers format SQL before writing it. Use `bindings` for the built-in formatter, or replace formatting completely:
59+
The default logger formats SQL before writing it. By default, it asks Knex to interpolate bindings into the logged SQL.
60+
61+
Set `bindings: false` to write the original SQL with placeholders, or replace formatting completely:
5562

5663
```ts
5764
knexTinyLogger(knex, {
@@ -73,7 +80,10 @@ knexTinyLogger(knex, {
7380

7481
## Colorful Logs
7582

76-
The colorful logger lives in a separate entrypoint.
83+
The colorful logger is the same string logger experience, with output colored by query state.
84+
It supports the same `bindings`, `formatter`, and `write` options as `defaultLogger`.
85+
86+
Successful SQL is cyan; failed SQL is red. The message shape otherwise matches `defaultLogger`.
7787

7888
```ts
7989
import knexTinyLogger from 'knex-tiny-logger'
@@ -93,13 +103,12 @@ import knexTinyLogger from 'knex-tiny-logger'
93103
import { pinoLogger } from 'knex-tiny-logger/pino'
94104

95105
knexTinyLogger(knex, {
96-
logger: pinoLogger(pino, {
97-
bindings: true,
98-
}),
106+
logger: pinoLogger(pino),
99107
})
100108
```
101109

102110
The pino adapter logs `sql`, `bindings`, and `durationMs`; errors also include `err`.
111+
Bindings are included by default. Set `bindings: false` to omit them from the structured payload.
103112

104113
## Custom Logger
105114

@@ -118,6 +127,21 @@ const logger: Logger = {
118127
knexTinyLogger(knex, { logger })
119128
```
120129

130+
Passing a function uses the default string formatting and writes each log message to that function:
131+
132+
```ts
133+
import { defaultLogger } from 'knex-tiny-logger'
134+
135+
knexTinyLogger(knex, { logger: console.log })
136+
137+
// same as
138+
knexTinyLogger(knex, {
139+
logger: defaultLogger({ write: console.log }),
140+
})
141+
```
142+
143+
## Logger Errors
144+
121145
Logger errors are caught so logging does not break queries. By default they are reported with `console.error`.
122146

123147
```ts
@@ -129,35 +153,36 @@ knexTinyLogger(knex, {
129153
})
130154
```
131155

132-
Simple function loggers also work:
133-
134-
```ts
135-
knexTinyLogger(knex, { logger: console.log })
136-
```
137-
138156
## Tracing
139157

140-
Use the tracer directly for lower-level integrations.
158+
For lower-level integrations:
141159

142160
```ts
143161
import { createTracer } from 'knex-tiny-logger/tracer'
144162

163+
const spans = new Map()
164+
145165
const tracer = createTracer(knex, {
146166
onStart(query) {
147-
span.start(query.sql)
167+
spans.set(query.queryId, tracerProvider.startSpan('sql', {
168+
sql: query.sql,
169+
bindings: query.bindings,
170+
}))
148171
},
149172
onEnd(query) {
150-
span.end({ durationMs: query.durationMs })
173+
spans.get(query.queryId)?.end({ durationMs: query.durationMs })
174+
spans.delete(query.queryId)
151175
},
152176
onError(query) {
153-
span.fail(query.error)
177+
spans.get(query.queryId)?.fail(query.error)
178+
spans.delete(query.queryId)
154179
},
155180
})
156181

157182
tracer.dispose()
158183
```
159184

160-
The tracer is raw on purpose: lifecycle, duration, SQL, bindings, errors. Formatting belongs to loggers.
185+
The tracer exposes query lifecycle events with duration, SQL, bindings, and errors.
161186

162187
## License
163188

src/colorful-logger.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@ const COLORIZE = {
1212
/**
1313
* Create the built-in ANSI-colored string logger.
1414
*
15-
* It writes to `console.log` by default. `bindings` configures the built-in
16-
* formatter; `formatter` lets you provide custom SQL formatting.
15+
* It behaves like `defaultLogger`, but colors output by query state.
16+
*
17+
* It writes to `console.log` by default.
18+
*
19+
* By default, it asks Knex to interpolate bindings into the logged SQL.
20+
* `bindings: false` writes the original SQL with placeholders.
21+
*
22+
* `formatter` lets you replace SQL formatting completely.
1723
*
1824
* @example
1925
* ```ts
2026
* import knexTinyLogger from 'knex-tiny-logger'
2127
* import { colorfulLogger } from 'knex-tiny-logger/colorful'
2228
*
23-
* knexTinyLogger(knex, { logger: colorfulLogger() })
29+
* knexTinyLogger(knex, {
30+
* logger: colorfulLogger(),
31+
* })
2432
* ```
2533
*/
2634
export function colorfulLogger(options: ColorfulLoggerOptions = {}): Logger {

src/default-logger.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ import type { DefaultLoggerOptions, Logger, QueryFormatter } from './types.ts'
55
/**
66
* Create the built-in dependency-free string logger.
77
*
8-
* It writes to `console.log` by default. `bindings` configures the built-in
9-
* formatter; `formatter` lets you provide custom SQL formatting.
8+
* It writes to `console.log` by default.
9+
*
10+
* By default, it asks Knex to interpolate bindings into the logged SQL.
11+
* `bindings: false` writes the original SQL with placeholders.
12+
*
13+
* `formatter` lets you replace SQL formatting completely.
1014
*
1115
* @example
1216
* ```ts
1317
* import knexTinyLogger, { defaultLogger } from 'knex-tiny-logger'
1418
*
15-
* knexTinyLogger(knex, { logger: defaultLogger({ bindings: false }) })
19+
* knexTinyLogger(knex, {
20+
* logger: defaultLogger({ bindings: false }),
21+
* })
1622
* ```
1723
*/
1824
export function defaultLogger(options: DefaultLoggerOptions = {}): Logger {

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ export type {
2828
* Attach query logging to a Knex instance and return the same instance.
2929
*
3030
* With no options, queries are logged through the built-in dependency-free
31-
* logger. Logger errors are caught and reported through `onLoggerError` so
32-
* logging does not break database queries.
31+
* `defaultLogger()`.
32+
*
33+
* Logger errors are caught and reported through `onLoggerError` so logging does
34+
* not break database queries.
3335
*
3436
* @example
3537
* ```ts

src/pino-logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface PinoLoggerOptions {
3232
* import { pinoLogger } from 'knex-tiny-logger/pino'
3333
*
3434
* knexTinyLogger(knex, {
35-
* logger: pinoLogger(pino, { bindings: true }),
35+
* logger: pinoLogger(pino),
3636
* })
3737
* ```
3838
*/

src/tracer.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,32 @@ type KnexWithEvents = Knex & {
2525
/**
2626
* Attach low-level query tracing hooks to a Knex instance.
2727
*
28-
* The tracer reports raw query lifecycle data and never formats SQL. Hook
29-
* errors are caught and passed to `onTracerError`, which is a no-op by default.
28+
* The tracer exposes query lifecycle events with duration, SQL, bindings, and
29+
* errors.
30+
*
31+
* Hook errors are caught and passed to `onTracerError`, which is a no-op by
32+
* default.
3033
*
3134
* @example
3235
* ```ts
3336
* import { createTracer } from 'knex-tiny-logger/tracer'
3437
*
38+
* const spans = new Map()
39+
*
3540
* const tracer = createTracer(knex, {
41+
* onStart(event) {
42+
* spans.set(event.queryId, tracerProvider.startSpan('sql', {
43+
* sql: event.sql,
44+
* bindings: event.bindings,
45+
* }))
46+
* },
3647
* onEnd(event) {
37-
* console.log(event.sql, event.durationMs)
48+
* spans.get(event.queryId)?.end({ durationMs: event.durationMs })
49+
* spans.delete(event.queryId)
50+
* },
51+
* onError(event) {
52+
* spans.get(event.queryId)?.fail(event.error)
53+
* spans.delete(event.queryId)
3854
* },
3955
* })
4056
*

src/types.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { Knex } from 'knex'
22

3-
/** Function logger adapter, for example `console.log`. */
3+
/**
4+
* Function logger adapter, for example `console.log`.
5+
*
6+
* Receives default string log messages.
7+
*/
48
export type SimpleLogger = (message?: unknown, ...optionalParams: unknown[]) => void
59

610
/** Receives one complete message from a string logger. */
@@ -23,7 +27,11 @@ export interface QueryFormatterInput {
2327

2428
/** Options for `defaultQueryFormatter`. */
2529
export interface DefaultQueryFormatterOptions {
26-
/** Include bindings in formatted SQL when Knex can interpolate them. Defaults to `true`. */
30+
/**
31+
* Ask Knex to interpolate bindings into the logged SQL.
32+
*
33+
* Defaults to `true`.
34+
*/
2735
bindings?: boolean
2836
}
2937

@@ -110,13 +118,15 @@ interface StringLoggerBaseOptions {
110118
/**
111119
* Options shared by string loggers.
112120
*
113-
* `bindings` configures the built-in formatter. `formatter` provides custom
114-
* SQL formatting, so `bindings` only applies when `formatter` is not provided.
121+
* Bindings are interpolated into the logged SQL by default through Knex.
122+
*
123+
* `formatter` provides custom SQL formatting, so `bindings` only applies when
124+
* `formatter` is not provided.
115125
*/
116126
export type StringLoggerOptions = StringLoggerBaseOptions &
117127
(
118128
| {
119-
/** Include bindings in SQL formatted by the built-in formatter. Defaults to `true`. */
129+
/** Ask Knex to interpolate bindings into the logged SQL. Defaults to `true`. */
120130
bindings?: boolean
121131
formatter?: undefined
122132
}
@@ -129,7 +139,7 @@ export type StringLoggerOptions = StringLoggerBaseOptions &
129139

130140
/** Options accepted by `defaultLogger`. */
131141
export type DefaultLoggerOptions = StringLoggerOptions
132-
/** Options accepted by `colorfulLogger`. */
142+
/** Options accepted by `colorfulLogger`; same formatting options as `defaultLogger`. */
133143
export type ColorfulLoggerOptions = StringLoggerOptions
134144

135145
/** Options for `createTracer`. */
@@ -140,8 +150,16 @@ export interface CreateTracerOptions extends TracerHooks {
140150

141151
/** Options for `knexTinyLogger`. */
142152
export interface KnexTinyLoggerOptions {
143-
/** Logger implementation or simple log function. Defaults to `defaultLogger()`. */
153+
/**
154+
* Logger implementation or simple log function.
155+
*
156+
* Defaults to String Logs through `defaultLogger()`.
157+
*/
144158
logger?: Logger | SimpleLogger
145-
/** Called when a logger hook, writer, or formatter throws. Defaults to `console.error`. */
159+
/**
160+
* Called when a logger hook, writer, or formatter throws.
161+
*
162+
* Defaults to `console.error`.
163+
*/
146164
onLoggerError?: (event: LoggerErrorEvent) => void
147165
}

0 commit comments

Comments
 (0)