Skip to content

Logging Configuration

Nishant Aanjaney Jalan edited this page Mar 24, 2024 · 6 revisions

Creating the simplest logger

Minimally, there are 3 steps to create your first logger.

  1. Create a Logestic instance.
  2. Define the parameters you wish to use.
  3. Format the string given the parameters [^1].

Putting the 3 steps together, you get:

import { Logestic } from 'logestic';

let logger = new Logestic()             // Step 1
logger = logger.use(['method', 'path']) // Step 2
const plugin = logger.format({          // Step 3
  onSuccess({ method, path }) {
    return `${method} ${path} was called and handled without server error.`;
  },
  onFailure({ request, error, code }) {
    return `Oops, ${error} was thrown with code: ${code}`;
  }
});

// or
const plugin = new Logestic()
  .use(['method', 'path']) 
  .format({
    onSuccess({ method, path }) {
      return `${method} ${path} was called and handled without server error.`;
    },
    onFailure({ request, error, code }) {
      return `Oops, ${error} was thrown with code: ${code}`;
    }
  });

plugin is now an instance of Elysia with a logestic object decorated in Elysia's context for explicit logging. This is great when you want consistency in your log messages[^2].

const app = new Elysia()
  .use(plugin)
  .get('/', ({ logestic }) => {
    logestic.debug('Why is it not working?');
    return 'you didn't call listen(3000)';
  });

[^1]: More on formatting here.

[^2]: HttpLogging are logs recorded on every request. ExplicitLogging are logs that you specify inside a handler. Read more here

Clone this wiki locally