0.1.4 • Published 9 months ago

@bogeychan/elysia-logger v0.1.4

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

@bogeychan/elysia-logger

A plugin for Elysia.js for logging using the pino library.

Migration Guide

Installation

bun add @bogeychan/elysia-logger

Usage

import { Elysia } from "elysia";
import { logger } from "@bogeychan/elysia-logger";

const app = new Elysia()
  .use(
    logger({
      level: "error",
    })
  )
  .get("/", (ctx) => {
    ctx.log.error(ctx, "Context");
    ctx.log.info(ctx.request, "Request"); // noop

    return "Hello World";
  })
  .listen(8080);

console.log(`Listening on ${app.server!.url}`);

Log to a file, or

import { fileLogger } from "@bogeychan/elysia-logger";

fileLogger({
  file: "./my.log",
});

Pipe the log entries into a stream

import { logger } from '@bogeychan/elysia-logger';

logger({
  stream: ... // default -> console output
});

Include additional request context info

import { logger, type InferContext } from "@bogeychan/elysia-logger";

const myPlugin = () => new Elysia().decorate("myProperty", 42);

// ...

class MyError extends Error {
  constructor(message: string, public myValue: string) {
    super(message);
  }
}

app = app.error("myError", MyError).use(myPlugin());

app
  .use(
    logger({
      /**
       * This function will be invoked for each `log`-method called
       * where you can pass additional properties that need to be logged
       */
      customProps(ctx: InferContext<typeof app>) {
        if (ctx.isError && ctx.code === "myError") {
          return {
            myValue: ctx.error.myValue,
          };
        }

        return {
          params: ctx.params,
          query: ctx.query,
          myProperty: ctx.myProperty,
        };
      },
    })
  )
  .get("/", (ctx) => {
    ctx.log.info(ctx, "Context");

    return "with-context";
  })
  .get("/error", () => {
    throw new MyError("whelp", "yay");
  });

Use the logger instance both Standalone and inside Context

import { createPinoLogger } from "@bogeychan/elysia-logger";

const log = createPinoLogger(/* ... */);

app
  .use(log.into(/* ... */))
  .onError((ctx) => {
    log.error(ctx, ctx.error.name);
    return "onError";
  })
  .get("/", (ctx) => {
    ctx.log.info(ctx, "Context");

    throw new Error("whelp");
  });

Automatic onAfterResponse & onError logging by default; based on pino-http

import { logger } from "@bogeychan/elysia-logger";

app
  .use(
    logger({
      autoLogging: true, // default
      autoLogging: false, // disabled
      autoLogging: {
        ignore(ctx) {
          return true; // ignore logging for requests based on condition
        },
      },
    })
  )
  .get("/", (ctx) => "autoLogging");

Checkout the examples folder on github for further use cases such as the integration of pino-pretty for readable console outputs.

License

MIT

0.1.4

9 months ago

0.1.3

9 months ago

0.0.22

1 year ago

0.0.23

12 months ago

0.1.0-rc.1

12 months ago

0.1.0-rc.2

12 months ago

0.1.0-rc.0

12 months ago

0.1.0

12 months ago

0.1.2

11 months ago

0.1.1

12 months ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.3

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago