Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom raw logger #461

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

eduardoboucas
Copy link
Member

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 eduardoboucas requested a review from a team as a code owner January 19, 2024 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant