functions icon indicating copy to clipboard operation
functions copied to clipboard

feat: support custom raw logger

Open eduardoboucas opened this issue 1 year ago • 0 comments
trafficstars

Which problem is this pull request solving?

This PR proposes two changes to the API of the recently-added system logger:

  1. Remove the NETLIFY_DEV and NETLIFY_ENABLE_SYSTEM_LOGGING environment variables in favour of a new Silent log 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 --debug flag 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")
    
  2. Allow consumers to specify a custom raw logger that replaces the default globalThis.console methods. We can use this to plug this logger into the system logger that we expose to build plugins, where we want to call utils.systemLog and not console.log on 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")
    }
    

eduardoboucas avatar Jan 19 '24 11:01 eduardoboucas