Skip to content

Commit

Permalink
fix: mute system logger in local dev (#458)
Browse files Browse the repository at this point in the history
Follow-up to #457. The system
logger shouldn't be visible in local dev.
  • Loading branch information
Skn0tt authored Jan 19, 2024
1 parent 3392c0b commit f4429db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lib/system_logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { env } from 'process'

const systemLogTag = '__nfSystemLog'

const serializeError = (error: Error): Record<string, unknown> => {
Expand Down Expand Up @@ -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 }))
}

Expand Down
22 changes: 22 additions & 0 deletions test/unit/system_logger.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const process = require("process")

const test = require('ava')

const { systemLogger, LogLevel } = require('../../dist/internal')
Expand Down Expand Up @@ -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
})

0 comments on commit f4429db

Please sign in to comment.