如何在全局配置文件中为ngx-logger配置记录程序级别 [英] How to configure logger level for ngx-logger in global config file

查看:38
本文介绍了如何在全局配置文件中为ngx-logger配置记录程序级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近为我的项目添加了 ngx-logger ,用于在应用程序中实现记录器级别.我已经在 ngx-logger 的配置中的 app.module.ts 中对记录器级别进行了硬编码,但是我需要在一些全局配置文件中实现它.

I have recently included ngx-logger for my project for implementing a logger level within the application. 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.

我已按照教程

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. 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.

这是我的配置:

  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:

environment.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

推荐答案

解决方案1:

您可以为所有文件设置另一个变量isdebug.

Solution 1:

You can set another variable isdebug for all files.

用于生产——————isdebug = 1

For production —————— isdebug=1

用于调试——————isdebug = 2

For debug —————— isdebug=2

模拟——————isdebug = 3

For mock —————— isdebug=3

您的编码.(environment.isdebug === 1 || environment.isdebug === 2)?NgxLoggerLevel.LOG :: NgxLoggerLevel.OFF

Your coding. (environment.isdebug === 1 || environment.isdebug === 2) ? NgxLoggerLevel.LOG :: NgxLoggerLevel.OFF

如果您想即时更改,请使用env.js进行配置,以下解决方案很好

If you want to change on the fly use env.js for configuration, The following solution is good

查看全文

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