Skip to content

Console Logging Standards โ€‹

Overview โ€‹

Console logs in production code are now enforced at the ESLint level. All console.log() statements outside of test/story files must be wrapped in a dev check or use the devLog utility.

Pattern: Using devLog โ€‹

The recommended approach for development logging is the devLog utility:

javascript
import { devLog } from '@/lib/devLog'

// All methods are available
devLog('Simple message')
devLog('Message:', { data })
devLog.warn('Warning message')
devLog.error('Error:', exception)
devLog.info('Info message')
devLog.debug('Debug info')
devLog.table(arrayOfData)

Benefits:

  • โœ… Automatically wrapped in isDevelopment check
  • โœ… Zero overhead in production (compiled away)
  • โœ… Consistent logging interface across codebase
  • โœ… Passes ESLint without configuration

Pattern: Manual Wrapping (if needed) โ€‹

If you need to use raw console.log:

javascript
const isDevelopment = import.meta.env.DEV

if (isDevelopment) {
  console.log('This only logs in development')
}

When to use:

  • Complex conditional logging logic
  • Temporary debugging during specific workflow
  • Generally prefer devLog instead

ESLint Configuration โ€‹

Rules โ€‹

File TypeRuleBehavior
Production codeno-console: errorโŒ Disallowed (must use devLog)
Test filesno-console: offโœ… Allowed (for test debugging)
Story filesno-console: offโœ… Allowed (for Storybook testing)
Utility/Logger filesno-console: offโœ… Allowed (implementation files)

Overrides โ€‹

See .eslintrc.json for the full configuration. Key overrides:

json
{
  "files": ["src/**/*.stories.{js,jsx}", "src/__tests__/**/*.{js,jsx}"],
  "rules": { "no-console": "off" }
}

Checking for Violations โ€‹

Run the linter to find console statements:

bash
npm run lint

Find all console.log statements across codebase:

bash
npm run lint:console

This will show:

  • Which files have unwrapped console.log
  • Line numbers and context
  • Suggestions for fixing

Migration Path โ€‹

Step 1: Add Import โ€‹

javascript
import { devLog } from '@/lib/devLog'

Step 2: Replace Usage โ€‹

javascript
// Before
console.log('Loading venues:', venues)

// After
devLog('Loading venues:', venues)

Step 3: Verify โ€‹

bash
npm run lint

Examples by File Type โ€‹

Service/Library File โ€‹

javascript
// src/lib/venueService.js
import { devLog } from './devLog'

export async function getVenues(location) {
  devLog('Fetching venues for:', location)
  
  const venues = await fetchVenuesAPI(location)
  devLog('Venues fetched:', venues)
  
  return venues
}

Component File โ€‹

javascript
// src/components/VenueList.jsx
import { devLog } from '@/lib/devLog'

export default function VenueList({ venues }) {
  useEffect(() => {
    devLog('VenueList mounted with venues:', venues.length)
    return () => {
      devLog('VenueList unmounted')
    }
  }, [venues])

  return (...)
}

Story File (console allowed) โ€‹

javascript
// src/components/VenueList.stories.jsx
export const Default = () => {
  const handleClick = () => {
    console.log('Story button clicked') // OK - stories allow console
  }
  return <VenueList onClick={handleClick} />
}

Test File (console allowed) โ€‹

javascript
// src/__tests__/venueService.test.js
describe('venueService', () => {
  it('loads venues', () => {
    console.log('Testing venue load') // OK - tests allow console
    expect(getVenues()).toBeDefined()
  })
})

Benefits โ€‹

  1. Production Ready: Console logs are eliminated in production builds
  2. Performance: Zero runtime overhead in production
  3. Consistency: Uniform logging interface across the codebase
  4. Maintainability: Easy to identify debug code
  5. Security: Prevents accidental leak of sensitive data via console
  6. ESLint Integration: Automated enforcement, no manual reviews needed

FAQ โ€‹

Q: Why not just remove all console.logs?
A: Development debugging is essential. devLog keeps them without polluting production.

Q: Can I use devLog in browser DevTools?
A: Yes! In development mode, devLog calls console.log directly, so they appear in DevTools.

Q: What about console.warn and console.error?
A: ESLint allows these by default (they're considered safe). Use devLog.warn() and devLog.error() for consistency.

Q: Can story/test files use regular console?
A: Yes, ESLint overrides allow console in test/story files for debugging purposes.

Built with VitePress