@mihari/logger-pino 0.1.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/LICENSE +21 -0
- package/README.md +39 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +84 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
- package/src/index.ts +94 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mihari Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, 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,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @mihari/logger-pino
|
|
2
|
+
|
|
3
|
+
Pino transport for the mihari log collection library.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mihari/logger-pino pino
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import pino from "pino";
|
|
15
|
+
import { createMihariTransport } from "@mihari/logger-pino";
|
|
16
|
+
|
|
17
|
+
const transport = createMihariTransport({
|
|
18
|
+
token: "your-api-token",
|
|
19
|
+
endpoint: "https://logs.example.com",
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const logger = pino(transport);
|
|
23
|
+
logger.info("Hello from pino");
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Level Mapping
|
|
27
|
+
|
|
28
|
+
| Pino Level | Mihari Level |
|
|
29
|
+
|------------|-------------|
|
|
30
|
+
| 10 (trace) | debug |
|
|
31
|
+
| 20 (debug) | debug |
|
|
32
|
+
| 30 (info) | info |
|
|
33
|
+
| 40 (warn) | warn |
|
|
34
|
+
| 50 (error) | error |
|
|
35
|
+
| 60 (fatal) | fatal |
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Writable } from "stream";
|
|
2
|
+
import { MihariConfig } from "@mihari/logger-types";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a pino transport writable stream that forwards logs to mihari.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import pino from "pino";
|
|
9
|
+
* import { createMihariTransport } from "@mihari/logger-pino";
|
|
10
|
+
*
|
|
11
|
+
* const transport = createMihariTransport({
|
|
12
|
+
* token: "your-token",
|
|
13
|
+
* endpoint: "https://logs.example.com",
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* const logger = pino(transport);
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Or with pino's transport option:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const logger = pino({
|
|
22
|
+
* transport: { target: "@mihari/logger-pino" }
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function createMihariTransport(config: MihariConfig): Writable;
|
|
27
|
+
/**
|
|
28
|
+
* Default export for use as a pino transport target.
|
|
29
|
+
* The module default export must be a function that returns a writable stream.
|
|
30
|
+
*/
|
|
31
|
+
export default function (config: MihariConfig): Writable;
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,OAAO,EAAY,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAsB9D;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,YAAY,GAAG,QAAQ,CAsCpE;AAED;;;GAGG;AACH,MAAM,CAAC,OAAO,WAAW,MAAM,EAAE,YAAY,GAAG,QAAQ,CAEvD"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMihariTransport = createMihariTransport;
|
|
4
|
+
exports.default = default_1;
|
|
5
|
+
const stream_1 = require("stream");
|
|
6
|
+
const logger_core_1 = require("@mihari/logger-core");
|
|
7
|
+
const logger_types_1 = require("@mihari/logger-types");
|
|
8
|
+
/**
|
|
9
|
+
* Maps pino numeric levels to mihari LogLevel values.
|
|
10
|
+
* Pino levels: 10=trace, 20=debug, 30=info, 40=warn, 50=error, 60=fatal
|
|
11
|
+
*/
|
|
12
|
+
function mapPinoLevel(pinoLevel) {
|
|
13
|
+
if (pinoLevel <= 20)
|
|
14
|
+
return logger_types_1.LogLevel.Debug;
|
|
15
|
+
if (pinoLevel <= 30)
|
|
16
|
+
return logger_types_1.LogLevel.Info;
|
|
17
|
+
if (pinoLevel <= 40)
|
|
18
|
+
return logger_types_1.LogLevel.Warn;
|
|
19
|
+
if (pinoLevel <= 50)
|
|
20
|
+
return logger_types_1.LogLevel.Error;
|
|
21
|
+
return logger_types_1.LogLevel.Fatal;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates a pino transport writable stream that forwards logs to mihari.
|
|
25
|
+
*
|
|
26
|
+
* Usage:
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import pino from "pino";
|
|
29
|
+
* import { createMihariTransport } from "@mihari/logger-pino";
|
|
30
|
+
*
|
|
31
|
+
* const transport = createMihariTransport({
|
|
32
|
+
* token: "your-token",
|
|
33
|
+
* endpoint: "https://logs.example.com",
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* const logger = pino(transport);
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Or with pino's transport option:
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const logger = pino({
|
|
42
|
+
* transport: { target: "@mihari/logger-pino" }
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
function createMihariTransport(config) {
|
|
47
|
+
const client = new logger_core_1.MihariClient(config);
|
|
48
|
+
const stream = new stream_1.Writable({
|
|
49
|
+
objectMode: true,
|
|
50
|
+
write(chunk, _encoding, callback) {
|
|
51
|
+
try {
|
|
52
|
+
const obj = typeof chunk === "string" ? JSON.parse(chunk) : chunk;
|
|
53
|
+
const { level, time, msg, message, ...rest } = obj;
|
|
54
|
+
const mihariLevel = mapPinoLevel(level);
|
|
55
|
+
const logMessage = msg ?? message ?? "";
|
|
56
|
+
const { pid, hostname, ...metadata } = rest;
|
|
57
|
+
client.log(mihariLevel, String(logMessage), {
|
|
58
|
+
...(pid !== undefined ? { pid } : {}),
|
|
59
|
+
...(hostname !== undefined ? { hostname } : {}),
|
|
60
|
+
...metadata,
|
|
61
|
+
});
|
|
62
|
+
callback();
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
callback(err instanceof Error ? err : new Error(String(err)));
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
final(callback) {
|
|
69
|
+
client
|
|
70
|
+
.shutdown()
|
|
71
|
+
.then(() => callback())
|
|
72
|
+
.catch((err) => callback(err instanceof Error ? err : new Error(String(err))));
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
return stream;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Default export for use as a pino transport target.
|
|
79
|
+
* The module default export must be a function that returns a writable stream.
|
|
80
|
+
*/
|
|
81
|
+
function default_1(config) {
|
|
82
|
+
return createMihariTransport(config);
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AA+CA,sDAsCC;AAMD,4BAEC;AA7FD,mCAAkC;AAClC,qDAAmD;AACnD,uDAA8D;AAE9D;;;GAGG;AACH,SAAS,YAAY,CAAC,SAAiB;IACrC,IAAI,SAAS,IAAI,EAAE;QAAE,OAAO,uBAAQ,CAAC,KAAK,CAAC;IAC3C,IAAI,SAAS,IAAI,EAAE;QAAE,OAAO,uBAAQ,CAAC,IAAI,CAAC;IAC1C,IAAI,SAAS,IAAI,EAAE;QAAE,OAAO,uBAAQ,CAAC,IAAI,CAAC;IAC1C,IAAI,SAAS,IAAI,EAAE;QAAE,OAAO,uBAAQ,CAAC,KAAK,CAAC;IAC3C,OAAO,uBAAQ,CAAC,KAAK,CAAC;AACxB,CAAC;AAUD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,qBAAqB,CAAC,MAAoB;IACxD,MAAM,MAAM,GAAG,IAAI,0BAAY,CAAC,MAAM,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QAAQ,EAAE,CAAC;iBACtB,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CACb,QAAQ,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAC9D,CAAC;QACN,CAAC;KACF,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,mBAAyB,MAAoB;IAC3C,OAAO,qBAAqB,CAAC,MAAM,CAAC,CAAC;AACvC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mihari/logger-pino",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pino transport for mihari log collection",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"types": "src/index.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"clean": "rm -rf dist",
|
|
13
|
+
"test": "cd ../.. && npx vitest run --config vitest.config.ts packages/pino"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"vitest": "^3.0.0"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@mihari/logger-core": "^0.1.0",
|
|
20
|
+
"@mihari/logger-types": "^0.1.0"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"pino": ">=8.0.0"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/mihari/mihari-js",
|
|
32
|
+
"directory": "packages/pino"
|
|
33
|
+
},
|
|
34
|
+
"gitHead": "dc10a3217caa819965eb3a1e2ff3901a16e510aa"
|
|
35
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Writable } from "stream";
|
|
2
|
+
import { MihariClient } from "@mihari/logger-core";
|
|
3
|
+
import { LogLevel, MihariConfig } from "@mihari/logger-types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Maps pino numeric levels to mihari LogLevel values.
|
|
7
|
+
* Pino levels: 10=trace, 20=debug, 30=info, 40=warn, 50=error, 60=fatal
|
|
8
|
+
*/
|
|
9
|
+
function mapPinoLevel(pinoLevel: number): LogLevel {
|
|
10
|
+
if (pinoLevel <= 20) return LogLevel.Debug;
|
|
11
|
+
if (pinoLevel <= 30) return LogLevel.Info;
|
|
12
|
+
if (pinoLevel <= 40) return LogLevel.Warn;
|
|
13
|
+
if (pinoLevel <= 50) return LogLevel.Error;
|
|
14
|
+
return LogLevel.Fatal;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface PinoLogObject {
|
|
18
|
+
level: number;
|
|
19
|
+
time: number;
|
|
20
|
+
msg?: string;
|
|
21
|
+
message?: string;
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Creates a pino transport writable stream that forwards logs to mihari.
|
|
27
|
+
*
|
|
28
|
+
* Usage:
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import pino from "pino";
|
|
31
|
+
* import { createMihariTransport } from "@mihari/logger-pino";
|
|
32
|
+
*
|
|
33
|
+
* const transport = createMihariTransport({
|
|
34
|
+
* token: "your-token",
|
|
35
|
+
* endpoint: "https://logs.example.com",
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* const logger = pino(transport);
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* Or with pino's transport option:
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const logger = pino({
|
|
44
|
+
* transport: { target: "@mihari/logger-pino" }
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export function createMihariTransport(config: MihariConfig): Writable {
|
|
49
|
+
const client = new MihariClient(config);
|
|
50
|
+
|
|
51
|
+
const stream = new Writable({
|
|
52
|
+
objectMode: true,
|
|
53
|
+
write(chunk: PinoLogObject | string, _encoding, callback) {
|
|
54
|
+
try {
|
|
55
|
+
const obj: PinoLogObject =
|
|
56
|
+
typeof chunk === "string" ? JSON.parse(chunk) : chunk;
|
|
57
|
+
|
|
58
|
+
const { level, time, msg, message, ...rest } = obj;
|
|
59
|
+
const mihariLevel = mapPinoLevel(level);
|
|
60
|
+
const logMessage = msg ?? message ?? "";
|
|
61
|
+
|
|
62
|
+
const { pid, hostname, ...metadata } = rest;
|
|
63
|
+
|
|
64
|
+
client.log(mihariLevel, String(logMessage), {
|
|
65
|
+
...(pid !== undefined ? { pid } : {}),
|
|
66
|
+
...(hostname !== undefined ? { hostname } : {}),
|
|
67
|
+
...metadata,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
callback();
|
|
71
|
+
} catch (err) {
|
|
72
|
+
callback(err instanceof Error ? err : new Error(String(err)));
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
final(callback) {
|
|
76
|
+
client
|
|
77
|
+
.shutdown()
|
|
78
|
+
.then(() => callback())
|
|
79
|
+
.catch((err) =>
|
|
80
|
+
callback(err instanceof Error ? err : new Error(String(err)))
|
|
81
|
+
);
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return stream;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Default export for use as a pino transport target.
|
|
90
|
+
* The module default export must be a function that returns a writable stream.
|
|
91
|
+
*/
|
|
92
|
+
export default function (config: MihariConfig): Writable {
|
|
93
|
+
return createMihariTransport(config);
|
|
94
|
+
}
|