generated from stegripe/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(packages): add @stegripe/pino-logger
- Loading branch information
Showing
6 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<div align="center"> | ||
|
||
<img src="https://cdn.stegripe.org/images/icon.png" alt="Stegripe Logo" width="256" height="256" /> | ||
|
||
# @stegripe/pino-logger | ||
|
||
**Plugin for [**`@sapphire/framework`**](https://github.com/sapphire/framework) used to handle logging with Pino.** | ||
|
||
[![GitHub](https://img.shields.io/github/license/stegripe/packages)](https://github.com/stegripe/sapphire-plugins/blob/main/LICENSE.md) | ||
|
||
</div> | ||
|
||
## Description | ||
|
||
This plugin is used to create custom loggers with Pino. | ||
|
||
## Features | ||
|
||
- Easy to use | ||
|
||
## Installation | ||
|
||
`@stegripe/pino-logger` depends on the following packages. Be sure to install these along with this package! | ||
|
||
- [`pino`](https://npmjs.com/package/pino) | ||
- [`discord.js`](https://npmjs.com/package/discord.js) | ||
- [`@sapphire/framework`](https://npmjs.com/package/@sapphire/framework) | ||
- [`@sapphire/utilities`](https://npmjs.com/package/@sapphire/utilities) | ||
|
||
You can use the following command to install this package, or replace `pnpm install` with your package manager of choice. | ||
|
||
```sh | ||
pnpm install @stegripe/pino-logger | ||
``` | ||
|
||
## Usage | ||
|
||
```ts | ||
import { SapphireClient, SapphireClientOptions } from "@sapphire/framework"; | ||
import { PinoLogger } from "@stegripe/pino-logger"; | ||
|
||
export class MyClient extends SapphireClient { | ||
public constructor(clientOptions: SapphireClientOptions) { | ||
super({ | ||
...clientOptions, | ||
logger: { | ||
instance: new PinoLogger({ | ||
name: "MyClient", | ||
timestamp: true, | ||
level: ISDEV ? "debug" : "info", | ||
formatters: { | ||
bindings: () => ({ pid: `MyClient@${process.pid}` }) | ||
} | ||
}) | ||
} | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
## With Custom Formatters | ||
|
||
```ts | ||
import { SapphireClient, SapphireClientOptions } from "@sapphire/framework"; | ||
import { PinoLogger } from "@stegripe/pino-logger"; | ||
|
||
export class MyClient extends SapphireClient { | ||
public constructor(clientOptions: SapphireClientOptions) { | ||
super({ | ||
...clientOptions, | ||
logger: { | ||
instance: new PinoLogger({ | ||
name: "MyClient", | ||
timestamp: true, | ||
level: ISDEV ? "debug" : "info", | ||
formatters: { | ||
bindings: () => ({ pid: `MyClient@${process.pid}` }) | ||
}, | ||
transport: { | ||
targets: [ | ||
{ | ||
target: "pino-pretty", | ||
level: ISDEV ? "debug" : "info", | ||
options: { translateTime: "SYS:yyyy-mm-dd HH:MM:ss" } | ||
} | ||
] | ||
} | ||
}) | ||
} | ||
}); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"name": "@stegripe/pino-logger", | ||
"version": "1.0.0", | ||
"description": "A simple logger for Sapphire Framework using Pino.", | ||
"homepage": "https://github.com/stegripe/sapphire-plugins#readme", | ||
"bugs": { | ||
"url": "https://github.com/stegripe/sapphire-plugins/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/stegripe/sapphire-plugins.git" | ||
}, | ||
"license": "AGPL-3.0", | ||
"author": "Stegripe Development <[email protected]>", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
} | ||
}, | ||
"files": [ | ||
"dist", | ||
"package.json" | ||
], | ||
"scripts": { | ||
"build": "pnpm typecheck && tsup --config ../../tsup.config.json", | ||
"lint": "eslint src --cache --cache-file .eslintcache", | ||
"lint:fix": "eslint src --cache --cache-file .eslintcache --fix", | ||
"pretty": "prettier --check src/**/*.ts", | ||
"pretty:write": "prettier --write src/**/*.ts", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@sapphire/framework": "^5.2.1", | ||
"@sapphire/utilities": "^3.17.0", | ||
"discord.js": "^14.15.3", | ||
"pino": "^9.3.2", | ||
"tslib": "^2.6.3" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^9.7.0", | ||
"prettier": "^3.2.5", | ||
"tsup": "^8.2.2", | ||
"typescript": "^5.4.5" | ||
}, | ||
"engines": { | ||
"node": ">=20.9", | ||
"npm": ">=10" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./lib/PinoLogger"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { inspect } from "node:util"; | ||
import { Logger as BuiltinLogger, LogLevel } from "@sapphire/framework"; | ||
import { cast } from "@sapphire/utilities"; | ||
import type { Level, Logger, LoggerOptions } from "pino"; | ||
import { pino } from "pino"; | ||
|
||
export class PinoLogger extends BuiltinLogger { | ||
public readonly pino: Logger; | ||
|
||
public constructor(options: LoggerOptions) { | ||
super(options.levelVal ?? LogLevel.Info); | ||
this.pino = pino(options); | ||
} | ||
|
||
public override has(level: LogLevel): boolean { | ||
return this.pino.levelVal <= Number(level.toString()); | ||
} | ||
|
||
public override write(level: LogLevel, ...values: readonly unknown[]): void { | ||
if (level < this.level) return; | ||
|
||
const method = cast<Level>(this.pino.levels.labels[level]); | ||
const message = values | ||
.map(value => (typeof value === "string" ? value : inspect(value, { depth: 0 }))) | ||
.join(" "); | ||
|
||
this.pino[method](message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/tsconfig", | ||
"extends": "../../tsconfig.eslint.json", | ||
"compilerOptions": { | ||
"allowJs": true | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/tsconfig", | ||
"extends": "../../tsconfig.base.json", | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |