# slonik-interceptor-query-logging

> Logs Slonik queries.

Latest version **49.13.0** (published 2026-11-06) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install slonik-interceptor-query-logging
pnpm add slonik-interceptor-query-logging
yarn add slonik-interceptor-query-logging
bun add slonik-interceptor-query-logging
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 49.13.0 |
| Published | 2026-11-06 |
| First published | 2019-02-24 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=24 |
| Dependencies | 3 |
| Unpacked size | 33.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 4939 |
| Author | Gajus Kuizinas |
| Maintainers | gajus |
| Keywords | interceptor, logging, postgresql |

## Links

- npm: https://www.npmjs.com/package/slonik-interceptor-query-logging
- Repository: https://github.com/gajus/slonik
- Homepage: https://github.com/gajus/slonik#readme
- Issues: https://github.com/gajus/slonik/issues
- npm.io page: https://npm.io/package/slonik-interceptor-query-logging

## Dependencies (3)

- [pretty-ms](https://npm.io/package/pretty-ms.md) ^9.3.0
- [crack-json](https://npm.io/package/crack-json.md) ^1.3.0
- [serialize-error](https://npm.io/package/serialize-error.md) ^12.0.0

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 49.13.0 (latest) — 2026-11-06
- 49.12.0 — 2026-10-24
- 49.11.0 — 2026-10-11
- 49.10.10 — 2026-09-21
- 49.10.9 — 2026-07-26
- 49.10.8 — 2026-07-26
- 49.10.7 — 2026-06-16
- 49.10.6 — 2026-06-16
- 49.10.5 — 2026-06-16
- 49.10.4 — 2026-06-16
- 49.10.3 — 2026-06-16
- 49.10.1 — 2026-06-10
- 49.10.0 — 2026-06-05
- 49.9.0 — 2026-06-03
- 49.8.0 — 2026-05-29
- … 121 more at https://npm.io/package/slonik-interceptor-query-logging/versions

## README

# slonik-interceptor-query-logging

[![NPM version](http://img.shields.io/npm/v/slonik-interceptor-query-logging.svg?style=flat-square)](https://www.npmjs.org/package/slonik-interceptor-query-logging)
[![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
[![Twitter Follow](https://img.shields.io/twitter/follow/kuizinas.svg?style=social&label=Follow)](https://twitter.com/kuizinas)

Logs [Slonik](https://github.com/gajus/slonik) queries.

## API

```ts
import { createQueryLoggingInterceptor } from "slonik-interceptor-query-logging";
```

```ts
/**
 * @property logValues Dictates whether to include parameter values used to execute the query. (default: true)
 */
type UserConfigurationType = {|
  +logValues: boolean
|};

(userConfiguration: UserConfigurationType) => InterceptorType;
```

## Example usage

Note: You must export `ROARR_LOG=true` environment variable for logs to be printed. Refer to [Roarr](https://github.com/gajus/roarr) documentation for more information.

```ts
import { createPool } from "slonik";
import { createQueryLoggingInterceptor } from "slonik-interceptor-query-logging";

const interceptors = [createQueryLoggingInterceptor()];

const pool = createPool("postgres://", {
  interceptors,
});

await connection.any(sql`
  SELECT
    id,
    code_alpha_2
  FROM country
`);
```

Produces log:

```json
{"context":{"package":"slonik","poolId":"01D4G1EHYPYX1DZ073G08QEPED","logLevel":30,"processId":769420982,"stats":{"idleConnectionCount":0,"totalConnectionCount":1,"waitingRequestCount":0}},"message":"created a new client connection","sequence":0,"time":1551021590799,"version":"1.0.0"}
{"context":{"package":"slonik","poolId":"01D4G1EHYPYX1DZ073G08QEPED","logLevel":30,"processId":769420982,"stats":{"idleConnectionCount":0,"totalConnectionCount":1,"waitingRequestCount":0}},"message":"client is checked out from the pool","sequence":1,"time":1551021590801,"version":"1.0.0"}
{"context":{"package":"slonik","poolId":"01D4G1EHYPYX1DZ073G08QEPED","connectionId":"01D4G1EJ8DZFX79EN8CRE5Z7VS","queryId":"01D4G1EJB9P3SJ3V0SSVRTZ800","logLevel":20,"sql":"SELECT id, code_alpha_2 FROM country","stackTrace":[],"values":[]},"message":"executing query","sequence":2,"time":1551021590890,"version":"1.0.0"}
{"context":{"package":"slonik","poolId":"01D4G1EHYPYX1DZ073G08QEPED","connectionId":"01D4G1EJ8DZFX79EN8CRE5Z7VS","queryId":"01D4G1EJB9P3SJ3V0SSVRTZ800","logLevel":20,"executionTime":"65ms","rowCount":250},"message":"query execution result","sequence":3,"time":1551021590918,"version":"1.0.0"}
{"context":{"package":"slonik","poolId":"01D4G1EHYPYX1DZ073G08QEPED","logLevel":30,"processId":769420982,"stats":{"idleConnectionCount":0,"totalConnectionCount":0,"waitingRequestCount":0}},"message":"client connection is closed and removed from the client pool","sequence":4,"time":1551021600922,"version":"1.0.0"}
```

Use [`roarr-cli`](https://github.com/gajus/roarr-cli) to pretty print the logs.

Using `roarr pretty-print` will produce logs in the following format:

```yaml
[2019-02-24T15:19:50.799Z] INFO (30) (@slonik): created a new client connection
poolId:    01D4G1EHYPYX1DZ073G08QEPED
processId: 769420982
stats:
  idleConnectionCount:  0
  totalConnectionCount: 1
  waitingRequestCount:  0

[2019-02-24T15:19:50.801Z] INFO (30) (@slonik): client is checked out from the pool
poolId:    01D4G1EHYPYX1DZ073G08QEPED
processId: 769420982
stats:
  idleConnectionCount:  0
  totalConnectionCount: 1
  waitingRequestCount:  0

[2019-02-24T15:19:50.890Z] DEBUG (20) (@slonik): executing query
poolId:       01D4G1EHYPYX1DZ073G08QEPED
connectionId: 01D4G1EJ8DZFX79EN8CRE5Z7VS
queryId:      01D4G1EJB9P3SJ3V0SSVRTZ800
sql:          SELECT id, code_alpha_2 FROM country
stackTrace:
  (empty array)
values:
  (empty array)

[2019-02-24T15:19:50.918Z] DEBUG (20) (@slonik): query execution result
poolId:        01D4G1EHYPYX1DZ073G08QEPED
connectionId:  01D4G1EJ8DZFX79EN8CRE5Z7VS
queryId:       01D4G1EJB9P3SJ3V0SSVRTZ800
executionTime: 65ms
rowCount:      250

[2019-02-24T15:20:00.922Z] INFO (30) (@slonik): client connection is closed and removed from the client pool
poolId:    01D4G1EHYPYX1DZ073G08QEPED
processId: 769420982
stats:
  idleConnectionCount:  0
  totalConnectionCount: 0
  waitingRequestCount:  0
```

## Logging `auto_explain`

`executionTime` log property describes how long it took for the client to execute the query, i.e. it includes the overhead of waiting for a connection and the network latency, among other things. However, it is possible to get the real query execution time by using [`auto_explain` module](https://www.postgresql.org/docs/current/auto-explain.html).

There are several pre-requisites:

```sql
-- Load the extension.
LOAD 'auto_explain';
-- or (if you are using AWS RDS):
LOAD '$libdir/plugins/auto_explain';

-- Enable logging of all queries.
SET auto_explain.log_analyze=true;
SET auto_explain.log_format=json;
SET auto_explain.log_min_duration=0;
SET auto_explain.log_timing=true;

-- Enables Slonik to capture auto_explain logs.
SET client_min_messages=log;
```

This can be configured using `afterPoolConnection` interceptor, e.g.

```ts
const pool = createPool("postgres://localhost", {
  interceptors: [
    {
      afterPoolConnection: async (connection) => {
        await connection.query(sql`LOAD 'auto_explain'`);
        await connection.query(sql`SET auto_explain.log_analyze=true`);
        await connection.query(sql`SET auto_explain.log_format=json`);
        await connection.query(sql`SET auto_explain.log_min_duration=0`);
        await connection.query(sql`SET auto_explain.log_timing=true`);
        await connection.query(sql`SET client_min_messages=log`);
      },
    },
  ],
});
```

Slonik logging interceptor recognises and parses the `auto_explain` JSON message e.g.

```yaml
[2018-12-31T21:15:21.010Z] INFO (30) (@slonik): notice message
notice:
  level:   notice
  message:
    Query Text: SELECT count(*) FROM actor
    Plan:
      Node Type:           Aggregate
      Strategy:            Plain
      Partial Mode:        Simple
      Parallel Aware:      false
      Startup Cost:        4051.33
      Total Cost:          4051.34
      Plan Rows:           1
      Plan Width:          8
      Actual Startup Time: 26.791
      Actual Total Time:   26.791
      Actual Rows:         1
      Actual Loops:        1
      Plans:
        -
          Node Type:           Seq Scan
          Parent Relationship: Outer
          Parallel Aware:      false
          Relation Name:       actor
          Alias:               actor
          Startup Cost:        0
          Total Cost:          3561.86
          Plan Rows:           195786
          Plan Width:          0
          Actual Startup Time: 0.132
          Actual Total Time:   15.29
          Actual Rows:         195786
          Actual Loops:        1
```

---
_Source: https://npm.io/package/slonik-interceptor-query-logging · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
