[英]How to configure logger level for ngx-logger in global config file
I have recently included ngx-logger
for my project for implementing a logger level within the application.我最近在我的项目中包含了ngx-logger
,用于在应用程序中实现记录器级别。 I have hard-coded the logger level in app.module.ts
within the configuration of ngx-logger
but I need to implement this in some global config file.我在app.module.ts
ngx-logger
的配置中对ngx-logger
器级别进行了硬编码,但我需要在一些全局配置文件中实现它。
I had followed a tutorial here , which told me to hard-code the level within the configuration.我在这里学习了一个教程,它告诉我在配置中对级别进行硬编码。 The problem with this approach is that apart from the configurations already stated,if there is any other environment
defined other than what I have coded it would produce an error.这种方法的问题在于,除了已经说明的配置之外,如果除了我编码的environment
定义了任何其他environment
它都会产生错误。 I want to remove this hard-coded configuration and instead use some "config" file for managing the environment variables.我想删除这个硬编码的配置,而是使用一些“配置”文件来管理环境变量。 But I am not sure how to do that and there are no online resources which I could find.但我不知道该怎么做,也没有我能找到的在线资源。
here is my configuration:这是我的配置:
imports: [
BrowserModule,
BrowserAnimationsModule,
ToastrModule.forRoot(),
MDBBootstrapModule.forRoot(),
AppAsideModule,
AppBreadcrumbModule.forRoot(),
AppFooterModule,
AppHeaderModule,
AppSidebarModule,
PerfectScrollbarModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
AppRoutingModule,
// Logger config based on environment
LoggerModule.forRoot({
level: !environment.production ? NgxLoggerLevel.LOG : NgxLoggerLevel.OFF,
// serverLogLevel
serverLogLevel: NgxLoggerLevel.OFF
})
]
environment.ts:环境.ts:
export const environment = {
production: false,
isDebugMode: true,
lang: 'en',
api: {
grant_type: 'password',
clientId: 'sugar',
clientSecret: '',
host: "https://blablabla.com:44375/rest/v11_1/",
platform: 'custom_api',
},
};
tsconfig.json: tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}
my goal : finding a better logger level implementation instead of hard-coding我的目标:找到更好的记录器级别实现而不是硬编码
You can set another variable isdebug for all files.您可以为所有文件设置另一个变量 isdebug。
For production —————— isdebug=1对于生产————— isdebug=1
For debug —————— isdebug=2对于调试—————— isdebug=2
For mock —————— isdebug=3对于模拟————— isdebug=3
Your coding.你的编码。 (environment.isdebug === 1 || environment.isdebug === 2) ? (environment.isdebug === 1 || environment.isdebug === 2) ? NgxLoggerLevel.LOG :: NgxLoggerLevel.OFF NgxLoggerLevel.LOG :: NgxLoggerLevel.OFF
If you want to change on the fly use env.js for configuration, The following solution is good如果你想动态更改使用 env.js 进行配置,以下解决方案是好的
https://www.jvandemo.com/how-to-use-environment-variables-to-configure-your-angular-application-without-a-rebuild/ https://www.jvandemo.com/how-to-use-environment-variables-to-configure-your-angular-application-without-a-rebuild/
Here's a solution that I took a few days ago, facing the same task:这是我几天前采取的解决方案,面临同样的任务:
In app-module.ts:在 app-module.ts 中:
import { LoggerModule, NgxLoggerLevel, NGXLogger, LoggerConfig } from 'ngx-logger';
//...
@NgModule({
declarations: [
// ...
],
imports: [
LoggerModule,
// ...
],
providers: [
{
provide: LoggerConfig, useFactory: () => Utility.loadJson<LoggerConfig>('/assets/loggerConfig.json', //
{ // defaults, in case file is not found:
level: NgxLoggerLevel.WARN,
// serverLoggingUrl: '/api/logs',
// serverLogLevel: NgxLoggerLevel.OFF,
// disableConsoleLogging" : true
})
},
NGXLogger,
//...
]
Then have your utility-method ready:然后准备好您的实用程序方法:
export class Utility {
static loadJson<T>(file: string, defaultObject: T, onError = (event) => null ): T {
const request = new XMLHttpRequest();
request.open('GET', file, false);
request.onerror = onError;
if (request.overrideMimeType) {
request.overrideMimeType('application/json');
}
request.send();
if (request.status === 200) {
return <T>JSON.parse(request.responseText);
}
return defaultObject;
}
// ...
Then create your loggerConfig.json file in src/assets:然后在 src/assets 中创建您的 loggerConfig.json 文件:
{
"// levels:": "TRACE=0, DEBUG=1, INFO=2, LOG=3, WARN=4, ERROR=5, FATAL=6, OFF=7",
"level": 1,
"// serverLogLevel" : 5,
"// serverLoggingUrl": "/api/logs",
"// disableConsoleLogging" : true
}
Now add loggerConfig.json
to your gitignore
-file and push the changes: You don't want developers overriding each other's settings.现在将loggerConfig.json
添加到您的gitignore
文件并推送更改:您不希望开发人员覆盖彼此的设置。
The idea is, that usually the provided default is used, but you have the possibility to have your own override in a configuration file.这个想法是,通常使用提供的默认值,但您可以在配置文件中拥有自己的覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.