functions
functions copied to clipboard
feat: support custom raw logger
Which problem is this pull request solving?
This PR proposes two changes to the API of the recently-added system logger:
-
Remove the
NETLIFY_DEVandNETLIFY_ENABLE_SYSTEM_LOGGINGenvironment variables in favour of a newSilentlog level. If we want to silence the logger from the CLI or any other consumer applications, we should let them configure that behaviour themselves rather than making the logger rely on their internals.Moreover, this lets the CLI more easily configure this behaviour conditionally — e.g. show system logs if the
--debugflag has been used, which is what we do currently with edge functions.import { systemLogger, LogLevel } from "@netlify/functions/dist/internal.js" const logger = systemLogger.withLogLevel(flags.debug ? LogLevel.Log : LogLevel.Silent) logger.log("Hello") -
Allow consumers to specify a custom raw logger that replaces the default
globalThis.consolemethods. We can use this to plug this logger into the system logger that we expose to build plugins, where we want to callutils.systemLogand notconsole.logon the resulting payload.I'm exporting the underlying class (which I've renamed to
StructuredLogger, because I think it's more accurate), which accepts a raw logger in the constructor.import { LogLevel, StructuredLogger } from "@netlify/functions/dist/internal.js" export const onPreBuild = ({ utils }) => { const logger = new StructuredLogger(LogLevel.Log, utils.systemLog) logger.withFields({ foo: "bar" }).log("This is a system log") }