LogTape 2.0.0: Dynamic logging and external configuration #133
dahlia
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
LogTape is a logging library for JavaScript and TypeScript that works across Deno, Node.js, Bun, and browsers. It's designed around structured logging with zero dependencies and flexible configuration—making it a solid foundation for both applications and libraries.
Version 2.0.0 marks a major release, introducing features that address some long-standing requests: dynamic context values, external configuration from JSON/YAML files, improved error handling, and async lazy evaluation. Here's what's new.
Dynamic context with
lazy()LogTape's
with()method lets you attach context that gets included with every log from that logger. The problem is thatwith()captures values at the moment it's called—and child loggers inherit those captured values, not references to the original variables.Consider this common pattern in SPAs:
The new
lazy()function solves this by deferring evaluation until logging time:Child loggers inherit the
lazy()wrapper itself, not its resolved value. Every time a log is written, the callback runs and captures the current state.lazy()is also useful when you need to capture state at logging time:This records the actual memory usage at the moment each log is written.
See the lazy evaluation documentation for more details.
Configuration from JSON, YAML, or TOML
Until now, LogTape configuration required TypeScript code. The new
@logtape/configpackage changes this by letting you load configuration from plain objects—which you can parse from JSON, YAML, TOML, or any other format.The configuration schema uses a module reference syntax for specifying sinks and formatters:
{ "sinks": { "console": { "type": "#console()", "formatter": { "type": "#ansiColor()", "timestamp": "date-time-tz" } }, "file": { "type": "@logtape/file#getFileSink()", "path": "/var/log/app.log", "formatter": "#jsonLines()" } }, "loggers": [ { "category": ["myapp"], "sinks": ["console", "file"], "lowestLevel": "info" } ] }Built-in shorthands like
#console(),#ansiColor(), and#jsonLines()map to LogTape's core exports. You can also reference external packages directly with@logtape/file#getFileSink()or define custom shorthands.Environment variable expansion is supported through the
expandEnvVars()utility:{ "sinks": { "file": { "type": "@logtape/file#getFileSink()", "path": "${LOG_PATH:/var/log/app.log}" } } }For graceful error handling in production, set
onInvalidConfig: "warn"to apply valid parts and log warnings rather than throwing errors.Better error logging
Logging errors used to require wrapping them in properties:
Now you can pass
Errorobjects directly toerror(),warn(), andfatal():The default message template is
{error.message}, and the full error (including stack trace and cause chain) is available in properties. The JSON Lines formatter now properly serializesErrorobjects, preservingname,message,stack,cause, anderrors(forAggregateError).Async lazy evaluation
For operations that require
await, logging methods now accept async callbacks:The async function only executes if debug logging is enabled. If disabled, the returned Promise resolves immediately without calling the function.
Checking if a level is enabled
When you need to conditionally execute multiple log statements or expensive setup work, use the new
isEnabledFor()method:Time-based log rotation
The
@logtape/filepackage now supports time-based rotation alongside the existing size-based rotation:Files are automatically named with date patterns: 2025-01-15.log (daily), 2025-01-15-09.log (hourly), or 2025-W03.log (weekly).
New integrations
Elysia framework
The
@logtape/elysiapackage provides HTTP request logging for Elysia applications:Like the other framework integrations (Express, Fastify, Hono, Koa), it supports Morgan-compatible format presets, custom format functions, request filtering, and configurable log levels.
log4js adaptor
The
@logtape/adaptor-log4jspackage allows LogTape to forward logs to existing log4js infrastructure:This is useful when adopting LogTape-enabled libraries in applications already using log4js.
OpenTelemetry improvements
The
@logtape/otelpackage received several updates:Simplified attributes: Property keys are no longer prefixed with
attributes., aligning with OpenTelemetry conventions. Existing queries referencingattributes.methodshould be updated to justmethod.Error handling: The new
exceptionAttributesoption controls howErrorobjects are converted. The default"semconv"follows OpenTelemetry semantic conventions (exception.type,exception.message,exception.stacktrace). Set"raw"for the previous JSON serialization behavior.Type preservation: Numbers and booleans are now preserved as their original types instead of being converted to strings.
Fingers crossed sink improvements
The
bufferLeveloption lets you separate which levels get buffered from which levels trigger the buffer flush:Logs at
infoorwarnpass through without buffering, whiledebugandtracelogs are buffered until an error triggers the flush.Windows Event Log improvements
The
@logtape/windows-eventlogpackage now usesNELOG_OEM_Code(3299) from netmsg.dll for proper message formatting in Windows Event Viewer. A newformatteroption allows custom text formatting.Browser compatibility
The
pagehideevent replaces the deprecatedunloadevent for automatic disposal in browser environments. This change improves compatibility with browser back/forward cache (bfcache) and mobile browsers.Formatters
Text and JSON Lines formatters now support the
lineEndingoption for Windows compatibility ("crlf"). The ANSI color formatter gains"none"and"disabled"options for thetimestampfield.Breaking changes
@logtape/otel
Property keys no longer include the
attributes.prefix. If you have queries or dashboards usingattributes.method, update them tomethod.Error objects now follow OpenTelemetry semantic conventions by default. Set
exceptionAttributes: "raw"for the previous behavior.@logtape/logtape
logger.warn(new Error(...))andlogger.fatal(new Error(...))now produce slightly different output since these methods acceptErrorobjects directly. Previously this was interpreted as the{*}shorthand.Upgrading
Update your packages:
npm update @logtape/logtape # or deno add jsr:@logtape/logtape@^2.0.0For the new packages:
If you're upgrading from 1.x, check the breaking changes section above. The
lazy()andconfigureFromObject()features are additive—existing configurations continue working.See the full changelog for complete details.
All reactions