Repository navigation
This major release makes the code do what the README says, and documents what it didn't cover. Internal helpers are no longer exported, more console methods are patched, and several output details change. Read the upgrade notes if you import anything besides the documented API, or if you search, alert on, or snapshot-test log output.
Upgrade notes
| Change | Before | After |
|---|---|---|
Exports FormatErrorObject, loadEnvConfig, logUsingConsoleJson, overrideStdOut, restoreStdOut |
exported | removed. They were internal and test helpers. The public API is LoggerAdaptToConsole, LoggerRestoreConsole, GetLogLevel, SetLogLevel, NativeConsoleLog, ErrorWithContext, LOG_LEVEL and the option types |
| End of each log entry on Node.js | {...}\n\n (a blank line after every entry) |
{...}\n |
| End of each log entry in browsers | the message had a trailing \n |
no trailing newline |
noNewLineCharactersExceptStack / CONSOLE_LOG_JSON_NO_NEW_LINE_CHARACTERS_EXCEPT_STACK |
removed the blank line | deprecated, no effect (the blank line is gone for everyone). Still accepted |
noStackForNonError on error logs |
@logCallStack removed from every log |
removed only from non-error logs. Error logs and console.trace keep it |
customOptions with contextKey |
nested under the context key: "context":{"service":"api",...} |
at the top level: "service":"api","context":{...} |
{ level } override together with an Error, e.g. console.log({ level: 'warn' }, err) |
logged at error |
logged at warn, with errCallStack. Level filtering uses the override too |
An Error logged with console.log/console.warn while logLevel is above that method's level |
filtered by the method's level, so it could be dropped even though it was written as error |
filtered as error |
An Error with its own level property |
the property could be taken as a level override | it is data; the log is error |
console.trace(...) on Node.js |
level: error, stack inside message ("Trace: msg\n at ...") |
level: info, message msg, stack in @logCallStack |
console.table(data) on Node.js |
an ASCII-box string in message |
message <console-table>, data in @table |
console.dir(obj) on Node.js |
printed with util.inspect, not JSON |
JSON, like console.log(obj) |
console.assert, count, countReset, time, timeLog, timeEnd, group, groupCollapsed, dirxml |
Node.js: JSON with plain-text messages; browsers: native output | JSON everywhere, with @label, @count, @durationMs. assert failures are error. groupEnd does nothing |
An Error with an empty message, e.g. console.error(new Error('')) |
no message field when metadata was on |
"message":"<no-error-message-was-passed-to-console-log>" |
onLog callback |
2 arguments; onLogTimeout had no effect |
3rd argument { signal }, an AbortSignal that aborts after onLogTimeout ms. Two-argument callbacks keep working |
transformOutput that changes its argument and then throws |
the changes leaked into the "fallback" output | the callback gets a copy; the fallback is really unmodified |
Features
- More console methods emit JSON.
trace,table,dir,dirxml,assert,count,countReset,time,timeLog,timeEnd,groupandgroupCollapsedare patched explicitly, with the same output in Node.js and browsers.LoggerRestoreConsole()restores them all. onLoggets an abort signal.onLog(jsonString, parsedObject, { signal }): passsignaltofetch(or anything else that takes one) and the work is cancelled afteronLogTimeout.signalis undefined on Node.js 10–14, which have noAbortController. New exported types:OnLogCallback,OnLogContext.colorizeBackgroundoption.CONSOLE_LOG_COLORIZE_BACKGROUNDcan now also be set as a direct option or throughenvOptions, like every other setting.
Bug Fix
- Bundler
definenow configures the logger. The README's vite/webpackdefine: { 'process.env.CONSOLE_LOG_JSON_...': '"true"' }example never worked, because the logger readprocess.env[name]and bundlers only replace staticprocess.env.NAMEexpressions. EachCONSOLE_LOG_*variable is now also read statically. - Every entry has a
message. An Error with an empty message no longer produces a log without one. - Nested context keys are always sorted. They were sorted only when the log merged several objects (as it does with the default metadata on), so with
noFileName,noPackageNameandnoStackForNonErrorall set, nested keys kept their original order. noStackForNonErrormatches its name, and the{ level }override,customOptionsandtransformOutputfixes listed above.
Documentation
- README examples corrected: circular references (
[Circular ~.self]),transformOutputfallback, newline options,contextKeytop-level fields. - New README sections: key collisions (
_prefix), placeholder messages, BigInt values, the standard color variables (FORCE_COLOR,FORCE_NO_COLOR,DYNO), the extended console methods, and callingLoggerAdaptToConsole()more than once (each call replaces the configuration). - Removed the browser "set
process.envbefore import" option: ES module imports are hoisted, so the ordering it showed wasn't real. Settings are read whenLoggerAdaptToConsole()runs.
Other
- Release workflow: the release commit and tag are pushed atomically, and a run fails before publishing if
mastermoved since it started. For v6.5.1,mastermoved during the run: the tag was pushed but the release commit (version bump and- deployedmarker) was rejected, and had to be merged intomasterby hand.