@logtape/koa 1.3.0-dev.1
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/LICENSE +20 -0
- package/README.md +104 -0
- package/deno.json +34 -0
- package/dist/_virtual/rolldown_runtime.cjs +30 -0
- package/dist/mod.cjs +193 -0
- package/dist/mod.d.cts +192 -0
- package/dist/mod.d.cts.map +1 -0
- package/dist/mod.d.ts +192 -0
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +193 -0
- package/dist/mod.js.map +1 -0
- package/package.json +70 -0
- package/src/mod.test.ts +727 -0
- package/src/mod.ts +392 -0
- package/tsdown.config.ts +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2024–2025 Hong Minhee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
<!-- deno-fmt-ignore-file -->
|
|
2
|
+
|
|
3
|
+
@logtape/koa: Koa adapter for LogTape
|
|
4
|
+
=====================================
|
|
5
|
+
|
|
6
|
+
[![JSR][JSR badge]][JSR]
|
|
7
|
+
[![npm][npm badge]][npm]
|
|
8
|
+
|
|
9
|
+
This package provides [Koa] middleware for HTTP request logging using [LogTape]
|
|
10
|
+
as the backend. It serves as an alternative to [koa-logger] with structured
|
|
11
|
+
logging support.
|
|
12
|
+
|
|
13
|
+
[JSR]: https://jsr.io/@logtape/koa
|
|
14
|
+
[JSR badge]: https://jsr.io/badges/@logtape/koa
|
|
15
|
+
[npm]: https://www.npmjs.com/package/@logtape/koa
|
|
16
|
+
[npm badge]: https://img.shields.io/npm/v/@logtape/koa?logo=npm
|
|
17
|
+
[Koa]: https://koajs.com/
|
|
18
|
+
[LogTape]: https://logtape.org/
|
|
19
|
+
[koa-logger]: https://github.com/koajs/logger
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
Installation
|
|
23
|
+
------------
|
|
24
|
+
|
|
25
|
+
~~~~ sh
|
|
26
|
+
deno add jsr:@logtape/koa # Deno
|
|
27
|
+
npm add @logtape/koa # npm
|
|
28
|
+
pnpm add @logtape/koa # pnpm
|
|
29
|
+
yarn add @logtape/koa # Yarn
|
|
30
|
+
bun add @logtape/koa # Bun
|
|
31
|
+
~~~~
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
Usage
|
|
35
|
+
-----
|
|
36
|
+
|
|
37
|
+
~~~~ typescript
|
|
38
|
+
import Koa from "koa";
|
|
39
|
+
import { configure, getConsoleSink } from "@logtape/logtape";
|
|
40
|
+
import { koaLogger } from "@logtape/koa";
|
|
41
|
+
|
|
42
|
+
await configure({
|
|
43
|
+
sinks: { console: getConsoleSink() },
|
|
44
|
+
loggers: [
|
|
45
|
+
{ category: ["koa"], sinks: ["console"], lowestLevel: "info" }
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const app = new Koa();
|
|
50
|
+
|
|
51
|
+
// Basic usage - should be used near the top of middleware stack
|
|
52
|
+
app.use(koaLogger());
|
|
53
|
+
|
|
54
|
+
app.use((ctx) => {
|
|
55
|
+
ctx.body = { hello: "world" };
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
app.listen(3000);
|
|
59
|
+
~~~~
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
Options
|
|
63
|
+
-------
|
|
64
|
+
|
|
65
|
+
~~~~ typescript
|
|
66
|
+
app.use(koaLogger({
|
|
67
|
+
category: ["myapp", "http"], // Custom category (default: ["koa"])
|
|
68
|
+
level: "debug", // Log level (default: "info")
|
|
69
|
+
format: "dev", // Predefined format (default: "combined")
|
|
70
|
+
skip: (ctx) => ctx.path === "/health", // Skip health check endpoint
|
|
71
|
+
logRequest: false, // Log after response (default: false)
|
|
72
|
+
}));
|
|
73
|
+
~~~~
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
Predefined Formats
|
|
77
|
+
------------------
|
|
78
|
+
|
|
79
|
+
- `"combined"` - Apache Combined Log Format with all properties (default)
|
|
80
|
+
- `"common"` - Apache Common Log Format (without referrer/userAgent)
|
|
81
|
+
- `"dev"` - Concise output for development
|
|
82
|
+
- `"short"` - Shorter format with remote address
|
|
83
|
+
- `"tiny"` - Minimal output
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
Custom Format
|
|
87
|
+
-------------
|
|
88
|
+
|
|
89
|
+
~~~~ typescript
|
|
90
|
+
app.use(koaLogger({
|
|
91
|
+
format: (ctx, responseTime) => ({
|
|
92
|
+
method: ctx.method,
|
|
93
|
+
path: ctx.path,
|
|
94
|
+
status: ctx.status,
|
|
95
|
+
duration: responseTime,
|
|
96
|
+
}),
|
|
97
|
+
}));
|
|
98
|
+
~~~~
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
License
|
|
102
|
+
-------
|
|
103
|
+
|
|
104
|
+
Distributed under the MIT License. See the *LICENSE* file for details.
|
package/deno.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@logtape/koa",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"exports": "./src/mod.ts",
|
|
6
|
+
"exclude": [
|
|
7
|
+
"coverage/",
|
|
8
|
+
"npm/",
|
|
9
|
+
"dist/"
|
|
10
|
+
],
|
|
11
|
+
"tasks": {
|
|
12
|
+
"build": "pnpm build",
|
|
13
|
+
"test": "deno test --allow-env --allow-sys --allow-net",
|
|
14
|
+
"test:node": {
|
|
15
|
+
"dependencies": [
|
|
16
|
+
"build"
|
|
17
|
+
],
|
|
18
|
+
"command": "node --experimental-transform-types --test"
|
|
19
|
+
},
|
|
20
|
+
"test:bun": {
|
|
21
|
+
"dependencies": [
|
|
22
|
+
"build"
|
|
23
|
+
],
|
|
24
|
+
"command": "bun test"
|
|
25
|
+
},
|
|
26
|
+
"test-all": {
|
|
27
|
+
"dependencies": [
|
|
28
|
+
"test",
|
|
29
|
+
"test:node",
|
|
30
|
+
"test:bun"
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
|
|
25
|
+
Object.defineProperty(exports, '__toESM', {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return __toESM;
|
|
29
|
+
}
|
|
30
|
+
});
|
package/dist/mod.cjs
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const __logtape_logtape = require_rolldown_runtime.__toESM(require("@logtape/logtape"));
|
|
3
|
+
|
|
4
|
+
//#region src/mod.ts
|
|
5
|
+
/**
|
|
6
|
+
* Get referrer from request headers.
|
|
7
|
+
* Returns undefined if the header is not present or empty.
|
|
8
|
+
*/
|
|
9
|
+
function getReferrer(ctx) {
|
|
10
|
+
const referrer = ctx.get("referrer") || ctx.get("referer");
|
|
11
|
+
return referrer !== "" ? referrer : void 0;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Get user agent from request headers.
|
|
15
|
+
* Returns undefined if the header is not present or empty.
|
|
16
|
+
*/
|
|
17
|
+
function getUserAgent(ctx) {
|
|
18
|
+
const userAgent = ctx.get("user-agent");
|
|
19
|
+
return userAgent !== "" ? userAgent : void 0;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get remote address from context.
|
|
23
|
+
* Returns undefined if not available.
|
|
24
|
+
*/
|
|
25
|
+
function getRemoteAddr(ctx) {
|
|
26
|
+
return ctx.ip !== "" ? ctx.ip : void 0;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get content length from response.
|
|
30
|
+
*/
|
|
31
|
+
function getContentLength(ctx) {
|
|
32
|
+
return ctx.response.length;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Build structured log properties from context.
|
|
36
|
+
*/
|
|
37
|
+
function buildProperties(ctx, responseTime) {
|
|
38
|
+
return {
|
|
39
|
+
method: ctx.method,
|
|
40
|
+
url: ctx.url,
|
|
41
|
+
path: ctx.path,
|
|
42
|
+
status: ctx.status,
|
|
43
|
+
responseTime,
|
|
44
|
+
contentLength: getContentLength(ctx),
|
|
45
|
+
remoteAddr: getRemoteAddr(ctx),
|
|
46
|
+
userAgent: getUserAgent(ctx),
|
|
47
|
+
referrer: getReferrer(ctx)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Combined format (Apache Combined Log Format).
|
|
52
|
+
* Returns all structured properties.
|
|
53
|
+
*/
|
|
54
|
+
function formatCombined(ctx, responseTime) {
|
|
55
|
+
return { ...buildProperties(ctx, responseTime) };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Common format (Apache Common Log Format).
|
|
59
|
+
* Like combined but without referrer and userAgent.
|
|
60
|
+
*/
|
|
61
|
+
function formatCommon(ctx, responseTime) {
|
|
62
|
+
const props = buildProperties(ctx, responseTime);
|
|
63
|
+
const { referrer: _referrer, userAgent: _userAgent,...rest } = props;
|
|
64
|
+
return rest;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Dev format (colored output for development).
|
|
68
|
+
* :method :path :status :response-time ms - :res[content-length]
|
|
69
|
+
*/
|
|
70
|
+
function formatDev(ctx, responseTime) {
|
|
71
|
+
const contentLength = getContentLength(ctx) ?? "-";
|
|
72
|
+
return `${ctx.method} ${ctx.path} ${ctx.status} ${responseTime.toFixed(3)} ms - ${contentLength}`;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Short format.
|
|
76
|
+
* :remote-addr :method :url :status :res[content-length] - :response-time ms
|
|
77
|
+
*/
|
|
78
|
+
function formatShort(ctx, responseTime) {
|
|
79
|
+
const remoteAddr = getRemoteAddr(ctx) ?? "-";
|
|
80
|
+
const contentLength = getContentLength(ctx) ?? "-";
|
|
81
|
+
return `${remoteAddr} ${ctx.method} ${ctx.url} ${ctx.status} ${contentLength} - ${responseTime.toFixed(3)} ms`;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Tiny format (minimal output).
|
|
85
|
+
* :method :path :status :res[content-length] - :response-time ms
|
|
86
|
+
*/
|
|
87
|
+
function formatTiny(ctx, responseTime) {
|
|
88
|
+
const contentLength = getContentLength(ctx) ?? "-";
|
|
89
|
+
return `${ctx.method} ${ctx.path} ${ctx.status} ${contentLength} - ${responseTime.toFixed(3)} ms`;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Map of predefined format functions.
|
|
93
|
+
*/
|
|
94
|
+
const predefinedFormats = {
|
|
95
|
+
combined: formatCombined,
|
|
96
|
+
common: formatCommon,
|
|
97
|
+
dev: formatDev,
|
|
98
|
+
short: formatShort,
|
|
99
|
+
tiny: formatTiny
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Normalize category to array format.
|
|
103
|
+
*/
|
|
104
|
+
function normalizeCategory(category) {
|
|
105
|
+
return typeof category === "string" ? [category] : category;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Creates Koa middleware for HTTP request logging using LogTape.
|
|
109
|
+
*
|
|
110
|
+
* This middleware provides Morgan-compatible request logging with LogTape
|
|
111
|
+
* as the backend, supporting structured logging and customizable formats.
|
|
112
|
+
* It serves as an alternative to koa-logger with structured logging support.
|
|
113
|
+
*
|
|
114
|
+
* @example Basic usage
|
|
115
|
+
* ```typescript
|
|
116
|
+
* import Koa from "koa";
|
|
117
|
+
* import { configure, getConsoleSink } from "@logtape/logtape";
|
|
118
|
+
* import { koaLogger } from "@logtape/koa";
|
|
119
|
+
*
|
|
120
|
+
* await configure({
|
|
121
|
+
* sinks: { console: getConsoleSink() },
|
|
122
|
+
* loggers: [
|
|
123
|
+
* { category: ["koa"], sinks: ["console"], lowestLevel: "info" }
|
|
124
|
+
* ],
|
|
125
|
+
* });
|
|
126
|
+
*
|
|
127
|
+
* const app = new Koa();
|
|
128
|
+
* app.use(koaLogger());
|
|
129
|
+
*
|
|
130
|
+
* app.use((ctx) => {
|
|
131
|
+
* ctx.body = { hello: "world" };
|
|
132
|
+
* });
|
|
133
|
+
*
|
|
134
|
+
* app.listen(3000);
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @example With custom options
|
|
138
|
+
* ```typescript
|
|
139
|
+
* app.use(koaLogger({
|
|
140
|
+
* category: ["myapp", "http"],
|
|
141
|
+
* level: "debug",
|
|
142
|
+
* format: "dev",
|
|
143
|
+
* skip: (ctx) => ctx.path === "/health",
|
|
144
|
+
* }));
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @example With custom format function
|
|
148
|
+
* ```typescript
|
|
149
|
+
* app.use(koaLogger({
|
|
150
|
+
* format: (ctx, responseTime) => ({
|
|
151
|
+
* method: ctx.method,
|
|
152
|
+
* path: ctx.path,
|
|
153
|
+
* status: ctx.status,
|
|
154
|
+
* duration: responseTime,
|
|
155
|
+
* }),
|
|
156
|
+
* }));
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* @param options Configuration options for the middleware.
|
|
160
|
+
* @returns Koa middleware function.
|
|
161
|
+
* @since 1.3.0
|
|
162
|
+
*/
|
|
163
|
+
function koaLogger(options = {}) {
|
|
164
|
+
const category = normalizeCategory(options.category ?? ["koa"]);
|
|
165
|
+
const logger = (0, __logtape_logtape.getLogger)(category);
|
|
166
|
+
const level = options.level ?? "info";
|
|
167
|
+
const formatOption = options.format ?? "combined";
|
|
168
|
+
const skip = options.skip ?? (() => false);
|
|
169
|
+
const logRequest = options.logRequest ?? false;
|
|
170
|
+
const formatFn = typeof formatOption === "string" ? predefinedFormats[formatOption] : formatOption;
|
|
171
|
+
const logMethod = logger[level].bind(logger);
|
|
172
|
+
return async (ctx, next) => {
|
|
173
|
+
const startTime = Date.now();
|
|
174
|
+
if (logRequest) {
|
|
175
|
+
if (!skip(ctx)) {
|
|
176
|
+
const result$1 = formatFn(ctx, 0);
|
|
177
|
+
if (typeof result$1 === "string") logMethod(result$1);
|
|
178
|
+
else logMethod("{method} {url}", result$1);
|
|
179
|
+
}
|
|
180
|
+
await next();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
await next();
|
|
184
|
+
if (skip(ctx)) return;
|
|
185
|
+
const responseTime = Date.now() - startTime;
|
|
186
|
+
const result = formatFn(ctx, responseTime);
|
|
187
|
+
if (typeof result === "string") logMethod(result);
|
|
188
|
+
else logMethod("{method} {url} {status} - {responseTime} ms", result);
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
//#endregion
|
|
193
|
+
exports.koaLogger = koaLogger;
|
package/dist/mod.d.cts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { LogLevel, LogLevel as LogLevel$1 } from "@logtape/logtape";
|
|
2
|
+
|
|
3
|
+
//#region src/mod.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Minimal Koa Context interface for compatibility across Koa 2.x and 3.x.
|
|
7
|
+
*
|
|
8
|
+
* This interface includes common aliases available on the Koa context object.
|
|
9
|
+
* See https://koajs.com/#context for the full API.
|
|
10
|
+
*
|
|
11
|
+
* @since 1.3.0
|
|
12
|
+
*/
|
|
13
|
+
interface KoaContext {
|
|
14
|
+
/** HTTP request method (alias for ctx.request.method) */
|
|
15
|
+
method: string;
|
|
16
|
+
/** Request URL (alias for ctx.request.url) */
|
|
17
|
+
url: string;
|
|
18
|
+
/** Request pathname (alias for ctx.request.path) */
|
|
19
|
+
path: string;
|
|
20
|
+
/** HTTP response status code (alias for ctx.response.status) */
|
|
21
|
+
status: number;
|
|
22
|
+
/** Remote client IP address (alias for ctx.request.ip) */
|
|
23
|
+
ip: string;
|
|
24
|
+
/** Koa Response object */
|
|
25
|
+
response: {
|
|
26
|
+
length?: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Get a request header field value (case-insensitive).
|
|
30
|
+
* @param field The header field name.
|
|
31
|
+
* @returns The header value, or an empty string if not present.
|
|
32
|
+
*/
|
|
33
|
+
get(field: string): string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Koa middleware function type.
|
|
37
|
+
* @since 1.3.0
|
|
38
|
+
*/
|
|
39
|
+
type KoaMiddleware = (ctx: KoaContext, next: () => Promise<void>) => Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Predefined log format names compatible with Morgan.
|
|
42
|
+
* @since 1.3.0
|
|
43
|
+
*/
|
|
44
|
+
type PredefinedFormat = "combined" | "common" | "dev" | "short" | "tiny";
|
|
45
|
+
/**
|
|
46
|
+
* Custom format function for request logging.
|
|
47
|
+
*
|
|
48
|
+
* @param ctx The Koa context object.
|
|
49
|
+
* @param responseTime The response time in milliseconds.
|
|
50
|
+
* @returns A string message or an object with structured properties.
|
|
51
|
+
* @since 1.3.0
|
|
52
|
+
*/
|
|
53
|
+
type FormatFunction = (ctx: KoaContext, responseTime: number) => string | Record<string, unknown>;
|
|
54
|
+
/**
|
|
55
|
+
* Structured log properties for HTTP requests.
|
|
56
|
+
* @since 1.3.0
|
|
57
|
+
*/
|
|
58
|
+
interface RequestLogProperties {
|
|
59
|
+
/** HTTP request method */
|
|
60
|
+
method: string;
|
|
61
|
+
/** Request URL */
|
|
62
|
+
url: string;
|
|
63
|
+
/** Request path */
|
|
64
|
+
path: string;
|
|
65
|
+
/** HTTP response status code */
|
|
66
|
+
status: number;
|
|
67
|
+
/** Response time in milliseconds */
|
|
68
|
+
responseTime: number;
|
|
69
|
+
/** Response content-length */
|
|
70
|
+
contentLength: number | undefined;
|
|
71
|
+
/** Remote client address */
|
|
72
|
+
remoteAddr: string | undefined;
|
|
73
|
+
/** User-Agent header value */
|
|
74
|
+
userAgent: string | undefined;
|
|
75
|
+
/** Referrer header value */
|
|
76
|
+
referrer: string | undefined;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Options for configuring the Koa LogTape middleware.
|
|
80
|
+
* @since 1.3.0
|
|
81
|
+
*/
|
|
82
|
+
interface KoaLogTapeOptions {
|
|
83
|
+
/**
|
|
84
|
+
* The LogTape category to use for logging.
|
|
85
|
+
* @default ["koa"]
|
|
86
|
+
*/
|
|
87
|
+
readonly category?: string | readonly string[];
|
|
88
|
+
/**
|
|
89
|
+
* The log level to use for request logging.
|
|
90
|
+
* @default "info"
|
|
91
|
+
*/
|
|
92
|
+
readonly level?: LogLevel$1;
|
|
93
|
+
/**
|
|
94
|
+
* The format for log output.
|
|
95
|
+
* Can be a predefined format name or a custom format function.
|
|
96
|
+
*
|
|
97
|
+
* Predefined formats:
|
|
98
|
+
* - `"combined"` - Apache Combined Log Format (structured, default)
|
|
99
|
+
* - `"common"` - Apache Common Log Format (structured, no referrer/userAgent)
|
|
100
|
+
* - `"dev"` - Concise colored output for development (string)
|
|
101
|
+
* - `"short"` - Shorter than common (string)
|
|
102
|
+
* - `"tiny"` - Minimal output (string)
|
|
103
|
+
*
|
|
104
|
+
* @default "combined"
|
|
105
|
+
*/
|
|
106
|
+
readonly format?: PredefinedFormat | FormatFunction;
|
|
107
|
+
/**
|
|
108
|
+
* Function to determine whether logging should be skipped.
|
|
109
|
+
* Return `true` to skip logging for a request.
|
|
110
|
+
*
|
|
111
|
+
* @example Skip logging for health check endpoint
|
|
112
|
+
* ```typescript
|
|
113
|
+
* app.use(koaLogger({
|
|
114
|
+
* skip: (ctx) => ctx.path === "/health",
|
|
115
|
+
* }));
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @default () => false
|
|
119
|
+
*/
|
|
120
|
+
readonly skip?: (ctx: KoaContext) => boolean;
|
|
121
|
+
/**
|
|
122
|
+
* If `true`, logs are written immediately when the request is received.
|
|
123
|
+
* If `false` (default), logs are written after the response is sent.
|
|
124
|
+
*
|
|
125
|
+
* Note: When `logRequest` is `true`, response-related properties
|
|
126
|
+
* (status, responseTime, contentLength) will not be available.
|
|
127
|
+
*
|
|
128
|
+
* @default false
|
|
129
|
+
*/
|
|
130
|
+
readonly logRequest?: boolean;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Creates Koa middleware for HTTP request logging using LogTape.
|
|
134
|
+
*
|
|
135
|
+
* This middleware provides Morgan-compatible request logging with LogTape
|
|
136
|
+
* as the backend, supporting structured logging and customizable formats.
|
|
137
|
+
* It serves as an alternative to koa-logger with structured logging support.
|
|
138
|
+
*
|
|
139
|
+
* @example Basic usage
|
|
140
|
+
* ```typescript
|
|
141
|
+
* import Koa from "koa";
|
|
142
|
+
* import { configure, getConsoleSink } from "@logtape/logtape";
|
|
143
|
+
* import { koaLogger } from "@logtape/koa";
|
|
144
|
+
*
|
|
145
|
+
* await configure({
|
|
146
|
+
* sinks: { console: getConsoleSink() },
|
|
147
|
+
* loggers: [
|
|
148
|
+
* { category: ["koa"], sinks: ["console"], lowestLevel: "info" }
|
|
149
|
+
* ],
|
|
150
|
+
* });
|
|
151
|
+
*
|
|
152
|
+
* const app = new Koa();
|
|
153
|
+
* app.use(koaLogger());
|
|
154
|
+
*
|
|
155
|
+
* app.use((ctx) => {
|
|
156
|
+
* ctx.body = { hello: "world" };
|
|
157
|
+
* });
|
|
158
|
+
*
|
|
159
|
+
* app.listen(3000);
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @example With custom options
|
|
163
|
+
* ```typescript
|
|
164
|
+
* app.use(koaLogger({
|
|
165
|
+
* category: ["myapp", "http"],
|
|
166
|
+
* level: "debug",
|
|
167
|
+
* format: "dev",
|
|
168
|
+
* skip: (ctx) => ctx.path === "/health",
|
|
169
|
+
* }));
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
172
|
+
* @example With custom format function
|
|
173
|
+
* ```typescript
|
|
174
|
+
* app.use(koaLogger({
|
|
175
|
+
* format: (ctx, responseTime) => ({
|
|
176
|
+
* method: ctx.method,
|
|
177
|
+
* path: ctx.path,
|
|
178
|
+
* status: ctx.status,
|
|
179
|
+
* duration: responseTime,
|
|
180
|
+
* }),
|
|
181
|
+
* }));
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* @param options Configuration options for the middleware.
|
|
185
|
+
* @returns Koa middleware function.
|
|
186
|
+
* @since 1.3.0
|
|
187
|
+
*/
|
|
188
|
+
declare function koaLogger(options?: KoaLogTapeOptions): KoaMiddleware;
|
|
189
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
190
|
+
//#endregion
|
|
191
|
+
export { FormatFunction, KoaContext, KoaLogTapeOptions, KoaMiddleware, LogLevel, PredefinedFormat, RequestLogProperties, koaLogger };
|
|
192
|
+
//# sourceMappingURL=mod.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.cts","names":[],"sources":["../src/mod.ts"],"sourcesContent":[],"mappings":";;;;;AAYA;AA2BA;;;;;AAGY;AAMA,UApCK,UAAA,CAoCW;EAUhB;EAAc,MAAA,EAAA,MAAA;EAAA;EACT,GAEH,EAAA,MAAA;EAAM;EAMH,IAAA,EAAA,MAAA;EAyBA;EAAiB,MAAA,EAAA,MAAA;EAAA;EAWP,EAAA,EAeP,MAAA;EAAgB;EAAiB,QAe7B,EAAA;IAAU,MAAA,CAAA,EAAA,MAAA;EAkNlB,CAAA;EAAS;;;AAET;;;;;;;;KAlTJ,aAAA,SACL,wBACO,kBACT;;;;;KAMO,gBAAA;;;;;;;;;KAUA,cAAA,SACL,8CAEO;;;;;UAMG,oBAAA;;;;;;;;;;;;;;;;;;;;;;;;UAyBA,iBAAA;;;;;;;;;;mBAWE;;;;;;;;;;;;;;oBAeC,mBAAmB;;;;;;;;;;;;;;wBAef;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAkNR,SAAA,WACL,oBACR"}
|