Skip to content

Instantly share code, notes, and snippets.

@vduseev
Created December 19, 2022 20:47
Show Gist options
  • Select an option

  • Save vduseev/79adb305bd601fbf60ef897b949e6036 to your computer and use it in GitHub Desktop.

Select an option

Save vduseev/79adb305bd601fbf60ef897b949e6036 to your computer and use it in GitHub Desktop.
JSON logging configuration for JavaScript using log4js (python compatible)
/**
* This configuration uses log4js and outputs logs to stdout
* in JSON format. The names of the fields are compatible with
* python-json-logger.
*
* The configuration was developed to the purposes of logging
* Express JS logs running inside docker and collected through
* FluentD.
*
* Reported fields:
* name: my.logger
* level: INFO
* message: ekrjgnwle
* timestamp: %Y-%m-%dT%H:%M:%S.%N%z
*/
const log4js = require("log4js");
log4js.configure({
appenders: {
stdout: {
type: "stdout",
layout: {
type: "pattern",
//pattern: `{"timestamp": "%d{yyyy-MM-ddThh:mm:ss.SSSO}", "name": "%c", "level": "%p", "message": "%m{0}"}`,
pattern: `{"timestamp": "%d{ISO8601_WITH_TZ_OFFSET}", "name": "%c", "level": "%p", "message": "%m{0}"}`,
},
},
},
categories: {
default: { appenders: ["stdout"], level: "info" },
},
});
exports.logger = log4js.getLogger("server");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment