ionic3日志组件

本文介绍了在 Ionic3 中创建一个日志组件 `logger`,用于替代 `console`,便于调试并能在生产环境中轻松移除日志输出,降低生产风险。通过在 `app.module.ts` 引入组件,并在 Constants 中设置环境变量,实现调试与生产的无缝切换。
摘要由CSDN通过智能技术生成

背景

我们在开发时都很喜欢使用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 i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值