@mihari/logger-bunyan 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 +43 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +75 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
- package/src/index.ts +91 -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,43 @@
|
|
|
1
|
+
# @mihari/logger-bunyan
|
|
2
|
+
|
|
3
|
+
Bunyan stream for the mihari log collection library.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mihari/logger-bunyan bunyan
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import bunyan from "bunyan";
|
|
15
|
+
import { MihariBunyanStream } from "@mihari/logger-bunyan";
|
|
16
|
+
|
|
17
|
+
const mihariStream = new MihariBunyanStream({
|
|
18
|
+
token: "your-api-token",
|
|
19
|
+
endpoint: "https://logs.example.com",
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const logger = bunyan.createLogger({
|
|
23
|
+
name: "my-app",
|
|
24
|
+
streams: [{ type: "raw", stream: mihariStream }],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
logger.info("Hello from bunyan");
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Level Mapping
|
|
31
|
+
|
|
32
|
+
| Bunyan Level | Mihari Level |
|
|
33
|
+
|-------------|-------------|
|
|
34
|
+
| 10 (trace) | debug |
|
|
35
|
+
| 20 (debug) | debug |
|
|
36
|
+
| 30 (info) | info |
|
|
37
|
+
| 40 (warn) | warn |
|
|
38
|
+
| 50 (error) | error |
|
|
39
|
+
| 60 (fatal) | fatal |
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Writable } from "stream";
|
|
2
|
+
import { MihariConfig } from "@mihari/logger-types";
|
|
3
|
+
interface BunyanLogRecord {
|
|
4
|
+
level: number;
|
|
5
|
+
msg: string;
|
|
6
|
+
time: string;
|
|
7
|
+
name: string;
|
|
8
|
+
hostname: string;
|
|
9
|
+
pid: number;
|
|
10
|
+
v: number;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Bunyan writable stream that forwards logs to mihari.
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import bunyan from "bunyan";
|
|
19
|
+
* import { MihariBunyanStream } from "@mihari/logger-bunyan";
|
|
20
|
+
*
|
|
21
|
+
* const mihariStream = new MihariBunyanStream({
|
|
22
|
+
* token: "your-token",
|
|
23
|
+
* endpoint: "https://logs.example.com",
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* const logger = bunyan.createLogger({
|
|
27
|
+
* name: "my-app",
|
|
28
|
+
* streams: [
|
|
29
|
+
* { type: "raw", stream: mihariStream },
|
|
30
|
+
* ],
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* logger.info("Hello from bunyan");
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare class MihariBunyanStream extends Writable {
|
|
37
|
+
private readonly client;
|
|
38
|
+
constructor(config: MihariConfig);
|
|
39
|
+
_write(chunk: BunyanLogRecord, _encoding: string, callback: (error?: Error | null) => void): void;
|
|
40
|
+
_final(callback: (error?: Error | null) => void): void;
|
|
41
|
+
}
|
|
42
|
+
export default MihariBunyanStream;
|
|
43
|
+
//# 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;AAc9D,UAAU,eAAe;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,kBAAmB,SAAQ,QAAQ;IAC9C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAe;gBAE1B,MAAM,EAAE,YAAY;IAKvB,MAAM,CACb,KAAK,EAAE,eAAe,EACtB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,GACvC,IAAI;IAkBE,MAAM,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,GAAG,IAAI;CAQhE;AAED,eAAe,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MihariBunyanStream = void 0;
|
|
4
|
+
const stream_1 = require("stream");
|
|
5
|
+
const logger_core_1 = require("@mihari/logger-core");
|
|
6
|
+
const logger_types_1 = require("@mihari/logger-types");
|
|
7
|
+
/**
|
|
8
|
+
* Maps bunyan numeric levels to mihari LogLevel values.
|
|
9
|
+
* Bunyan levels: 10=trace, 20=debug, 30=info, 40=warn, 50=error, 60=fatal
|
|
10
|
+
*/
|
|
11
|
+
function mapBunyanLevel(bunyanLevel) {
|
|
12
|
+
if (bunyanLevel <= 20)
|
|
13
|
+
return logger_types_1.LogLevel.Debug;
|
|
14
|
+
if (bunyanLevel <= 30)
|
|
15
|
+
return logger_types_1.LogLevel.Info;
|
|
16
|
+
if (bunyanLevel <= 40)
|
|
17
|
+
return logger_types_1.LogLevel.Warn;
|
|
18
|
+
if (bunyanLevel <= 50)
|
|
19
|
+
return logger_types_1.LogLevel.Error;
|
|
20
|
+
return logger_types_1.LogLevel.Fatal;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Bunyan writable stream that forwards logs to mihari.
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import bunyan from "bunyan";
|
|
28
|
+
* import { MihariBunyanStream } from "@mihari/logger-bunyan";
|
|
29
|
+
*
|
|
30
|
+
* const mihariStream = new MihariBunyanStream({
|
|
31
|
+
* token: "your-token",
|
|
32
|
+
* endpoint: "https://logs.example.com",
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* const logger = bunyan.createLogger({
|
|
36
|
+
* name: "my-app",
|
|
37
|
+
* streams: [
|
|
38
|
+
* { type: "raw", stream: mihariStream },
|
|
39
|
+
* ],
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* logger.info("Hello from bunyan");
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
class MihariBunyanStream extends stream_1.Writable {
|
|
46
|
+
constructor(config) {
|
|
47
|
+
super({ objectMode: true });
|
|
48
|
+
this.client = new logger_core_1.MihariClient(config);
|
|
49
|
+
}
|
|
50
|
+
_write(chunk, _encoding, callback) {
|
|
51
|
+
try {
|
|
52
|
+
const { level, msg, time, name, hostname, pid, v, ...rest } = chunk;
|
|
53
|
+
const mihariLevel = mapBunyanLevel(level);
|
|
54
|
+
this.client.log(mihariLevel, msg, {
|
|
55
|
+
bunyanName: name,
|
|
56
|
+
hostname,
|
|
57
|
+
pid,
|
|
58
|
+
...rest,
|
|
59
|
+
});
|
|
60
|
+
callback();
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
callback(err instanceof Error ? err : new Error(String(err)));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
_final(callback) {
|
|
67
|
+
this.client
|
|
68
|
+
.shutdown()
|
|
69
|
+
.then(() => callback())
|
|
70
|
+
.catch((err) => callback(err instanceof Error ? err : new Error(String(err))));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.MihariBunyanStream = MihariBunyanStream;
|
|
74
|
+
exports.default = MihariBunyanStream;
|
|
75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C,OAAO,GAAG,EAAE,CAAC;YACb,QAAQ,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;IAEQ,MAAM,CAAC,QAAwC;QACtD,IAAI,CAAC,MAAM;aACR,QAAQ,EAAE;aACV,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC;aACtB,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;IACN,CAAC;CACF;AAtCD,gDAsCC;AAED,kBAAe,kBAAkB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mihari/logger-bunyan",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Bunyan stream 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/bunyan"
|
|
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
|
+
"bunyan": ">=1.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/bunyan"
|
|
33
|
+
},
|
|
34
|
+
"gitHead": "dc10a3217caa819965eb3a1e2ff3901a16e510aa"
|
|
35
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Writable } from "stream";
|
|
2
|
+
import { MihariClient } from "@mihari/logger-core";
|
|
3
|
+
import { LogLevel, MihariConfig } from "@mihari/logger-types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Maps bunyan numeric levels to mihari LogLevel values.
|
|
7
|
+
* Bunyan levels: 10=trace, 20=debug, 30=info, 40=warn, 50=error, 60=fatal
|
|
8
|
+
*/
|
|
9
|
+
function mapBunyanLevel(bunyanLevel: number): LogLevel {
|
|
10
|
+
if (bunyanLevel <= 20) return LogLevel.Debug;
|
|
11
|
+
if (bunyanLevel <= 30) return LogLevel.Info;
|
|
12
|
+
if (bunyanLevel <= 40) return LogLevel.Warn;
|
|
13
|
+
if (bunyanLevel <= 50) return LogLevel.Error;
|
|
14
|
+
return LogLevel.Fatal;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface BunyanLogRecord {
|
|
18
|
+
level: number;
|
|
19
|
+
msg: string;
|
|
20
|
+
time: string;
|
|
21
|
+
name: string;
|
|
22
|
+
hostname: string;
|
|
23
|
+
pid: number;
|
|
24
|
+
v: number;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Bunyan writable stream that forwards logs to mihari.
|
|
30
|
+
*
|
|
31
|
+
* Usage:
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import bunyan from "bunyan";
|
|
34
|
+
* import { MihariBunyanStream } from "@mihari/logger-bunyan";
|
|
35
|
+
*
|
|
36
|
+
* const mihariStream = new MihariBunyanStream({
|
|
37
|
+
* token: "your-token",
|
|
38
|
+
* endpoint: "https://logs.example.com",
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* const logger = bunyan.createLogger({
|
|
42
|
+
* name: "my-app",
|
|
43
|
+
* streams: [
|
|
44
|
+
* { type: "raw", stream: mihariStream },
|
|
45
|
+
* ],
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* logger.info("Hello from bunyan");
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export class MihariBunyanStream extends Writable {
|
|
52
|
+
private readonly client: MihariClient;
|
|
53
|
+
|
|
54
|
+
constructor(config: MihariConfig) {
|
|
55
|
+
super({ objectMode: true });
|
|
56
|
+
this.client = new MihariClient(config);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
override _write(
|
|
60
|
+
chunk: BunyanLogRecord,
|
|
61
|
+
_encoding: string,
|
|
62
|
+
callback: (error?: Error | null) => void
|
|
63
|
+
): void {
|
|
64
|
+
try {
|
|
65
|
+
const { level, msg, time, name, hostname, pid, v, ...rest } = chunk;
|
|
66
|
+
const mihariLevel = mapBunyanLevel(level);
|
|
67
|
+
|
|
68
|
+
this.client.log(mihariLevel, msg, {
|
|
69
|
+
bunyanName: name,
|
|
70
|
+
hostname,
|
|
71
|
+
pid,
|
|
72
|
+
...rest,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
callback();
|
|
76
|
+
} catch (err) {
|
|
77
|
+
callback(err instanceof Error ? err : new Error(String(err)));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
override _final(callback: (error?: Error | null) => void): void {
|
|
82
|
+
this.client
|
|
83
|
+
.shutdown()
|
|
84
|
+
.then(() => callback())
|
|
85
|
+
.catch((err) =>
|
|
86
|
+
callback(err instanceof Error ? err : new Error(String(err)))
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export default MihariBunyanStream;
|