Simple json layout module for log4js.
npm install log4js-layout-jsonOutput:
{"time":"2024-05-11T18:18:34.266Z","category":"default","level":"INFO","msg":"Initializing DB connection"} Adding context:
log.addContext("user", "john");{"time":"2024-05-11T18:19:34.266Z","category":"default","level":"INFO","msg":"Initializing DB connection","user":"john"} Set the layout type to json.
Each log object contains the following properties:
time- time in ISO 8601 formatcategory- specified when log4js is initializedmsg- if the log message is a string, otherwise omittedlevel- level in human readable format
import log4js from "log4js"; import { layout as jsonLayout } from "log4js-json-layout"; log4js.addLayout("json", jsonLayout);minimal:
log4js.configure({ appenders: { out: { type: "stdout", layout: { type: "json", includeFileName: true, includeFunctionName: true, }, }, }, categories: { default: { level: "debug", appenders: ["out"], }, }, });