ionic3日志组件

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 背景我们在开发时都很喜欢使用console.*来打印出日志信息以方便我们调试代码,但是在上线时肯定是需要去除所有的console;但是大多数情况下开发者都会忘记移除console输出,这会带来生产日志风险。

背景

我们在开发时都很喜欢使用console.*来打印出日志信息以方便我们调试代码,但是在上线时肯定是需要去除所有的console;但是大多数情况下开发者都会忘记移除console输出,这会带来生产日志风险。

编写

  1. 新增一个日志组件:logger

ionic g provier logger
  1. 修改logger.ts的内容如下:
import {Injectable} from '@angular/core';
import {Constants} from "../Constants";

@Injectable()
export class Logger {

  constructor() {

  }

  /**
   * Logs messages or objects  with the debug level.
   * Works the same as console.log().
   */
  public log(...objects: any[]) {
    this.log_real(console.log, LogLevel.Debug, objects);
  }

  /**
   * Logs messages or objects  with the debug level.
   * Works the same as console.debug().
   */
  public debug(...objects: any[]) {
    this.log_real(console.log, LogLevel.Debug, objects);
  }

  /**
   * Logs messages or objects  with the info level.
   * Works the same as console.info().
   */
  public info(...objects: any[]) {
    this.log_real(console.info, LogLevel.Info, objects);
  }

  /**
   * Logs messages or objects  with the warning level.
   * Works the same as console.warn().
   */
  public warn(...objects: any[]) {
    this.log_real(console.warn, LogLevel.Warning, objects);
  }

  /**
   * Logs messages or objects  with the error level.
   * Works the same as console.error().
   */
  public error(...objects: any[]) {
    this.log_real(console.error, LogLevel.Error, objects);
  }

  protected async log_real(func: Function, level: LogLevel, objects: any[]) {
    const env = Constants.ENVIRONMENT || 'development';
    if (env !== 'production') {
      func.apply(console, objects);
    }
  }
}

export enum LogLevel {
  Error,
  Warning,
  Info,
  Debug,
}
  1. 在app.module.ts中引入Logger组件:
import {Logger} from "../common/logger/logger";
......
  providers: [
    Logger, 
]
......

使用:

  1. 在Constants中新增一个变量:
export const Constants = {
  ENVIRONMENT: 'development',//app环境,开发时为development,正式发布时需求注掉或者改为production
}
  1. 在需要打印日志的地方引用
import {Injectable} from "@angular/core";
import {RequestPreviewHandler} from "../RequestPreviewHandler";
import {HttpRequest} from "@angular/common/http";
import {Logger} from "../../../../common/logger/logger";

@Injectable()
export class DefaultRequestPreviewHandler extends RequestPreviewHandler {
  constructor(private logger: Logger) {
    super();
  }


  handle(request: HttpRequest<any>): HttpRequest<any> {
    this.logger.warn("未注入自定义请求前置处理类,使用默认前置处理");
    return request;
  }
}

效果与使用console一致,如下:


img_53448d43a57329fe051c784c61099afd.png
logger.png
  1. 生产发版
    只需要将Constants中ENVIRONMENT的值为production即可去除所有日志输出
export const Constants = {
  ENVIRONMENT: 'production',//app环境,开发时为development,正式发布时需求注掉或者改为production
}
;

避免了还需要手动删除console相关代码的问题。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
消息中间件 安全 Dubbo
Log4j安全漏洞前车之鉴,呕心整理工作中常用开源组件避坑版本
Log4j安全漏洞前车之鉴,呕心整理工作中常用开源组件避坑版本
395 0
|
JSON 中间件 Go
Golang高性能日志库zap + lumberjack 日志切割组件详解
Golang高性能日志库zap + lumberjack 日志切割组件详解
1520 0
Golang高性能日志库zap + lumberjack 日志切割组件详解
|
4月前
|
Perl
CocoaLumberjack增强异步日志组件BITCocoaLumberjack的使用
CocoaLumberjack增强异步日志组件BITCocoaLumberjack的使用
48 1
|
1月前
|
消息中间件 监控 搜索推荐
OpenFeign日志组件Logger原理与应用
该文章详细解释了如何在OpenFeign中配置并使用请求和响应的GZIP压缩功能。
|
1月前
|
Kubernetes API Docker
在K8S中,如何查看kubelet组件的日志?
在K8S中,如何查看kubelet组件的日志?
|
2月前
|
存储 弹性计算 运维
可观测性体系问题之Process Layer在ECS稳定性平台中的工作如何解决
可观测性体系问题之Process Layer在ECS稳定性平台中的工作如何解决
31 0
|
3月前
|
开发框架 安全 Java
信息打点-语言框架&开发组件&FastJson&Shiro&Log4j&SpringBoot等
信息打点-语言框架&开发组件&FastJson&Shiro&Log4j&SpringBoot等
|
4月前
|
NoSQL Go Redis
Golang实现redis系列-(1)日志组件的封装
Golang实现redis系列-(1)日志组件的封装
79 0
|
9月前
|
存储 JSON API
微服务框架 go-zero logx 日志组件剖析
微服务框架 go-zero logx 日志组件剖析
180 0
|
11月前
|
Shell Go
Golang 语言三方库 lumberjack 日志切割组件怎么使用?
Golang 语言三方库 lumberjack 日志切割组件怎么使用?
452 0