Skip to content

Commit

Permalink
feat(packages): add @stegripe/pino-logger
Browse files Browse the repository at this point in the history
  • Loading branch information
noxzym committed Jul 31, 2024
1 parent 143db80 commit 722791e
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 0 deletions.
93 changes: 93 additions & 0 deletions packages/pino-logger/README.md
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" }
}
]
}
})
}
});
}
}
```
57 changes: 57 additions & 0 deletions packages/pino-logger/package.json
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"
}
}
1 change: 1 addition & 0 deletions packages/pino-logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./lib/PinoLogger";
29 changes: 29 additions & 0 deletions packages/pino-logger/src/lib/PinoLogger.ts
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);
}
}
9 changes: 9 additions & 0 deletions packages/pino-logger/tsconfig.eslint.json
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"]
}
6 changes: 6 additions & 0 deletions packages/pino-logger/tsconfig.json
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"]
}

0 comments on commit 722791e

Please sign in to comment.