Stop Using console.log and Start Using This Service!
Logging is an essential part of any software development process. It helps developers debug applications, understand workflows, and track down issues. However, relying solely on console.log for logging in an Angular application can quickly become problematic, especially when moving from development to production environments.
In this article, I will introduce a logging service that can be easily integrated into your Angular application. This service allows you to control the log level based on the environment configuration, ensuring that verbose logging is available during development but minimized or disabled in production.
Why You Should Stop Using console.log
Using console.log
statements scattered throughout your codebase can lead to several issues:
- Performance Issues: Excessive logging in production can degrade performance.
- Security Risks: Sensitive information might be logged and exposed.
- Clutter: Logs can quickly become cluttered, making it difficult to find relevant information.
By using a structured logging service, you can mitigate these issues and have more control over what gets logged and when.
Introducing the Log Service
Here’s a simple but effective logging service for your Angular application. It uses an enumerated log level to control what messages are logged based on the current environment configuration.
Log Levels Enum
First, we define an enumeration for the different log levels:
1
2
3
4
5
6
7
export enum LogLevel {
info,
error,
warn,
debug,
all,
}
Log Service
Next, we create the LogService that utilizes this enum to conditionally log messages:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Injectable } from "@angular/core";
import { environment } from "../../environments/environment";
import { LogLevel } from "../models/log.interface";
@Injectable({
providedIn: "root",
})
export class LogService {
constructor() {}
log(message?: any, ...optionalParams: any[]) {
if (environment.logLevel >= LogLevel.debug) {
console.log(...[message, ...optionalParams]);
}
}
table(message?: any, ...optionalParams: any[]) {
if (environment.logLevel >= LogLevel.debug) {
console.table(...[message, ...optionalParams]);
}
}
trace(message?: any, ...optionalParams: any[]) {
if (environment.logLevel >= LogLevel.debug) {
console.trace(...[message, ...optionalParams]);
}
}
error(message?: any, ...optionalParams: any[]) {
if (environment.logLevel >= LogLevel.error) {
console.error(...[message, ...optionalParams]);
}
}
debug(message?: any, ...optionalParams: any[]) {
if (environment.logLevel >= LogLevel.debug) {
console.debug(...[message, ...optionalParams]);
}
}
info(message?: any, ...optionalParams: any[]) {
if (environment.logLevel >= LogLevel.info) {
console.info(...[message, ...optionalParams]);
}
}
warn(message?: any, ...optionalParams: any[]) {
if (environment.logLevel >= LogLevel.warn) {
console.warn(...[message, ...optionalParams]);
}
}
}
Environment Configuration
Finally, we configure the environments to use different log levels:
1
2
3
4
5
6
7
8
9
10
11
// environment.ts (Development)
export const environment = {
production: false,
logLevel: LogLevel.debug
};
// environment.prod.ts (Production)
export const environment = {
production: true,
logLevel: LogLevel.info
};
How to Use the Log Service
To start using the LogService, inject it into your components or services:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { Component, OnInit } from '@angular/core';
import { LogService } from './services/log.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(private logService: LogService) {}
ngOnInit() {
this.logService.info('Application initialized');
this.logService.debug('Debugging information');
}
}
Conclusion
By using this LogService
, you can gain better control over your logging output, ensuring that you have detailed logs in development while keeping your production environment clean and efficient. This approach not only enhances the maintainability of your application but also helps in adhering to best practices for logging in software development.
Stop scattering console.log
statements in your code today and start using a structured logging service for a more robust and manageable application!