0.0.17 โ€ข Published 2 months ago

@otherguy/elysia-logging v0.0.17

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@otherguy/elysia-logging

An advanced logging plugin designed for Elysia.js, prioritizing structured logging tailored for production environments.

npm version npm downloads Snyk Monitored GitHub Workflow Status (with event) GitHub License Code Climate Coverage CodeFactor Grade Sonar CodeSmells


๐ŸŒˆ Features

  • 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

# For bun
bun add @otherguy/elysia-logging

# For npm
npm install @otherguy/elysia-logging

# For yarn
yarn add @otherguy/elysia-logging

๐Ÿ“š 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.

import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";

// Create Elysia app
const app = new Elysia()
  .use(ElysiaLogging())
  .get("/", () => {
    return new Response("Welcome to Bun!");
  })
  .listen(3000);

console.log(`Running at http://${app.server?.hostname}:${app.server?.port}`);

Using the default settings, the plugin will log the following for each request. Since the console module is pretty printing the JSON, the output in the console will be formatted slightly differently.

{
  "message": "GET / completed with status 200 in 363.3ยตs",
  "request": {
    "ip": "127.0.0.1",
    "method": "GET",
    "url": {
      "path": "/",
      "params": {}
    }
  },
  "response": {
    "status_code": 200,
    "time": 363250
  }
}

Custom Logger

Since the console is very limited, you may want to use a custom logger. The recommended logger is pino but you can use any logger that can implement the Logger interface.

See the examples directory for implementation examples.

๐Ÿชœ Examples

Even more examples are provided in the examples directory.

  • Basic - A basic example of using the plugin with the default settings
  • JSON - A basic example of logging in JSON
  • Custom Function - An example of using a function as custom logging formatter
  • On Error - An example of logging errors in addition to access logging
  • Pino - An example of using the Pino logger
  • Pino Pretty - An example of using the Pino logger with pino-pretty (not recommended for production)
  • TSLog - An example of using the TSLog logger
  • Bunyan - An example of using the Bunyan logger
  • Winston - An example of using the Winston logger

๐Ÿ“œ To Do

  • Add logger format classes
  • Add whitelist for request parameters

โš–๏ธ License

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

๐Ÿšง Contributing

Bug reports, feature requests and Pull Requests are more than welcome on GitHub at otherguy/elysia-logging! Please remember to add test coverage for your code if you are contributing.

โ™ฅ๏ธ Acknowledgements

0.0.17

2 months ago

0.0.16

6 months ago

0.0.15

6 months ago

0.0.14

6 months ago

0.0.13

6 months ago

0.0.12

6 months ago

0.0.11

6 months ago

0.0.10

6 months ago

0.0.9

6 months ago

0.0.8

6 months ago

0.0.7

7 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago