Writers
Logger writers are responsible for handling how and where log messages are output. In Hive Logger, writers are pluggable components that receive structured log data and determine its final destination and format. This allows you to easily customize logging behavior, such as printing logs to the console, writing them as JSON, storing them in memory for testing, or sending them to external systems.
By default, Hive Logger provides several built-in writers, but you can also implement your own to suit your application’s needs. The built-ins are:
MemoryLogWriter
Writes the logs to memory allowing you to access the logs. Mostly useful for testing.
Outputs:
ConsoleLogWriter (default)
The default log writer used by the Hive Logger. It outputs log messages to the console in a human-friendly, colorized format, making it easy to distinguish log levels and read structured attributes. Each log entry includes a timestamp, the log level (with color), the message, and any additional attributes (with colored keys), which are pretty-printed and formatted for clarity.
The writer works in both Node.js and browser-like environments, automatically disabling colors if
not supported. This makes ConsoleLogWriter ideal for all cases, providing clear and readable logs
out of the box.
Outputs:
Disabling Colors
You can disable colors in the console output by setting the NO_COLOR=1 environment variable. All
environments that need the logger to not color the output will automatically set this following the
NO_COLOR convention.
JSONLogWriter
Built-in log writer that outputs each log entry as a structured JSON object. When used, it prints logs to the console in JSON format, including all provided attributes, the log level, message, and a timestamp.
In the JSONLogWriter implementation, any attributes you provide with the keys msg, timestamp, or
level will be overwritten in the final log output. This is because the writer explicitly sets
these fields when constructing the log object. If you include these keys in your attributes, their
values will be replaced by the logger’s own values in the JSON output.
If the LOG_JSON_PRETTY=1 environment variable is provided, the output will be pretty-printed for
readability; otherwise, it is compact.
This writer’s format is ideal for machine parsing, log aggregation, or integrating with external logging systems, especially useful for production environments or when logs need to be consumed by other tools.
Outputs:
Or pretty printed:
Optional Writers
Hive Logger includes some writers for common loggers of the JavaScript ecosystem with optional peer dependencies.
LogTapeLogWriter
Use the LogTape logger library for writing Hive Logger’s logs.
@logtape/logtape is an optional peer dependency, so you must install it first.
PinoLogWriter (Node.js Only)
Use the Node.js pino logger library for writing Hive Logger’s
logs.
pino is an optional peer dependency, so you must install it first.
WinstonLogWriter (Node.js Only)
Use the Node.js winston logger library for writing Hive
Logger’s logs.
winston is an optional peer dependency, so you must install it first.
Custom Writers
You can implement custom log writers for the Hive Logger by creating a class that implements the
LogWriter interface. This interface requires a single write method, which receives the log
level, attributes, and message and an optional flush method allowing you to ensure all writer jobs
are completed when the logger is flushed.
Your writer can perform any action, such as sending logs to a file, external service, or custom destination.
Writers can be synchronous (returning void) or asynchronous (returning a Promise<void>). If your
writer performs asynchronous operations (like network requests or file writes), simply return a
promise from the write method.
Furthermore, you can optionally implement the flush method to ensure that all pending writes are
completed before the logger is disposed or flushed. This is particularly useful for asynchronous
writers that need to ensure all logs are written before the application exits or the logger is no
longer needed.
Example of HTTP Writer
Example of Daily File Log Writer (Node.js Only)
Here is an example of a custom log writer that writes logs to a daily log file. It will write to a file for each day in a given directory.
Flushing and Non-Blocking Logging
The logger does not block when you log asynchronously. Instead, it tracks all pending async writes
internally. When you call log.flush() it waits for all pending writes to finish, ensuring no logs
are lost on shutdown. During normal operation, logging remains fast and non-blocking, even if some
writers are async.
This design allows you to use async writers without impacting the performance of your application or blocking the main thread.
After all writes have been completed, the logger will call the optional flush method on the
writers, executing any custom finalization logic you may have implemented.
Explicit Resource Management
The Hive Logger also supports Explicit Resource Management. This allows you to ensure that all pending asynchronous log writes are properly flushed before your application exits or when the logger is no longer needed.
You can use the logger with await using (in environments that support it) to wait for all log
operations to complete. This is especially useful in serverless or short-lived environments where
you want to guarantee that no logs are lost due to unfinished asynchronous operations.
Handling Async Write Errors
The Logger handles write errors for asynchronous writers by tracking all write promises. When
await log.flush() is called (including during async disposal), it waits for all pending writes to
settle. If any writes fail (i.e., their promises reject), their errors are collected and after all
writes have settled, if there were any errors, an AggregateError is thrown containing all the
individual write errors.
Outputs: