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

fix: mute system logger in local dev #458

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})