问题描述
我希望将日志记录引入angular2应用,并希望检查您可以为此推荐的任何好的库或方法.
I am looking to introduce logging to an angular2 app and want to check any good libraries or approaches you could recommend on this.
记录的要求是:
- 将具有用于配置日志记录的选项,例如信息,警告,错误,调试和详细信息.
- 将能够将日志保存到本地存储中,然后在一定间隔后将日志同步到服务器端点
- 将能够支持Json格式并可以控制日志格式
下面的要求很高兴,并且您可以分享与网络工作者合作的任何经验.
Requirement below would be nice to have and any experience on working with web worker that you can share would be appreciated.
- 将日志记录功能构建为网络工作者,从而远离浏览器线程,而我们有可能使用应用程序缓存作为临时存储,这会很好吗?
任何对此的建议将不胜感激.
Any advice on this would be much appreciated.
推荐答案
您可以使用ngx-logger
You can use ngx-logger
NGX Logger 是用于angular的简单日志记录模块(目前支持angular 4.3+) .它允许对控制台进行漂亮的打印",并允许将日志消息发布到URL进行服务器端日志记录.
NGX Logger is a simple logging module for angular (currently supports angular 4.3+). It allows "pretty print" to the console, as well as allowing log messages to be POSTed to a URL for server-side logging.
安装
安装后,您需要导入我们的主模块:
Once installed you need to import our main module:
import { LoggerModule, NgxLoggerLevel } from 'ngx-logger';
剩下的唯一部分是在应用程序模块中列出导入的模块,并传入配置以初始化记录器.
The only remaining part is to list the imported module in your application module, passing in a config to intialize the logger.
@NgModule({
declarations: [AppComponent, ...],
imports: [LoggerModule.forRoot({serverLoggingUrl: '/api/logs', level: NgxLoggerLevel.DEBUG, serverLogLevel: NgxLoggerLevel.ERROR}), ...],
bootstrap: [AppComponent]
})
export class AppModule {
}
用法
要使用Logger,您需要在本地将其导入,然后调用其中一种日志记录功能
To use the Logger, you will need import it locally, then call one of the logging functions
import { Component } from '@angular/core';
import { NGXLogger } from 'ngx-logger';
@Component({
selector: 'your-component',
templateUrl: './your.component.html',
styleUrls: ['your.component.less'],
providers: [NGXLogger]
})
export class YourComponent {
constructor(private logger: NGXLogger) {
this.logger.debug('Your log message goes here');
this.logger.debug('Multiple', 'Argument', 'support');
};
}
配置选项
serverLogLevel-仅将指定级别或更高级别的日志发送到服务器(OFF禁用服务器的记录器)serverLoggingUrl-POST日志的URL级别:该应用只会记录该级别或更高级别的消息(关闭"将禁用客户端的记录器)enableDarkTheme:将默认颜色设置为白色,而不是黑色跟踪|调试|信息|日志|警告|错误|关闭服务器端日志记录
serverLogLevel - Only sends logs to the server for the level specified or higher (OFF disables the logger for the server)serverLoggingUrl - URL to POST logslevel: The app will only log message for that level or higher (OFF disables the logger for the client)enableDarkTheme: Sets the default color to white instead of blackTRACE|DEBUG|INFO|LOG|WARN|ERROR|OFFServer Side Logging
如果存在serverLogginUrl,则NGX Logger将尝试将该日志发布到服务器.
If serverLogginUrl exists, NGX Logger will attempt to POST that log to the server.
有效负载示例{级别:调试",消息:您的日志消息在此处"}
Payload Example {level: 'DEBUG', message: 'Your log message goes here'}
您也可以阅读相同的文档. https://www.npmjs.com/package/ngx-logger
You can read the documentation as well for the same.https://www.npmjs.com/package/ngx-logger
这篇关于Angular2记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!