Skip to content

Conversation

@Zeteticks
Copy link
Contributor

Overview

This PR introduces a production-ready structured logging system that replaces scattered console statements throughout the codebase with a centralized, environment-aware logging service.

Problem

Currently, the codebase has console statements scattered across 15+ files, which:

  • Makes production debugging difficult
  • Provides no structured context
  • Cannot be easily filtered or aggregated
  • Exposes debug logs in production

Solution

New Structured Logger (src/lib/logger.ts)

A centralized logging utility featuring:

  • Multiple log levels: debug, info, warn, error
  • Environment-aware: Verbose in dev, minimal in production
  • Request tracing: Support for context (requestId, userId, documentId, etc.)
  • Child loggers: Maintain persistent context across related operations
  • Structured formatting: Consistent timestamp and context formatting

Updated Files

  1. src/lib/api-utils.ts

    • Replaced console.error with logger.error in error handler
    • Added proper error context
  2. src/app/employer/documents/components/AgentChatInterface.tsx

    • Replaced all console.log/error with appropriate logger methods
    • Added context (chatId, documentId, companyId) for traceability
    • Converted debug console.log to logger.debug (dev-only)
  3. src/app/employer/documents/fetchWithRetries.ts

    • Replaced console.warn with logger.warn
    • Added retry context (url, attempt, maxRetries)

Usage Example

import { logger } from '~/lib/logger';

// Simple logging
logger.info('Document uploaded successfully', { documentId: 123 });

// Error logging with context
logger.error('Failed to process document', 
  { documentId: 123, userId: 'abc' }, 
  error
);

// Child logger with persistent context
const requestLogger = logger.withContext({ requestId: 'req-123' });
requestLogger.info('Processing request'); // Includes requestId automatically

Benefits

Better Debugging: Structured logs with context make issues easier to trace
Production-Ready: Automatic log level filtering based on environment
Maintainability: Easy to add log aggregation services (Datadog, Sentry, etc.)
Consistency: Uniform format across the entire application
Performance: Production builds only log warnings and errors

Testing

Manually tested in development environment:

  • ✅ API error handling with proper context
  • ✅ Chat interface logging during user interactions
  • ✅ Network retry scenarios with attempt tracking
  • ✅ Verified log output format and context preservation
  • ✅ Confirmed production mode filters debug/info logs

Migration Path

This PR updates the most critical files. Future PRs can gradually replace remaining console statements across:

  • Other API routes
  • Employee interface components
  • Admin pages

Breaking Changes

None - This is a purely additive change with no impact on existing functionality.


Looking forward to feedback! Happy to make any adjustments.

This PR introduces a production-ready structured logging system that replaces scattered console statements throughout the codebase with a centralized, environment-aware logging service.

## Changes

### New Files
- `src/lib/logger.ts`: Centralized logging utility with:
  - Support for multiple log levels (debug, info, warn, error)
  - Environment-aware behavior (verbose in dev, minimal in production)
  - Request tracing with context support (requestId, userId, documentId, etc.)
  - Child logger support for maintaining persistent context
  - Structured log formatting with timestamps

### Modified Files
- `src/lib/api-utils.ts`: Replaced console.error with structured logger in error handler
- `src/app/employer/documents/components/AgentChatInterface.tsx`: Replaced console.log/error with logger across component
- `src/app/employer/documents/fetchWithRetries.ts`: Replaced console.warn with structured logger

## Benefits
- **Better Debugging**: Structured logs with context make issues easier to trace
- **Production-Ready**: Automatic log level filtering based on environment
- **Maintainability**: Centralized logging makes it easy to add features like log aggregation
- **Consistency**: Uniform log format across the entire application
- **Performance**: Production builds only log warnings and errors

## Testing
Manually tested in development environment with various scenarios:
- API error handling
- Chat interface interactions
- Network retry scenarios
- Verified log output format and context preservation

Resolves issue with scattered console statements making production debugging difficult.
@vercel
Copy link

vercel bot commented Nov 20, 2025

@Zeteticks is attempting to deploy a commit to the Timothy Lin's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Dec 5, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
pdr-ai-v2 Error Error Dec 5, 2025 3:01pm

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