diff --git a/src/lib/system_logger.ts b/src/lib/system_logger.ts index a6cc5046..80afab13 100644 --- a/src/lib/system_logger.ts +++ b/src/lib/system_logger.ts @@ -1,3 +1,5 @@ +import { env } from 'process' + const systemLogTag = '__nfSystemLog' const serializeError = (error: Error): Record => { @@ -28,6 +30,10 @@ class SystemLogger { } private doLog(logger: typeof console.log, message: string) { + if (env.NETLIFY_DEV && !env.NETLIFY_ENABLE_SYSTEM_LOGGING) { + return + } + logger(systemLogTag, JSON.stringify({ msg: message, fields: this.fields })) } diff --git a/test/unit/system_logger.js b/test/unit/system_logger.js index 2a1926c7..712dfb28 100644 --- a/test/unit/system_logger.js +++ b/test/unit/system_logger.js @@ -1,3 +1,5 @@ +const process = require("process") + const test = require('ava') const { systemLogger, LogLevel } = require('../../dist/internal') @@ -35,3 +37,23 @@ test('Fields', (t) => { console.log = originalLog }) + +test('Local Dev', (t) => { + const originalLog = console.log + const logs = [] + console.log = (...message) => logs.push(message) + systemLogger.log('hello!') + t.is(logs.length, 1) + + process.env.NETLIFY_DEV= "true" + systemLogger.log('hello!') + t.is(logs.length, 1) + + process.env.NETLIFY_ENABLE_SYSTEM_LOGGING= "true" + systemLogger.log('hello!') + t.is(logs.length, 2) + + delete process.env.NETLIFY_DEV + delete process.env.NETLIFY_ENABLE_SYSTEM_LOGGING + console.log = originalLog +})