Angular控制台仅在开发环境上记录日志 [英] Angular console log only on development environment

查看:216
本文介绍了Angular控制台仅在开发环境上记录日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的Angular应用程序(使用Angular CLI制作)中,我们使用了几个console语句.是否存在一种全局方法来检测环境,然后仅在开发中的组件和服务中显示console.log?

In our Angular app (made with Angular CLI) we use several console statements. Is there a global way to detect environment and then display console.log in our components and service only under development?

我所说的全球性含义-我知道我们可以使用类似的东西:

What I mean by global way - I know we can use something like:

if (!environment.production) {
  console.log(this.reviewTasksList);
}

但是每次使用此代码时,我们都必须console.log(以及必要的导入操作,以获取environment变量),我们的代码将变得很冗长.

But by using this code everytime we have to console.log (along with necessary import to get environment variable) our code will become kind of verbose.

我想知道是否有办法:

  • 以更快的方式访问环境
  • 也许会在产品构建时删除所有控制台日志

或者这里更好的解决方案是创建一个记录器服务,并在其中检查所有环境?

Or the better solution here is to create a logger service and do all the environment check within it?

我不希望捆绑包的大小受到调试语句和服务的影响.

I don't want my bundle size to be impacted by debug statements and service.

推荐答案

您可以使用

You can use isDevMode() or your environment.production to check if the code runs in development or production mode.

我认为记录器服务是一个好主意,然后根据模式在提供商中注册其他记录器服务.

I think a logger service would be a good idea and then register a different logger service in providers depending on the mode.

另请参见 https://github.com/angular/angular/pull/14308

这篇关于Angular控制台仅在开发环境上记录日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆