gitmyhub

node-bunyan

JavaScript ★ 7.2k updated 2y ago

a simple and fast JSON logging module for node.js services

Bunyan is a Node.js logging library that writes every log entry as a single JSON line, making server logs machine-readable and filterable with its included command-line tool.

JavaScriptNode.jssetup: easycomplexity 2/5

Bunyan is a logging library for Node.js applications. Its central idea is that server logs should be structured data rather than free-form text, so every log entry it produces is a single line of JSON. Each line includes fields like the application name, process ID, hostname, timestamp, severity level, and the message you asked it to log. Because the output is machine-readable JSON, it is easy to search and filter with other tools.

The library has a small API. You create a logger by calling bunyan.createLogger with a name and optional settings, then call methods named after severity levels: trace, debug, info, warn, error, and fatal. Each call writes a JSON line to wherever you have configured the logger to write. You can also pass extra fields alongside the message, and those fields get folded into the JSON record. Error objects are handled specially so their stack traces are captured in a structured way.

Bunyan comes with a command-line tool, also called bunyan, that reads the raw JSON log output and displays it in a human-friendly, colorized format. The CLI can also filter records by level, time range, or any custom field, so you can search through logs without relying on grep over plain text.

The streams system controls where log output goes. You can write to a file, to stdout, to a rotating file (where old log files are moved aside automatically), or to any custom destination. Loggers can fan out to multiple streams at different severity levels, for example writing everything to a file but only warnings and above to a monitoring service.

Child loggers let you create a specialized copy of an existing logger that inherits its settings but adds extra fields automatically to every record it writes. The full README is longer than what was shown.

Where it fits