@kanjijs/logger 0.2.0-beta.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/README.md +41 -0
- package/package.json +13 -0
- package/src/index.ts +2 -0
- package/src/logger.module.ts +8 -0
- package/src/logger.service.ts +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @kanjijs/logger
|
|
2
|
+
|
|
3
|
+
A production-ready, context-aware application logger for Kanjijs Framework. Built on top of [Pino](https://github.com/pinojs/pino).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **JSON Structured Logging**: Ideal for production (Datadog, CloudWatch, etc).
|
|
8
|
+
- **Context Aware**: Automatically injects the current `requestId` into every log line using `@kanjijs/core`'s AsyncLocalStorage.
|
|
9
|
+
- **Environment Aware**: Pretty prints logs in development, uses strict JSON in production.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
1. Import `LoggerModule` in your App Module:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Module } from "@kanjijs/core";
|
|
17
|
+
import { LoggerModule } from "@kanjijs/logger";
|
|
18
|
+
|
|
19
|
+
@Module({
|
|
20
|
+
imports: [LoggerModule],
|
|
21
|
+
// ...
|
|
22
|
+
})
|
|
23
|
+
export class AppModule {}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
2. Inject `Logger` in your services:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { Injectable } from "@kanjijs/core";
|
|
30
|
+
import { Logger } from "@kanjijs/logger";
|
|
31
|
+
|
|
32
|
+
@Injectable()
|
|
33
|
+
export class UserService {
|
|
34
|
+
constructor(private logger: Logger) {}
|
|
35
|
+
|
|
36
|
+
createUser() {
|
|
37
|
+
this.logger.info("Creating user...");
|
|
38
|
+
// Output: {"level":30,"time":...,"msg":"Creating user...","requestId":"abc-123"}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kanjijs/logger",
|
|
3
|
+
"version": "0.2.0-beta.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "bun build ./src/index.ts --outdir ./dist --target bun"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@kanjijs/core": "workspace:*",
|
|
11
|
+
"pino": "^9.0.0"
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Injectable, getRequestId } from "@kanjijs/core";
|
|
2
|
+
import pino from "pino";
|
|
3
|
+
|
|
4
|
+
@Injectable()
|
|
5
|
+
export class Logger {
|
|
6
|
+
private pino: pino.Logger;
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
this.pino = pino({
|
|
10
|
+
level: process.env.LOG_LEVEL || "info",
|
|
11
|
+
transport: process.env.NODE_ENV === "development" ? {
|
|
12
|
+
target: "pino-pretty",
|
|
13
|
+
options: { colorize: true }
|
|
14
|
+
} : undefined,
|
|
15
|
+
base: undefined // Remove hostname/pid to be cleaner
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private getMixin() {
|
|
20
|
+
const requestId = getRequestId();
|
|
21
|
+
return requestId ? { requestId } : {};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
info(msg: string, ...args: any[]) {
|
|
25
|
+
this.pino.info(this.getMixin(), msg, ...args);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
error(msg: string, ...args: any[]) {
|
|
29
|
+
this.pino.error(this.getMixin(), msg, ...args);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
warn(msg: string, ...args: any[]) {
|
|
33
|
+
this.pino.warn(this.getMixin(), msg, ...args);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
debug(msg: string, ...args: any[]) {
|
|
37
|
+
this.pino.debug(this.getMixin(), msg, ...args);
|
|
38
|
+
}
|
|
39
|
+
}
|