Home > Web Front-end > JS Tutorial > body text

Let's talk about how Angular+Service improves the logging function

青灯夜游
Release: 2021-09-17 10:24:45
forward
1990 people have browsed it

How to improve Angular's log usage? The following article will introduce to you how to use the Service management console output in Angular to improve the logging function. I hope it will be helpful to you!

Let's talk about how Angular+Service improves the logging function

Improve the way logging is used in Angular applications

AngularYes A very popular development framework. Front-end developers like to use console to debug their code in applications. However, due to the need for continuous delivery/deployment, these debugging codes will be deleted and will not enter production. release environment. [Related tutorial recommendations: "angular tutorial"]

Lets talk about how Angular+Service improves the logging function

##Let Angular help us implement this function

Angular provides us with the function of registering Services into the application, so that we can reuse some functions in the component.

Therefore, we can use

Service to manage our console output and thereby improve the logging function.

1 : Use Service to manage the console

import { Injectable } from '@angular/core';
@Injectable({
  providedIn: 'root'
})
export class LogService {

  constructor() {

  }
  trace(...data: any[]): void {
    console.trace(data);
  }
  log(...data: any[]): void {
    console.log(data);
  }
}
Copy after login

Use it in the

AppComponent component::

logService.log('console executed from AppComponent');
Copy after login

Lets talk about how Angular+Service improves the logging function

The above code is easy to understand, but there is a problem, we cannot know whether the log is in the application Which component is printed, unless we indicate it in the log message. For example, the log information in the figure shows that it comes from the

AppComponent component. We hope that the right side of the log can automatically indicate which component it comes from, and It is not the file bit log.service.ts:xx that defines the entire logging system, and we do not need to manually indicate it in the log message.

1.1: Use logService.trace()

It can be used to trace the source of the log, which looks good, but in fact it will add some unnecessary Logging.

2: Enhanced version of logService

import { Injectable } from '@angular/core';
@Injectable({
  providedIn: 'root'
})
export class LogService {

  constructor() {

  }
  trace(source: string, ...data: any[]): void {
    console.trace(data);
  }
  log(source: string, ...data: any[]): void {
    console.log(data);
  }
}
Copy after login

Compared with the previous one, the enhanced version of the

logService class method receives additional parameters.

logService.log('AppComponent','console executed from AppComponent');
Copy after login

Lets talk about how Angular+Service improves the logging function

Very good

The above is the detailed content of Let's talk about how Angular+Service improves the logging function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:掘金--JaylenL
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template