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)
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.
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:
As I described above, following things are available:
Modes (Transports)
console
file
Levels
error
warn
info
verbose
debug
silly
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
[x] I have read the CONTRIBUTING document.
[x] I have considered the accessibility of my changes (i.e. did I add proper content descriptions to images, or run my changes with talkback enabled?)
[x] I have documented my code if needed
Todo
[x] example config
[x] test file and json logger
[x] use file and console name as mode
[x] use logger for server listening message
[x] use morgan to parse tokens
[x] add router log
[x] own template parser
[x] use response time instead of total time
[x] change NullTransport
[x] remove all keys from json in router mode
[x] uncaught/rejected error handler winston
[x] add env compatibility
[x] add default config path
[x] fix docker-compose.yml due to new config approach
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. (IfVOCASCAN_CONFIG
is not set, vocasan search for a file calledvocascan.config.js
orvocascan.config.json
in the vocascan root directory)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.(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.
2. Logger
You can define different logger transports.
This config creates two loggers. One named
console
. And one namedmyCustomLogger
. 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 calledfile
the mode is automaticallyfile
. Currently there are only two types of modes.console
andfile
. After defining the loggers, you can configure them. Withenable_default_log
,enable_sql_log
andenable_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:As I described above, following things are available:
Modes (Transports)
console
file
Levels
error
warn
info
verbose
debug
silly
So if you set the
level
property towarn
, onlywarn
anderror
levels will be logged. If level isdebug
, 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
Todo
docker-compose.yml
due to new config approachResolves