Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
otherguy committed Nov 23, 2023
1 parent 2957d40 commit c8b3468
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 66 deletions.
78 changes: 25 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ An advanced logging plugin designed for [Elysia.js](https://elysiajs.com), prior

---

## Features
## 🌈 Features

## Installation
* **Structured Logging** - Log in JSON format for easy parsing and filtering
* **Customizable** - Customize the log level, formatter and logger
* **Production First** - Designed for production environments first

## 🚀 Installation

```bash
# For bun
Expand All @@ -32,7 +36,7 @@ npm install @otherguy/elysia-logging
yarn add @otherguy/elysia-logging
```

## Usage
## 📚 Usage

By default, the plugin will log to the console using the `console` module. The default formatter is `json` and the default log level is `info`.

Expand Down Expand Up @@ -73,66 +77,34 @@ Using the default settings, the plugin will log the following for each request.

### Custom Logger

Since the `console` is very limited, you may want to use a custom logger. The recommended logger is [pino](https://github.com/pinojs/pino) but you can use any logger that implements the `Logger` interface.
Since the `console` is very limited, you may want to use a custom logger. The recommended logger is [pino](https://github.com/pinojs/pino) but you can use any logger that can implement the `Logger` interface.

* [Pino](https://github.com/pinojs/pino)
* [Winston](https://github.com/winstonjs/winston)
See the [examples](examples) directory for implementation examples.

#### Pino
* [Pino](https://github.com/pinojs/pino) (see [Pino](examples/pino.ts) and [Pino Pretty](examples/pino-pretty.ts) examples)
* [Winston](https://github.com/winstonjs/winston) (see [Winston](examples/winston.ts) example)

```ts
import { Elysia } from "elysia";
import { ElysiaLogging, type Logger } from "@otherguy/elysia-logging";
import { pino } from "pino";
## 🪜 Examples

// Create Pino logger
const logger : Logger = pino({
level: Bun.env.LOG_LEVEL ?? "info",
});

const app = new Elysia()
.use(ElysiaLogging(logger, {
format: "json",
level: "info",
}))
.get("/", () => {
return new Response("Welcome to Bun!");
})
.listen(Bun.env.PORT ?? 3000);
A few examples are provided in the [`examples`](examples) directory.

logger.log(`Running at http://${app.server?.hostname}:${app.server?.port}`);
```
* [Basic](examples/basic.ts) - A basic example of using the plugin with the default settings
* [JSON](examples/json.ts) - A basic example of logging in JSON
* [Custom Function](examples/custom-function.ts) - An example of using a function as custom logging formatter
* [On Error](examples/on-error.ts) - An example of logging errors in addition to access logging
* [Pino](examples/pino.ts) - An example of using the [Pino](https://github.com/pinojs/pino) logger
* [Pino Pretty](examples/pino.ts) - An example of using the [Pino](https://github.com/pinojs/pino) logger with [pino-pretty](https://github.com/pinojs/pino-pretty) _(not recommended for production)_
* [Winston](examples/winston.ts) - An example of using the [Winston](https://github.com/winstonjs/winston) logger

#### Winston
## 📜 To Do

```ts
import { Elysia } from "elysia";
import { ElysiaLogging, type Logger } from "@otherguy/elysia-logging";
import { createLogger, transports, format } from "winston";

// Create Winston logger
const logger : Logger = createLogger({
level: Bun.env.LOG_LEVEL ?? "info",
format: format.json(),
transports: [new transports.Console()],
});

const app = new Elysia()
.use(ElysiaLogging(logger, {
format: "json",
level: "info",
}))
.get("/", () => {
return new Response("Welcome to Bun!");
})
.listen(Bun.env.PORT ?? 3000);

logger.log(`Running at http://${app.server?.hostname}:${app.server?.port}`);
```
* [ ] Add logger format classes
* [ ] Add whitelist for request parameters
* [ ] Add more logger examples (Bunyan, npmlog)

## ⚖️ License

This project is distributed under the [MIT](LICENSE.md) License, allowing for open source distribution and modification, subject to the terms outlined in the [LICENSE.md](LICENSE.md) file.
This project is distributed under the [MIT](LICENSE.md) license, allowing for open source distribution and modification, subject to the terms outlined in the [LICENSE.md](LICENSE.md) file.

## 🚧 Contributing

Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion examples/basic.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";

// Use console for logging
const logger = console;
const logger : Logger = console;

// Create ElysiaLogging instance
const elysiaLogging = ElysiaLogging(logger, {
// Log in short format
format: "short",
});

Expand Down
4 changes: 2 additions & 2 deletions examples/custom-function.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";

// Use console for logging
const logger = console;
const logger : Logger= console;

// Create ElysiaLogging instance
const elysiaLogging = ElysiaLogging(logger, {
Expand All @@ -24,4 +25,3 @@ const app = new Elysia()
});

logger.info(`🦊 Running at http://${app.server?.hostname}:${app.server?.port}`);

4 changes: 2 additions & 2 deletions examples/json.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";

// Use console for logging
const logger = console;
const logger : Logger = console;

// Create ElysiaLogging instance
const elysiaLogging = ElysiaLogging(logger, {
Expand All @@ -24,4 +25,3 @@ const app = new Elysia()
});

logger.info(`🦊 Running at http://${app.server?.hostname}:${app.server?.port}`);

4 changes: 2 additions & 2 deletions examples/on-error.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";

// Use console for logging
const logger = console;
const logger : Logger = console;

// Create ElysiaLogging instance
const elysiaLogging = ElysiaLogging(logger, {
Expand All @@ -28,4 +29,3 @@ const app = new Elysia()
});

logger.info(`🦊 Running at http://${app.server?.hostname}:${app.server?.port}`);

4 changes: 2 additions & 2 deletions examples/pino-pretty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { pino, Logger } from "pino";
import { type Logger } from "../src/types";
import { pino } from "pino";

// Define Pino logger
const logger : Logger = pino({
Expand Down Expand Up @@ -70,4 +71,3 @@ const app = new Elysia()
});

logger.info(`🦊 Running at http://${app.server?.hostname}:${app.server?.port}`);

3 changes: 2 additions & 1 deletion examples/pino.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { pino, Logger } from "pino";
import { type Logger } from "../src/types";
import { pino } from "pino";

// Define Pino logger
const logger : Logger = pino({
Expand Down
41 changes: 41 additions & 0 deletions examples/winston.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { createLogger, transports, format } from "winston";

// Define Winston logger
const logger : Logger = createLogger({
// Use the LOG_LEVEL environment variable, or default to "info"
level: Bun.env.LOG_LEVEL ?? "info",

// Use JSON format
format: format.json(),

// Log to the console
transports: [new transports.Console()],
});

const elysiaLogging = ElysiaLogging(logger, {
// Use the pino "http" custom level defined above
level: "http",

// Access logs in JSON format
format: "json",
})

//
const app = new Elysia()
.use(elysiaLogging)
.get("/", () => {
if (Math.random() < 0.75) {
return new Response("Welcome to Bun!");
}
throw new Error("Whoops!");
})
.listen({
port: Bun.env.PORT ?? 3000,
maxRequestBodySize: Number.MAX_SAFE_INTEGER,
});

logger.info(`🦊 Running at http://${app.server?.hostname}:${app.server?.port}`);

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"jest-sonar": "^0.2.16",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.3.2"
"typescript": "^5.3.2",
"winston": "^3.11.0"
},
"homepage": "https://github.com/otherguy/elysia-logging",
"bugs": "https://github.com/otherguy/elysia-logging/issues",
Expand Down
2 changes: 1 addition & 1 deletion spec/elysiaLogging.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { Logger } from "../src/types";
import { type Logger } from "../src/types";

interface MockLogger extends Logger {
debug: <T extends unknown[]>(...args: T) => void;
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
},

/* Include */
"include": ["src/**/*"],
"include": [
"src/**/*",
"examples/**/*"
],
}

0 comments on commit c8b3468

Please sign in to comment.