Coder Social home page Coder Social logo

skog's Introduction

Add context to your Node.js logs

Forest photo by Gustav Gullstrand on Unsplash

Getting started

Skog is a opinionated logging library built on top of Pino.

npm install skog
import log, { initializeLogger } from "skog";

initializeLogger();
setFields({ app: "my-app" });
log.info("Hello world");

If your app uses Express, use skogMiddleware to add req_id to every log line.

import log, { initializeLogger, skogMiddleware, setFields } from "skog";
import express from "express";

const app = express();

const app = express();
app.use(skogMiddleware);
app.get("/", () => {
  // This will log "req_id" automatically! You don't need to do anything else!
  log.info("Logging a message");
});

initializeLogger();
setFields({ app: "demo" });
app.listen(3000, () => {
  log.info("Starting server");
});

Recipes

skog exports more functions so you can create your own middleware. For example, if you want to add your own fields or if you want to create middleware for other frameworks.

Add other fields

Example: add a "session_id" field in your logs

import { runWithSkog } from "skog";
import { nanoid } from "nanoid";

function myMiddleware(req, res, next) {
  // Assuming that "req.session.id" exists...
  runWithSkog({ req_id: nanoid(), session_id: req.session.id.slice(-3) }, next);
}

Middleware for other frameworks

As you can see from the example above, runWithSkog accepts two arguments: an object with fields and a function. runWithSkog will return the same thing as returned by the function.

So, you can create a middleware for Next.js:

import { NextResponse } from "next/server";
import { nanoid } from "nanoid";

function middleware(req: NextRequest, ev: NextFetchEvent) {
  return runWithSkogContext(
    {
      session_id: req.cookies["session_id"]?.slice(-3),
      req_id: nanoid(),
    },
    NextResponse.next
  );
}

skog's People

Contributors

exacs avatar dependabot[bot] avatar jhsware avatar

Watchers

Mikko Puustinen avatar James Cloos avatar  avatar Johan Saldes avatar Krzysztof Starzecki avatar  avatar

skog's Issues

Potentially silently ignoring log lines

I noticed two things in these code lines

if (typeof arg1 === "string") {
fn(arg1);
} else if (typeof arg2 === "undefined" || typeof arg2 === "string") {
fn(arg1, arg2);
}

I see two different problems with this code:

  1. There is no "fallback else" statement. If this function is called with something else then (string, undefined) or (any, undefined|string) it will be silently ignored. Silently ignoring log lines shouldn't be possible IMO, I would much prefer if it threw an error if this is unexpected usage
  2. The code is a bit confusing: if arg2 is undefined, the function call will be fn(arg1, undefined), which is exactly the same as fn(arg1). And if these two calls are identical, what's the purpose of these two "if" blocks? Is this on purpose?

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.