Skip to content

Debug switches

John Biundo edited this page Jul 15, 2019 · 6 revisions

Table of Contents

Overview

NestJSConfigManager uses the awesome debug utility to let you peek inside what it's doing. When you enable debug mode, the module will print out some extra logging information that may be useful in confirming correct behavior of your setup. Here's a sample using the debug switch cfg:

[0] 2019-07-12T16:27:37.004Z cfg > cfg options:  { envKey: 'MY_ENV', useFile: 'config/test.env', onError: 'exit' }
[0] 2019-07-12T16:27:37.006Z cfg > environment (using default): undefined
[0] 2019-07-12T16:27:37.006Z cfg > envRoot:  /home/john/code/config-typeorm
[0] 2019-07-12T16:27:37.006Z cfg > resolving envfile path...
[0] 2019-07-12T16:27:37.006Z cfg > ... from "useFile" with file: config/test.env
[0] 2019-07-12T16:27:37.007Z cfg > Parsing dotenv config file:  /home/john/code/config-typeorm/config/test.env
[0] 2019-07-12T16:27:37.009Z cfg > Parsed config:  { DB_USERNAME: 'john',
[0]   DB_PASSWORD: 'mypassword',
[0]   DB_HOST: '192.168.2.90',
[0]   DB_NAME: 'mydb',
[0]   ORM_SYNCHRONIZE: 'true',
[0]   MAIL_HOST: 'smtp.ethereal.email',
[0]   MAIL_SMTP_PORT: '587',
[0]   MAIL_AUTH_USER: 'myuser@ethereal.email',
[0]   MAIL_AUTH_PASS: 'myetherealpass',
[0]   ADMIN_USER_EMAIL: 'admin@test.com',
[0]   ADMIN_EMAIL_FROM: '<no-reply@test.com>',
[0]   JWT_SECRET: 'hellotheresecret',
[0]   JWT_ACTIVATE_EXPIRATION_MINUTES: '10',
[0]   JWT_LOGIN_EXPIRATION_MINUTES: '4320',
[0]   REDIS_HOST: '192.168.2.90',
[0]   REDIS_PORT: '6379',
[0]   REDIS_CONNECT_TIMEOUT: '1000'}
[0] >> loading schema
[0] 2019-07-12T16:27:37.011Z cfg > Loaded 18 configuration spec keys.
[0] 2019-07-12T16:27:37.012Z cfg > updatedConfig (after cascade):  { DB_USERNAME: 'john',
[0]   DB_PASSWORD: 'mypassword',
[0]   DB_HOST: '192.168.2.90',
[0]   DB_NAME: 'mydb',
[0]   ORM_SYNCHRONIZE: 'true',
[0]   MAIL_HOST: 'smtp.ethereal.email',
[0]   MAIL_SMTP_PORT: '587',
[0]   MAIL_AUTH_USER: 'myuser@ethereal.email',
[0]   MAIL_AUTH_PASS: 'myetherealpass',
[0]   ADMIN_USER_EMAIL: 'admin@test.com',
[0]   ADMIN_EMAIL_FROM: '<no-reply@test.com>',
[0]   JWT_SECRET: 'hellotheresecret',
[0]   JWT_ACTIVATE_EXPIRATION_MINUTES: '10',
[0]   JWT_LOGIN_EXPIRATION_MINUTES: '4320',
[0]   REDIS_HOST: '192.168.2.90',
[0]   REDIS_PORT: '6379',
[0]   REDIS_CONNECT_TIMEOUT: '1000',
[0]   DB_PORT: 5432 }

The parsed config shows the configuration read in from your .env file.

The updatedConfig (after cascade) shows how your final set of environment variables has been computed after applying appropriate options, and default values from the schema. In the example above, we can deduce that a default value has been applied to DB_PORT because it does not appear in the parsed config, but does appear in the updatedConfig. We'll be able to see this explicitly when using the debug switch trace as shown below.

Switches

There are two switches available to control the Debug output:

  • cfg - turns on basic debug log output, as shown above
  • trace - displays the resolveMap as shown in the trace API

You enable Debug modes by setting switches. To turn on a switch, set it in the environment like this:

turn on the cfg switch (linux)

$ export DEBUG=cfg

Warning: Be mindful of setting this switch in production. It will log your environment variables -- including secrets. As a measure of protection, the portion of the output that displays environment variable values is suppressed if the current environment is 'production'. Note, however, that this assumes you follow the convention of setting the current environment to the value 'production' and that if you use a different value, this protection will not apply. In that case, be sure to disable the cfg switch yourself.

If you do want to see a full cfg log in production (perhaps you are simulating production to debug an issue), you can override this basic protection by setting the environment variable 'TRACE_PRODUCTION' to true. When this is done, DEBUG=trace and/or DEBUG=cfg will operate normally in production.

turn on both cfg and trace switches (linux)

$ export DEBUG=cfg,trace

You can, of course, set this up in your package.json, for example:

"start:dev": "DEBUG=cfg,trace concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ",

Trace debug

Below is a sample of the output log when DEBUG=trace is set for the above configuration. See the trace API documentation for details on what this information shows you.

Warning: Be mindful of setting this switch in production. It will log your environment variables -- including secrets. As a measure of protection, this flag is automatically disabled if the current environment is 'production'. Note, however, that this assumes you follow the convention of setting the current environment to the value 'production' and that if you use a different value, this protection will not apply. In that case, be sure to disable the trace switch yourself.

If you do want to log environment variables in production (perhaps you are simulating production to debug an issue), you can override this basic protection by setting the environment variable 'TRACE_PRODUCTION' to true. When this is done, DEBUG=trace and/or DEBUG=cfg will operate in production.

resolveMap:
[0]  { DB_HOST:
[0]    { dotenv: '192.168.2.90',
[0]      env: '--',
[0]      default: 'localhost',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: '192.168.2.90' },
[0]   DB_PORT:
[0]    { dotenv: '--',
[0]      env: '--',
[0]      default: 5432,
[0]      resolvedFrom: 'default',
[0]      isExtra: false,
[0]      resolvedValue: 5432 },
[0]   DB_USERNAME:
[0]    { dotenv: 'john',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: 'john' },
[0]   DB_PASSWORD:
[0]    { dotenv: 'mypassword',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: 'mypassword' },
[0]   DB_NAME:
[0]    { dotenv: 'mydb',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: 'mydb' },
[0]   ORM_SYNCHRONIZE:
[0]    { dotenv: 'true',
[0]      env: '--',
[0]      default: true,
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: 'true' },
[0]   MAIL_HOST:
[0]    { dotenv: 'smtp.ethereal.email',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: 'smtp.ethereal.email' },
[0]   MAIL_SMTP_PORT:
[0]    { dotenv: '587',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: '587' },
[0]   MAIL_AUTH_USER:
[0]    { dotenv: 'myuser@ethereal.email',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: 'myuser@ethereal.email' },
[0]   MAIL_AUTH_PASS:
[0]    { dotenv: 'myetherealpass',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: 'myetherealpass' },
[0]   ADMIN_USER_EMAIL:
[0]    { dotenv: 'admin@test.com',
[0]      env: '--',
[0]      default: 'admin@test.com',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: 'admin@test.com' },
[0]   ADMIN_EMAIL_FROM:
[0]    { dotenv: '<no-reply@newb.com>',
[0]      env: '--',
[0]      default: '<no-reply>@newb.com',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: '<no-reply@newb.com>' },
[0]   JWT_SECRET:
[0]    { dotenv: 'hellotheresecret',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: 'hellotheresecret' },
[0]   JWT_ACTIVATE_EXPIRATION_MINUTES:
[0]    { dotenv: '10',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: '10' },
[0]   JWT_LOGIN_EXPIRATION_MINUTES:
[0]    { dotenv: '4320',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: '4320' },
[0]   REDIS_HOST:
[0]    { dotenv: '192.168.2.90',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: '192.168.2.90' },
[0]   REDIS_PORT:
[0]    { dotenv: '6379',
[0]      env: '--',
[0]      default: '--',
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: '6379' },
[0]   REDIS_CONNECT_TIMEOUT:
[0]    { dotenv: '1000',
[0]      env: '--',
[0]      default: 1000,
[0]      resolvedFrom: 'dotenv',
[0]      isExtra: false,
[0]      resolvedValue: '1000' }}