Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 1d86c35

Browse files
authored
feat: add pino-logger package (#14)
1 parent 54a8ab3 commit 1d86c35

File tree

17 files changed

+1029
-1
lines changed

17 files changed

+1029
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@sapphire/decorators": "^4.3.4",
6464
"@sapphire/framework": "next",
6565
"@sapphire/pieces": "^3.3.1",
66-
"discord.js": "^13.6.0"
66+
"discord.js": "^13.6.0",
67+
"pino-pretty": "^7.6.1"
6768
}
6869
}

packages/pino-logger/.npmignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Ignore a blackhole and the folder for development
2+
node_modules/
3+
.vs/
4+
.idea/
5+
*.iml
6+
coverage/
7+
docs/
8+
9+
# Yarn files
10+
.yarn/install-state.gz
11+
.yarn/build-state.yml
12+
13+
14+
# Ignore JavaScript files
15+
**/*.tsbuildinfo
16+
!jest.config.ts
17+
18+
# Ignore heapsnapshot and log files
19+
*.heapsnapshot
20+
*.log
21+
22+
# Ignore package locks
23+
package-lock.json
24+
25+
# Ignore the GH cli downloaded by workflows
26+
gh
27+
28+
# Ignore the "wiki" folder so we can checkout the wiki inside the same folder
29+
wiki/
30+
31+
# Additional ignore
32+
test.js
33+
.github
34+
.gitignore
35+
tsconfig.json
36+
src/
37+
.eslintrc
38+
.swc
39+
.gitattributes
40+
build.config.ts
41+
tsconfig.eslint.json
42+
.turbo

packages/pino-logger/LICENSE

Lines changed: 672 additions & 0 deletions
Large diffs are not rendered by default.

packages/pino-logger/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# @zhycorporg/pino-logger
2+
> Plugin for @sapphire/framework to have pino logger
3+
4+
# Instalation
5+
```
6+
npm install @sapphire/framework discord.js @zhycorporg/pino-logger
7+
```
8+
9+
# Example
10+
```ts
11+
import { SapphireClient, SapphireClientOptions } from "@sapphire/framework";
12+
import { Intents } from "discord.js";
13+
import { join } from "node:path";
14+
import "@zhycorporg/pino-logger/register";
15+
16+
class TestClient extends SapphireClient {
17+
public constructor(clientOptions?: SapphireClientOptions) {
18+
super({
19+
fetchPrefix: () => "t!",
20+
intents: [
21+
Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES
22+
],
23+
logger: {
24+
pino: {
25+
timestamp: true
26+
}
27+
},
28+
loadMessageCommandListeners: true,
29+
baseUserDirectory: join(__dirname),
30+
...clientOptions
31+
});
32+
}
33+
}
34+
```
35+
36+
# Example with pino-pretty
37+
```ts
38+
import { SapphireClient, SapphireClientOptions } from "@sapphire/framework";
39+
import { Intents } from "discord.js";
40+
import { join } from "node:path";
41+
import "@zhycorporg/pino-logger/register";
42+
43+
class TestClient extends SapphireClient {
44+
public constructor(clientOptions?: SapphireClientOptions) {
45+
super({
46+
fetchPrefix: () => "t!",
47+
intents: [
48+
Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES
49+
],
50+
logger: {
51+
pino: {
52+
timestamp: true,
53+
transport: {
54+
targets: [
55+
{ target: "pino-pretty", level: "info", options: { translateTime: "SYS:yyyy-mm-dd HH:MM:ss.l o" } }
56+
]
57+
}
58+
}
59+
},
60+
loadMessageCommandListeners: true,
61+
baseUserDirectory: join(__dirname),
62+
...clientOptions
63+
});
64+
}
65+
}
66+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Config } from "@jest/types";
2+
3+
export default async (): Promise<Config.InitialOptions> => ({
4+
displayName: "unit test",
5+
preset: "ts-jest",
6+
testEnvironment: "node",
7+
testRunner: "jest-circus/runner",
8+
testMatch: ["<rootDir>/tests/**/*.test.ts"],
9+
globals: {
10+
"ts-jest": {
11+
tsconfig: "<rootDir>/tests/tsconfig.json"
12+
}
13+
}
14+
});

packages/pino-logger/package.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "@zhycorporg/pino-logger",
3+
"description": "Plugin for @sapphire/framework to have pino logger",
4+
"author": "Zhycorp <[email protected]>",
5+
"version": "1.0.0",
6+
"main": "dist/index.js",
7+
"module": "dist/index.mjs",
8+
"exports": {
9+
".": {
10+
"import": "./dist/index.mjs",
11+
"require": "./dist/index.js"
12+
},
13+
"./register": {
14+
"import": "./dist/register.mjs",
15+
"require": "./dist/register.js",
16+
"types": "./dist/register.d.ts"
17+
}
18+
},
19+
"files": [
20+
"dist/**/*.js*",
21+
"dist/**/*.mjs*",
22+
"dist/**/*.d*"
23+
],
24+
"license": "AGPL-3.0",
25+
"homepage": "https://github.com/zhycorp/sapphire-plugins/tree/main/packages/pino-logger",
26+
"repository": {
27+
"type": "git",
28+
"url": "git+https://github.com/zhycorp/sapphire-plugins.git",
29+
"directory": "packages/command-context"
30+
},
31+
"bugs": {
32+
"url": "https://github.com/zhycorp/sapphire-plugins/issues"
33+
},
34+
"readme": "https://github.com/zhycorp/sapphire-plugins/blob/main/README.md",
35+
"engines": {
36+
"node": ">=16.6.0",
37+
"npm": ">=7.0.0"
38+
},
39+
"eslintConfig": {
40+
"extends": "@hazmi35/eslint-config/typescript",
41+
"ignorePatterns": [
42+
"dist/*",
43+
"jest.config.ts",
44+
"rollup.bundle.ts"
45+
],
46+
"parserOptions": {
47+
"project": "./tsconfig.eslint.json"
48+
}
49+
},
50+
"scripts": {
51+
"build": "rimraf dist && tsc -b src && rollup -c rollup.bundle.ts && npm run esm:default",
52+
"esm:default": "gen-esm-wrapper dist/index.js dist/index.mjs",
53+
"lint": "eslint src tests --ext ts",
54+
"lint:fix": "eslint src tests --fix --ext ts",
55+
"format": "prettier --write {src,tests}/**/*.ts",
56+
"prepareForRelease": "npm run build && npm run format && npm run lint && npm run lint:fix",
57+
"test": "jest",
58+
"release": "npm publish --access public"
59+
},
60+
"devDependencies": {
61+
"gen-esm-wrapper": "^1.1.3",
62+
"typescript": "4.6.4"
63+
},
64+
"dependencies": {
65+
"pino": "^7.11.0"
66+
}
67+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import versionInjector from "rollup-plugin-version-injector";
2+
3+
export default {
4+
input: "dist/index.js",
5+
output: [
6+
{
7+
file: "./dist/index.js",
8+
format: "cjs"
9+
}
10+
],
11+
plugins: [versionInjector()]
12+
};

packages/pino-logger/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./lib/PinoLogger";
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable class-methods-use-this */
2+
/* eslint-disable @typescript-eslint/consistent-type-imports */
3+
import type { ILogger } from "@sapphire/framework";
4+
import pino, { LoggerOptions, Logger } from "pino";
5+
6+
export class PinoLogger implements ILogger {
7+
protected pino: Logger;
8+
public constructor(options: LoggerOptions) {
9+
this.pino = pino(options);
10+
}
11+
12+
public trace(...values: readonly [unknown]): void {
13+
this.pino.trace(...values);
14+
}
15+
16+
public debug(...values: readonly [unknown]): void {
17+
this.pino.debug(...values);
18+
}
19+
20+
public info(...values: readonly [unknown]): void {
21+
this.pino.info(...values);
22+
}
23+
24+
public warn(...values: readonly [unknown]): void {
25+
this.pino.warn(...values);
26+
}
27+
28+
public error(...values: readonly [unknown]): void {
29+
this.pino.error(...values);
30+
}
31+
32+
public fatal(...values: readonly [unknown]): void {
33+
this.pino.fatal(...values);
34+
}
35+
36+
public has(): boolean {
37+
return true;
38+
}
39+
40+
public write(): void {
41+
throw new Error("Method is not implemented.");
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable @typescript-eslint/no-empty-interface */
2+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
3+
/* eslint-disable @typescript-eslint/consistent-type-imports */
4+
import { Plugin, preGenericsInitialization, SapphireClient } from "@sapphire/framework";
5+
import type { ClientOptions } from "discord.js";
6+
import type { LoggerOptions } from "pino";
7+
import { PinoLogger } from "./lib/PinoLogger";
8+
9+
export class PinoLoggerPlugin extends Plugin {
10+
public static [preGenericsInitialization](this: SapphireClient, options: ClientOptions) {
11+
options.logger ??= { pino: { } };
12+
options.logger.instance = new PinoLogger(options.logger.pino);
13+
}
14+
}
15+
16+
declare module "@sapphire/framework" {
17+
export interface ClientLoggerOptions {
18+
pino: LoggerOptions;
19+
}
20+
}
21+
22+
SapphireClient.plugins.registerPreGenericsInitializationHook(PinoLoggerPlugin[preGenericsInitialization], "PinoLogger-PreGenericsInitialization");

0 commit comments

Comments
 (0)