Code Monkey home page Code Monkey logo

elysia-logger's Introduction

@bogeychan/elysia-logger

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

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 http://${app.server!.hostname}:${app.server!.port}`);

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 for debugging tools

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

const myPlugin = () => (app: Elysia) => app.decorate('myProperty', 42);

// ...

app = app.use(myPlugin());

app
  .use(
    logger({
      /**
       * This function will be invoked for each `log`-method called with `context`
       * where you can pass additional properties that need to be logged
       */
      customProps(ctx: InferContext<typeof app>) {
        return {
          params: ctx.params,
          query: ctx.query,
          myProperty: ctx.myProperty
        };
      }
    })
  )
  .get('/', (ctx) => {
    ctx.log.info(ctx, 'Context');

    return 'with-context';
  });

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 { message: '1234', name: 'MyError' };
  });

Automatic onResponse 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

elysia-logger's People

Contributors

bogeychan avatar loganarnett avatar mmadson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.