@remix-run/logger-middleware 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # logger-middleware
2
2
 
3
- HTTP request/response logging middleware for Remix. It logs request metadata and response details with configurable output formats.
3
+ HTTP request/response logging middleware for Remix. It logs request metadata and response details with configurable output formats, and exposes the configured log function on request context.
4
4
 
5
5
  ## Features
6
6
 
7
7
  - **Request/Response Logging** - Logs method, path, status, and response metadata
8
+ - **Context Logger** - Exposes `context.logger` (or `context.get(Logger)`)
8
9
  - **Token-Based Formatting** - Customize log output with built-in placeholders
9
10
  - **Structured Timing Data** - Includes request duration and timestamps
10
11
  - **Colorized Output** - Highlights method, status, duration, and content length in TTY output
@@ -18,16 +19,23 @@ npm i remix
18
19
  ## Usage
19
20
 
20
21
  ```ts
21
- import { createRouter } from 'remix/fetch-router'
22
- import { logger } from 'remix/logger-middleware'
22
+ import { createRouter } from 'remix/router'
23
+ import { logger } from 'remix/middleware/logger'
23
24
 
24
25
  let router = createRouter({
25
26
  middleware: [logger()],
26
27
  })
27
28
 
29
+ router.get('/users/:id', (context) => {
30
+ context.logger(`Loading user ${context.params.id}`)
31
+ return Response.json(loadUser(context.params.id))
32
+ })
33
+
28
34
  // Logs: [19/Nov/2025:14:32:10 -0800] GET /users/123 200 1234
29
35
  ```
30
36
 
37
+ Use `context.logger(message)` (or `context.get(Logger)(message)`) for app logs that should use the configured logger.
38
+
31
39
  ### Custom Format
32
40
 
33
41
  You can use the `format` option to customize the log format. The following tokens are available:
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { type LoggerOptions, logger } from './lib/logger.ts';
1
+ export { Logger, type LoggerFunction, type LoggerOptions, logger } from './lib/logger.ts';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA"}
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { logger } from "./lib/logger.js";
1
+ export { Logger, logger } from "./lib/logger.js";
@@ -1,4 +1,18 @@
1
- import type { Middleware } from '@remix-run/fetch-router';
1
+ import { type Middleware } from '@remix-run/fetch-router';
2
+ /**
3
+ * Function used to write log messages.
4
+ */
5
+ export interface LoggerFunction {
6
+ /**
7
+ * Writes a log message.
8
+ */
9
+ (message: string): void;
10
+ }
11
+ /**
12
+ * Context key used to read the current request logger with `context.get(Logger)`.
13
+ * The `logger()` middleware also installs the logger as `context.logger`.
14
+ */
15
+ export declare const Logger: import("../../../fetch-router/src/lib/request-context.ts").ContextKey<LoggerFunction>;
2
16
  /**
3
17
  * Options for the {@link logger} middleware.
4
18
  */
@@ -35,7 +49,7 @@ export interface LoggerOptions {
35
49
  *
36
50
  * @default console.log
37
51
  */
38
- log?: (message: string) => void;
52
+ log?: LoggerFunction;
39
53
  /**
40
54
  * Enables ANSI colors for high-signal log tokens.
41
55
  *
@@ -60,5 +74,9 @@ export interface LoggerOptions {
60
74
  * @param options Options for the logger
61
75
  * @returns The logger middleware
62
76
  */
63
- export declare function logger(options?: LoggerOptions): Middleware;
77
+ export declare function logger(options?: LoggerOptions): Middleware<{
78
+ key: typeof Logger;
79
+ value: LoggerFunction;
80
+ property: 'logger';
81
+ }>;
64
82
  //# sourceMappingURL=logger.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/lib/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAIzD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;OAIG;IACH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;IAC/B;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,OAAO,GAAE,aAAkB,GAAG,UAAU,CA4C9D"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/lib/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAI3E;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;CACxB;AAED;;;GAGG;AACH,eAAO,MAAM,MAAM,uFAAqC,CAAA;AAExD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;OAIG;IACH,GAAG,CAAC,EAAE,cAAc,CAAA;IACpB;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,OAAO,GAAE,aAAkB,GAC1B,UAAU,CAAC;IAAE,GAAG,EAAE,OAAO,MAAM,CAAC;IAAC,KAAK,EAAE,cAAc,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,CA+C/E"}
@@ -1,4 +1,10 @@
1
+ import { createContextKey } from '@remix-run/fetch-router';
1
2
  import { createStyles } from '@remix-run/terminal';
3
+ /**
4
+ * Context key used to read the current request logger with `context.get(Logger)`.
5
+ * The `logger()` middleware also installs the logger as `context.logger`.
6
+ */
7
+ export const Logger = createContextKey();
2
8
  /**
3
9
  * Creates a middleware handler that logs various request/response info.
4
10
  *
@@ -8,7 +14,9 @@ import { createStyles } from '@remix-run/terminal';
8
14
  export function logger(options = {}) {
9
15
  let { colors, format = '[%date] %method %path %status %contentLength', log = console.log, } = options;
10
16
  let colorizer = getColorizer(colors);
11
- return async ({ request, url }, next) => {
17
+ return async (context, next) => {
18
+ context.set(Logger, log, { property: 'logger' });
19
+ let { request, url } = context;
12
20
  let start = new Date();
13
21
  let response = await next();
14
22
  let end = new Date();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remix-run/logger-middleware",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Middleware for logging HTTP requests and responses",
5
5
  "author": "Michael Jackson <mjijackson@gmail.com>",
6
6
  "license": "MIT",
@@ -29,11 +29,11 @@
29
29
  "@types/node": "^24.6.0",
30
30
  "@typescript/native-preview": "7.0.0-dev.20251125.1",
31
31
  "@remix-run/assert": "0.2.0",
32
- "@remix-run/fetch-router": "0.18.2",
33
- "@remix-run/test": "0.3.0"
32
+ "@remix-run/fetch-router": "0.19.0",
33
+ "@remix-run/test": "0.4.0"
34
34
  },
35
35
  "dependencies": {
36
- "@remix-run/fetch-router": "^0.18.2",
36
+ "@remix-run/fetch-router": "^0.19.0",
37
37
  "@remix-run/terminal": "^0.1.0"
38
38
  },
39
39
  "keywords": [
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export { type LoggerOptions, logger } from './lib/logger.ts'
1
+ export { Logger, type LoggerFunction, type LoggerOptions, logger } from './lib/logger.ts'
package/src/lib/logger.ts CHANGED
@@ -1,7 +1,23 @@
1
- import type { Middleware } from '@remix-run/fetch-router'
1
+ import { createContextKey, type Middleware } from '@remix-run/fetch-router'
2
2
 
3
3
  import { createStyles } from '@remix-run/terminal'
4
4
 
5
+ /**
6
+ * Function used to write log messages.
7
+ */
8
+ export interface LoggerFunction {
9
+ /**
10
+ * Writes a log message.
11
+ */
12
+ (message: string): void
13
+ }
14
+
15
+ /**
16
+ * Context key used to read the current request logger with `context.get(Logger)`.
17
+ * The `logger()` middleware also installs the logger as `context.logger`.
18
+ */
19
+ export const Logger = createContextKey<LoggerFunction>()
20
+
5
21
  /**
6
22
  * Options for the {@link logger} middleware.
7
23
  */
@@ -38,7 +54,7 @@ export interface LoggerOptions {
38
54
  *
39
55
  * @default console.log
40
56
  */
41
- log?: (message: string) => void
57
+ log?: LoggerFunction
42
58
  /**
43
59
  * Enables ANSI colors for high-signal log tokens.
44
60
  *
@@ -64,7 +80,9 @@ export interface LoggerOptions {
64
80
  * @param options Options for the logger
65
81
  * @returns The logger middleware
66
82
  */
67
- export function logger(options: LoggerOptions = {}): Middleware {
83
+ export function logger(
84
+ options: LoggerOptions = {},
85
+ ): Middleware<{ key: typeof Logger; value: LoggerFunction; property: 'logger' }> {
68
86
  let {
69
87
  colors,
70
88
  format = '[%date] %method %path %status %contentLength',
@@ -72,7 +90,10 @@ export function logger(options: LoggerOptions = {}): Middleware {
72
90
  } = options
73
91
  let colorizer = getColorizer(colors)
74
92
 
75
- return async ({ request, url }, next) => {
93
+ return async (context, next) => {
94
+ context.set(Logger, log, { property: 'logger' })
95
+
96
+ let { request, url } = context
76
97
  let start = new Date()
77
98
  let response = await next()
78
99
  let end = new Date()