vocascan / vocascan-server

Server for Vocascan
https://vocascan.com
Apache License 2.0
26 stars 5 forks source link

Feature/config logger #61

Closed luwol03 closed 2 years ago

luwol03 commented 2 years ago
Status Type Env Vars Change
:white_check_mark: Ready Feature/Refactor Yes

Description

This PR introduces two new things:

1. Config

There are two new ways to configure vocascan.

1.1 config file

You can add a env variable called VOCASCAN_CONFIG to set path to your config. The file could either be a json or a js file. Later (in another pr, when the cli will be introduced) you can also transmit the path via cli arguments. (If VOCASCAN_CONFIG is not set, vocasan search for a file called vocascan.config.js or vocascan.config.json in the vocascan root directory)

module.exports = {
  debug: false,

  server: {
    port: 5000,
    jwt_secret: 'abc',
    salt_rounds: 10,
  },
}

1.2 env variables

You can also set all options available via the config file also via env variables. To achieve this vocascan parses all env variables starting with VOCASCAN__. Then you can use two underlines to replicate the paths in the config file. For example, the above config example in env variables would be the following.

VOCASCAN__DEBUG=false
VOCASCAN__SERVER__PORT=5000
VOCASCAN__SERVER__JWT_SECRET=abc
VOCASCAN__SERVER__SALT_ROUNDS=10

(The capitalization in the env names doesn't matter, because later everything is converted to lowercase anyway)

And of course the old env variables still work, but result in a warning that this type of configuration is deprecated. image

2. Logger

You can define different logger transports.

module.exports = {
  log: {
    console: {
      level: "debug",
      enable_router_log: true,
      stderr_levels: ['error'],
    },
    myCustomLogger: {
      mode: "file",
      filename: "./logs/vocascan.log",
      enable_sql_log: true,
      enable_router_log: true,
      max_size: 100000,
      max_files: 3,
      archive_logs: true,
    },
    myCustomLogger: {
      mode: "console", // shouldn't be necessary, because console is the default
      stderr_levels: ["error", "warn"],
      colorize: true,
      handle_exceptions: false,
    }

This config creates two loggers. One named console. And one named myCustomLogger. And as you noticed, the logger called console has no mode property, because If you use a mode as a logger name, the mode automatically stick to the name. So if a logger is called file the mode is automatically file. Currently there are only two types of modes. console and file. After defining the loggers, you can configure them. With enable_default_log, enable_sql_log and enable_router_log you can define if you want to log default log (all errors and general log), sql queries and router request logs. And thanks to the self-written small template engiene it is even possible to define the output format of the log message. For all available options see the config joi schema until the documentation is not written.

In this example Ill only used the config file version. But the env version also works fine. Just a little example how to define the myCustomLogger2 from above:

VOCASCAN__MYCUSTOMLOGGER2__MODE=console
VOCASCAN__MYCUSTOMLOGGER2__STDERR_LEVELS=error,warn
VOCASCAN__MYCUSTOMLOGGER2__COLORIZE=true
VOCASCAN__MYCUSTOMLOGGER2__HANDLE_EXCEPTIONS=false

As I described above, following things are available:

Modes (Transports)
Levels

So if you set the level property to warn, only warn and error levels will be logged. If level is debug, debug and all above will be logged.

Motivation and Context

I worked on the config parsing and logging to laying the foundation stones for the vocascan-server cli which I working on and to get rid of the things that have been bothering me for a long time.

Checklist

Resolves

luwol03 commented 2 years ago

I added a first draft for the documentation in vocascan/documentation#3.