Skip to main content
The LoggerService provides a comprehensive logging API for development and debugging with environment-based enablement and structured console output.

Import

import { LoggerService } from '@jet/services/logger/logger.service';

Usage

Basic Logging

import { inject } from '@angular/core';
import { LoggerService } from '@jet/services/logger/logger.service';

export class MyComponent {
  readonly #loggerService = inject(LoggerService);

  ngOnInit() {
    this.#loggerService.log('Component initialized with data:', this.data);
  }
}

Initialization Logging

@Injectable({ providedIn: 'root' })
export class MyService {
  readonly #loggerService = inject(LoggerService);

  constructor() {
    this.#loggerService.logServiceInitialization('MyService');
  }
}

@Component({ ... })
export class MyComponent implements OnInit {
  readonly #loggerService = inject(LoggerService);

  ngOnInit() {
    this.#loggerService.logComponentInitialization('MyComponent');
  }
}

Error Logging

try {
  await this.performOperation();
} catch (error) {
  if (error instanceof Error) {
    this.#loggerService.logError(error);
  } else {
    this.#loggerService.logException(error);
  }
}

Signal Effect Logging

const userSignal = signal<User | null>(null);

effect(() => {
  this.#loggerService.logEffectRun('user');
  const user = userSignal();
  // ... effect logic
}, { debugName: 'user' });

Methods

log

Logs one or more values to the console using console.log().
args
...unknown[]
required
Variable number of arguments to log
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/logger/logger.service.ts:14
public log(...args: unknown[]): void
Example:
this.loggerService.log('User data:', user, 'Timestamp:', Date.now());

logClassInitialization

Logs class initialization using console.info().
className
string
required
The name of the class being initialized
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/logger/logger.service.ts:22 Output format: Class {className} initialized.
public logClassInitialization(className: string): void
Example:
export class DataManager {
  constructor() {
    this.loggerService.logClassInitialization('DataManager');
  }
}
// Console: "Class DataManager initialized."

logComponentInitialization

Logs component initialization using console.debug().
componentName
string
required
The name of the component being initialized
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/logger/logger.service.ts:30 Output format: Component {componentName} initialized.
public logComponentInitialization(componentName: string): void
Example:
export class UserProfileComponent implements OnInit {
  ngOnInit() {
    this.loggerService.logComponentInitialization('UserProfileComponent');
  }
}
// Console: "Component UserProfileComponent initialized."

logDirectiveInitialization

Logs directive initialization using console.debug().
directiveName
string
required
The name of the directive being initialized
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/logger/logger.service.ts:38 Output format: Directive {directiveName} initialized.
public logDirectiveInitialization(directiveName: string): void
Example:
@Directive({ selector: '[appHighlight]' })
export class HighlightDirective implements OnInit {
  ngOnInit() {
    this.loggerService.logDirectiveInitialization('HighlightDirective');
  }
}
// Console: "Directive HighlightDirective initialized."

logEffectRun

Logs when an Angular signal effect runs using console.warn().
signalName
string
required
The name of the signal whose effect is running
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/logger/logger.service.ts:46 Output format: Running effect for {signalName}.
public logEffectRun(signalName: string): void
Example:
effect(() => {
  this.loggerService.logEffectRun('settings');
  const settings = this.settingsSignal();
  // ... effect logic
}, { debugName: 'settings' });
// Console: "Running effect for settings."

logError

Logs an Error object using console.error().
error
Error
required
The Error object to log
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/logger/logger.service.ts:54
public logError(error: Error): void
Example:
try {
  throw new Error('Failed to load data');
} catch (error) {
  if (error instanceof Error) {
    this.loggerService.logError(error);
  }
}

logException

Logs an unknown exception using console.error().
exception
unknown
required
The exception to log (any type)
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/logger/logger.service.ts:62
public logException(exception: unknown): void
Example:
try {
  await this.apiCall();
} catch (exception) {
  this.loggerService.logException(exception);
}

logServiceInitialization

Logs service initialization using console.info().
serviceName
string
required
The name of the service being initialized
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/logger/logger.service.ts:70 Output format: Service {serviceName} initialized.
public logServiceInitialization(serviceName: string): void
Example:
@Injectable({ providedIn: 'root' })
export class DataService {
  constructor() {
    this.loggerService.logServiceInitialization('DataService');
  }
}
// Console: "Service DataService initialized."

logWarning

Logs a warning message using console.warn().
warning
string
required
The warning message to log
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/logger/logger.service.ts:78
public logWarning(warning: string): void
Example:
if (!this.config.apiKey) {
  this.loggerService.logWarning('API key not configured');
}

Configuration

Environment Setup

Configure logging in your environment files:
// src/environments/environment.ts
export const environment = {
  isLoggingEnabled: true // Enabled in development
};

// src/environments/environment.prod.ts
export const environment = {
  isLoggingEnabled: false // Disabled in production
};

Injection Token

The service uses the IS_LOGGING_ENABLED injection token:
import { IS_LOGGING_ENABLED } from '@jet/injection-tokens/is-logging-enabled.injection-token';
import { environment } from './environments/environment';

export const appConfig: ApplicationConfig = {
  providers: [
    { provide: IS_LOGGING_ENABLED, useValue: environment.isLoggingEnabled }
  ]
};

Features

Environment-Based Enablement

All logging methods check if logging is enabled before outputting:
public log(...args: unknown[]): void {
  if (!this.#isLoggingEnabled) {
    return; // No-op in production
  }
  console.log(...args);
}

Console Level Mapping

Different methods use appropriate console levels:
  • log()console.log() - General logging
  • logInfo(), logServiceInitialization(), logClassInitialization()console.info() - Informational
  • logComponentInitialization(), logDirectiveInitialization()console.debug() - Debug info
  • logWarning(), logEffectRun()console.warn() - Warnings
  • logError(), logException()console.error() - Errors

TypeScript Type Safety

Methods accept appropriate types:
  • log() accepts ...unknown[] for maximum flexibility
  • logError() requires an Error object
  • logException() accepts unknown for any exception type
  • Other methods accept specific strings

Best Practices

  1. Use appropriate log levels - Choose the right method for the message type
  2. Include context - Add relevant data to help with debugging
  3. Disable in production - Set isLoggingEnabled: false for production builds
  4. Use structured logging - Leverage specific methods like logServiceInitialization()
  5. Log errors properly - Use logError() for Error objects and logException() for unknown types

Common Patterns

Service Lifecycle Tracking

@Injectable({ providedIn: 'root' })
export class DataService {
  readonly #loggerService = inject(LoggerService);

  constructor() {
    this.#loggerService.logServiceInitialization('DataService');
  }

  fetchData() {
    this.#loggerService.log('Fetching data...');
    // ... fetch logic
  }
}

Component Lifecycle Tracking

export class UserComponent implements OnInit, OnDestroy {
  readonly #loggerService = inject(LoggerService);

  ngOnInit() {
    this.#loggerService.logComponentInitialization('UserComponent');
  }

  ngOnDestroy() {
    this.#loggerService.log('UserComponent destroyed');
  }
}

Error Handling

async loadData() {
  try {
    const data = await this.api.getData();
    this.#loggerService.log('Data loaded:', data);
  } catch (error) {
    if (error instanceof Error) {
      this.#loggerService.logError(error);
    } else {
      this.#loggerService.logException(error);
    }
    throw error;
  }
}

Dependencies

The LoggerService depends on:
  • IS_LOGGING_ENABLED - Environment-based feature flag

Build docs developers (and LLMs) love