Skip to content

Conversation

@RajdeepKushwaha5
Copy link

Replace Console Statements with Structured Logging

Overview

This PR introduces a comprehensive logging infrastructure to replace scattered console statements throughout the Angular application with structured, environment-aware logging. This improves code quality, debugging capabilities, and production readiness.

Changes Made

New LoggerService Implementation

  • Created: src/app/shared/services/logger.service.ts
  • Features:
    • Environment-aware logging levels (DEBUG/INFO/WARN/ERROR/OFF)
    • Production-safe configuration (ERROR level in production, DEBUG in development)
    • Clean API with methods: debug(), info(), warn(), error()
    • Centralized logging configuration

Component Updates

Updated 16 TypeScript components and 1 service to use LoggerService:

Components:

  • app.component.ts
  • sample.component.ts
  • disable-popup.component.ts
  • lesson-plan-resource-details.component.ts
  • upload-popup.component.ts
  • user-staff-list.component.ts
  • audit-log.component.ts
  • school-add-edit.component.ts
  • school-list.component.ts
  • shikshan-user-manage.component.ts
  • lesson-content-list.component.ts
  • question-bank-generation.component.ts
  • dashboard.component.ts
  • inspect-lesson-plan.component.ts
  • add-edit-user.component.ts

Services:

  • idle.service.ts

Console Statement Replacements

  • Total console statements replaced: 28
  • Replaced with meaningful logger calls including contextual error messages
  • Examples:
    // Before
    console.log('upload file logic here');
    console.error(err);
    
    // After
    this.logger.debug('Upload file logic triggered');
    this.logger.error('Error fetching user details:', err);
    

Benefits

Production Readiness

  • Environment-aware logging: DEBUG logs in development, ERROR only in production
  • No more debug noise in production console
  • Structured error handling with meaningful messages

Improved Debugging

  • Centralized logging configuration - easy to adjust log levels
  • Contextual error messages instead of raw console output
  • Consistent logging format across the application

Code Quality

  • Removed debug code from production builds
  • Professional logging infrastructure following best practices
  • Better maintainability with single source of logging logic

Monitoring & Observability

  • Structured logs ready for log aggregation tools
  • Meaningful error context for better issue tracking
  • Consistent error reporting across all components

Technical Details

LoggerService API

@Injectable({
  providedIn: 'root'
})
export class LoggerService {
  debug(message: string, ...optionalParams: any[]): void
  info(message: string, ...optionalParams: any[]): void
  warn(message: string, ...optionalParams: any[]): void
  error(message: string, ...optionalParams: any[]): void
}

Environment Configuration

  • Development: All log levels enabled (DEBUG, INFO, WARN, ERROR)
  • Production: Only ERROR level enabled
  • Configurable: Easy to adjust via environment variables

Files Changed

  • Modified: 41 files
  • New: 1 file (logger.service.ts)
  • Lines: +3,204 insertions, -1,766 deletions

Testing Considerations
✅ Build verification: All components compile successfully
✅ LoggerService integration: All components properly inject and use LoggerService
✅ Environment testing: Verify different log levels work in dev/prod
✅ Error handling: Confirm error messages are meaningful and contextual

Breaking Changes

  • None: This is a non-breaking change that improves existing functionality
  • Backward compatibility: No API changes to existing component interfaces

Related Issues

  • Improves code quality and debugging capabilities
  • Addresses production logging concerns
  • Prepares codebase for better monitoring and observability

- Add LoggerService with environment-aware logging (DEBUG/INFO/WARN/ERROR/OFF)
- Replace all console.log/error/warn statements with proper logger methods
- Update 16 TypeScript components and 1 service to use LoggerService
- Improve code quality and production readiness with structured logging
- Add meaningful error messages for better debugging and monitoring
@RajdeepKushwaha5
Copy link
Author

@microsoft-github-policy-service agree

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