如果在生产环境中,如何只启用错误日志记录?
How to only enable error logging if in production environment?
我有一个 Angular 5 应用程序,使用 sentry.io 进行错误记录,它使用 raven.js.
我让这一切正常工作,但不想在 运行 开发时记录错误。如何仅在启用生产模式时启用错误日志记录?
我的 app.module.ts
中有以下内容
import * as Raven from 'raven-js';
Raven
.config('https://xxx@sentry.io/xxx')
.install();
export class RavenErrorHandler implements ErrorHandler {
handleError(err: any): void {
Raven.captureException(err);
}
}
你应该使用函数 isDevMode
import { isDevMode } from '@angular/core';
export class AppComponent {
constructor() {
console.log(isDevMode());
}
}
如果没有返回true,应该是prod
如果您使用 angular-cli,他们有您可以简单地执行的环境文件
if (environment.production) { ... }
我认为它值得分享,因为我有一个类似的问题,我想...
- 仅在生产中使用我的 Sentry.io errorHandler
- 在开发中使用Angular默认的ErrorHandler
- 将依赖项注入处理程序
app.error-handler.ts
第一步是根据 environment.production
属性 return SentryErrorHanddler
或 Angular 默认值 ErrorHandler
实现工厂提供程序函数并接收所需的依赖项作为参数。
import { ErrorHandler } from '@angular/core';
import * as Sentry from '@sentry/browser';
import { environment } from '../environments/environment';
export function errorHandlerFactory(toastService: ToastService) {
if (environment.production) {
return new SentryErrorHandler(toastService);
}
return new ErrorHandler();
}
第二步是实现 SentryErrorHandler
class 将接收依赖关系到构造函数 (注意我没有在 class 前加上 @Injectable装饰器,因为它将被上面的工厂实例化,而不是被 DI 注入).
export class SentryErrorHandler implements ErrorHandler {
constructor(public toastService: ToastService) {
Sentry.init({
dsn: 'https://<YOUR-SENTRY-KEY>@sentry.io/<YOUR-PROJECT-ID>',
});
}
handleError(error: any) {
Sentry.captureException(error.originalError || error);
this.toastService.show('An error occurs, please try again later.');
throw error;
}
}
app.module.ts
最后,在 AppModule
中为 ErrorHandler
提供已实现的工厂提供程序 errorHandlerFactory
并通过 deps
属性 指定它需要一些注入依赖项,这是将作为参数传递给工厂提供者函数
import { ErrorHandler } from '@angular/core';
import { errorHandlerFactory } from './app.error-handler';
@NgModule({
//...
providers: [
{ provide: ErrorHandler, useFactory: errorHandlerFactory, deps: [ToastService] },
],
})
export class AppModule { }
请注意,与 ToastService
相关的所有内容仅作为示例,表示您希望访问自定义处理程序的外部依赖项。
我有一个 Angular 5 应用程序,使用 sentry.io 进行错误记录,它使用 raven.js.
我让这一切正常工作,但不想在 运行 开发时记录错误。如何仅在启用生产模式时启用错误日志记录?
我的 app.module.ts
中有以下内容import * as Raven from 'raven-js';
Raven
.config('https://xxx@sentry.io/xxx')
.install();
export class RavenErrorHandler implements ErrorHandler {
handleError(err: any): void {
Raven.captureException(err);
}
}
你应该使用函数 isDevMode
import { isDevMode } from '@angular/core';
export class AppComponent {
constructor() {
console.log(isDevMode());
}
}
如果没有返回true,应该是prod
如果您使用 angular-cli,他们有您可以简单地执行的环境文件
if (environment.production) { ... }
我认为它值得分享,因为我有一个类似的问题,我想...
- 仅在生产中使用我的 Sentry.io errorHandler
- 在开发中使用Angular默认的ErrorHandler
- 将依赖项注入处理程序
app.error-handler.ts
第一步是根据 environment.production
属性 return SentryErrorHanddler
或 Angular 默认值 ErrorHandler
实现工厂提供程序函数并接收所需的依赖项作为参数。
import { ErrorHandler } from '@angular/core';
import * as Sentry from '@sentry/browser';
import { environment } from '../environments/environment';
export function errorHandlerFactory(toastService: ToastService) {
if (environment.production) {
return new SentryErrorHandler(toastService);
}
return new ErrorHandler();
}
第二步是实现 SentryErrorHandler
class 将接收依赖关系到构造函数 (注意我没有在 class 前加上 @Injectable装饰器,因为它将被上面的工厂实例化,而不是被 DI 注入).
export class SentryErrorHandler implements ErrorHandler {
constructor(public toastService: ToastService) {
Sentry.init({
dsn: 'https://<YOUR-SENTRY-KEY>@sentry.io/<YOUR-PROJECT-ID>',
});
}
handleError(error: any) {
Sentry.captureException(error.originalError || error);
this.toastService.show('An error occurs, please try again later.');
throw error;
}
}
app.module.ts
最后,在 AppModule
中为 ErrorHandler
提供已实现的工厂提供程序 errorHandlerFactory
并通过 deps
属性 指定它需要一些注入依赖项,这是将作为参数传递给工厂提供者函数
import { ErrorHandler } from '@angular/core';
import { errorHandlerFactory } from './app.error-handler';
@NgModule({
//...
providers: [
{ provide: ErrorHandler, useFactory: errorHandlerFactory, deps: [ToastService] },
],
})
export class AppModule { }
请注意,与 ToastService
相关的所有内容仅作为示例,表示您希望访问自定义处理程序的外部依赖项。