我对 angular 2 有一个要求,例如,我想将所有控制台日志移动到像 Logstash 这样的服务器,并想在 Kibana 中查看这些日志。创建我自己的日志服务如下。
import { Injectable } from '@angular/core';
@Injectable()
export class LoggerService {
enabled = true;
noop = () => {};
constructor() {
// You can do a check on your environment or some other flag to
// enable/disable logging
// if ((<any>'<%= ENV %>') === 'prod') this.enabled = false;
}
get debug() {
if (this.enabled) return console.debug.bind(console);
return this.noop;
}
get error() {
if (this.enabled) return console.error.bind(console);
return this.noop;
}
get log() {
if (this.enabled) return console.log.bind(console);
return this.noop;
}
get info() {
if (this.enabled) return console.info.bind(console);
return this.noop;
}
get warn() {
if (this.enabled) return console.warn.bind(console);
return this.noop;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以在其他一些组件中使用该服务,如下所示。
构造函数(私有 loggerService LoggerService){
this.loggerService.log("some error");
Run Code Online (Sandbox Code Playgroud)
}
但是上面的行在控制台中打印日志。我想将这些日志移动到服务器,以便我们可以随时查看。我为此要求谷歌了很多,但在angular2中没有得到任何东西。但是找到了一个博客,它在 angular 1.x 中实现。
有人可以在这里帮助我。提前致谢。
| 归档时间: |
|
| 查看次数: |
1561 次 |
| 最近记录: |