https://github.com/kth/skog
Logging with context for Node.js
https://github.com/kth/skog
bunyan kth-e-larande logging nodejs
Last synced: about 1 month ago
JSON representation
Logging with context for Node.js
- Host: GitHub
- URL: https://github.com/kth/skog
- Owner: KTH
- Created: 2019-07-23T13:20:42.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2025-07-29T09:41:10.000Z (11 months ago)
- Last Synced: 2025-10-19T06:35:31.662Z (8 months ago)
- Topics: bunyan, kth-e-larande, logging, nodejs
- Language: TypeScript
- Homepage:
- Size: 2.13 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Getting started
Skog is a opinionated logging library built on top of Pino.
```
npm install skog
```
```ts
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.
```ts
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
```ts
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:
```ts
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
);
}
```