|
1 | | -A Simple Log Facade for JavaScript |
2 | | -=================================== |
| 1 | +An Extensible Wrapper for Node Config |
| 2 | +===================================== |
3 | 3 |
|
4 | | -[](https://nodei.co/npm/@alt-javascript/logger/) |
| 4 | +[](https://nodei.co/npm/@alt-javascript/config/) |
5 | 5 | <br/> |
6 | | - |
7 | | - <br/> |
8 | | -[release notes](https://github.com/craigparra/alt-logger/blob/main/History.md) |
| 6 | + |
| 7 | + <br/> |
| 8 | +[release notes](https://github.com/craigparra/alt-config/blob/main/History.md) |
9 | 9 |
|
10 | 10 | <a name="intro">Introduction</a> |
11 | 11 | -------------------------------- |
12 | | -A simple configurable logging facade for javascript, using the popular [config](https://www.npmjs.com/package/config) |
13 | | -package interface. |
| 12 | +An extensible wrapper of the popular config package, supporting placeholder resolution, encrypted |
| 13 | +and default (fallback) values. |
14 | 14 |
|
15 | 15 | <a name="usage">Usage</a> |
16 | 16 | ------------------------- |
17 | 17 |
|
18 | | -To use the module, import the LoggerFactory and call the `getLogger` function with a logging category (your module |
19 | | -requires path is a sensible choice). |
| 18 | +To use the module, simply import the substituted package as you would with the popular |
| 19 | +[config](https://www.npmjs.com/package/config) package |
20 | 20 |
|
21 | 21 | ```javascript |
22 | | -const {LoggerFactory} = require('@alt-javascript/logger'); |
23 | | -const logger = LoggerFactory.getLogger('@myorg/mypackage/MyModule'); |
| 22 | +const config = require('@alt-javascript/config'); |
24 | 23 |
|
25 | | -logger.info('Hello world!'); |
| 24 | +config.get('key'); |
| 25 | +config.get('nested.key'); |
| 26 | +config.get('unknown','use this instead'); // this does not throw an error |
26 | 27 | ``` |
27 | | -The LoggerFactory will create a ConsoleLogger (uses `console.log('...')`) object instance, with the root logging level |
28 | | -set to `info` by default. To change the logging level for your module (category), add something similar to the |
29 | | -following in your [config](https://www.npmjs.com/package/config) files. |
| 28 | + |
| 29 | +Config values that include the common `${placeholder}` syntax, will resolve the inline |
| 30 | +placeholders, so the `config.get('placeholder')'` path below will return `start.one.two.end`. |
| 31 | + |
| 32 | +Config values that start with the prefix `enc.` will be decrypted with the |
| 33 | +[jasypt](https://www.npmjs.com/package/jasypt) package port, with the passphrase being |
| 34 | +sourced from the `process.env.NODE_CONFIG_PASSPHRASE` environment variable. |
30 | 35 |
|
31 | 36 | `local-development.json` |
32 | 37 | ```json |
33 | 38 | { |
34 | | - "logger" : { |
35 | | - "level" : { |
36 | | - "/" : "info", |
37 | | - "@myorg/mypackage/MyModule" : "debug" |
38 | | - } |
| 39 | + "key": "value", |
| 40 | + "one" : "one", |
| 41 | + "placeholder": "start.${one}.${nested.two}.end", |
| 42 | + "placeholderEncrypted": "start.${nested.encrypted}.end", |
| 43 | + "nested" : { |
| 44 | + "key" : "value", |
| 45 | + "two" : "two", |
| 46 | + "placeholder": "start.${one}.${nested.two}.end", |
| 47 | + "encrypted" : "enc.pxQ6z9s/LRpGB+4ddJ8bsq8RqELmhVU2", |
| 48 | + "encryptedWithSecret" : "enc./emLGkD3cbfqoSPijGZ0jh1p1SYIHQeJ" |
39 | 49 | } |
40 | 50 | } |
41 | 51 | ``` |
42 | 52 |
|
43 | | -The logger supports the following levels by default, but is fully configurable. |
44 | | - |
45 | | -```javascript |
46 | | -{ |
47 | | - ENUMS: { |
48 | | - fatal: 0, error: 1, warn: 2, info: 3, verbose: 4, debug: 5, |
49 | | - }, |
50 | | - DEBUG: 'debug', |
51 | | - VERBOSE: 'verbose', |
52 | | - INFO: 'info', |
53 | | - WARN: 'warn', |
54 | | - ERROR: 'error', |
55 | | - FATAL: 'fatal', |
56 | | -} |
57 | | -``` |
58 | | - |
59 | | -You can test if a level is enabled for your module (category), for example. |
60 | | - |
61 | | -```javascript |
62 | | -if (logger.isDebugEnabled()){ |
63 | | - logger.debug(`This a performance impacting logline => ${costlyFunction()}`) |
64 | | -} |
65 | | -``` |
66 | | -<a name="conf">Configuring</a> |
67 | | ------------------------------- |
68 | | - |
69 | | -While the LoggerFactory uses sensible defaults, it is flexible and pluggable. To use the popular |
70 | | -[winston](https://www.npmjs.com/package/winston) package you can pass a `WinstonLogger` provider to the `getLogger` |
71 | | -function, passing it your winston options (nullish options will fall back to defaults). |
72 | | - |
73 | | ->Be aware [winston](https://www.npmjs.com/package/winston) is not included as a package dependency for |
74 | | -> [@alt-javascript/logger](https://www.npmjs.com/package/@alt-javascript/logger) to keep it light-weight. |
75 | | - |
76 | | - |
77 | | -```javascript |
78 | | -const {LoggerFactory,WinstonLogger} = require('@alt-javascript/logger'); |
79 | | -const logger = LoggerFactory.getLogger('@myorg/mypackage/MyModule', new WinstonLogger({/*mywinstonoptions*/})); |
80 | | - |
81 | | -logger.info('Hello world!'); |
82 | | -``` |
83 | | - |
84 | | -The `ConsoleLogger` uses a JSONFormatter, but a PlainTextFormatter (or similar implementation) can easily be |
85 | | -substituted. |
86 | | - |
87 | | -```javascript |
88 | | -const {LoggerFactory,WinstonLogger} = require('@alt-javascript/logger'); |
89 | | -const logger = LoggerFactory.getLogger('@myorg/mypackage/MyModule', new ConsoleLogger('@myorg/mypackage/MyModule',new PlainTextFromatter())); |
90 | | - |
91 | | -logger.info('Hello world!'); |
92 | | -``` |
93 | | - |
94 | 53 | <a name="testing">Testability</a> |
95 | 54 | ------------------------- |
96 | 55 |
|
97 | | -Testing loggers is hard, and testability is a first class concern at @alt-javascript so the logging facade, |
98 | | -and the module exports an EphemeralLogger and EphemeralLogSink that will capture log lines that can be asserted. |
| 56 | +Testing config is hard, and testability is a first class concern at @alt-javascript so the config wrapper, |
| 57 | +and the module exports an EphemeralConfig that can source config paths from a plain old javascript |
| 58 | +object as follows, allowing you to assert varying configurations easily |
99 | 59 |
|
100 | 60 | ```javascript |
101 | | -const {LoggerFactory,WinstonLogger} = require('@alt-javascript/logger'); |
102 | | -const ephemeralLogger = new EphemeralLogger('@myorg/mypackage/MyModule'); |
103 | | -const logger = LoggerFactory.getLogger('@myorg/mypackage/MyModule', ephemeralLogger); |
104 | | - |
105 | | -logger.info('Hello world!'); |
| 61 | +const { |
| 62 | + EphemeralConfig, ValueResolvingConfig, PlaceHolderResolver, PlaceHolderSelector |
| 63 | + |
| 64 | +} = require('@alt-javascript/config'); |
106 | 65 |
|
107 | | -//... |
| 66 | +const ephemeralConfig = new EphemeralConfig({ |
| 67 | + key: 'value', |
| 68 | + nested: { |
| 69 | + key: 'value', |
| 70 | + }, |
| 71 | +}); |
108 | 72 |
|
109 | | -assert.isTrue(ephemeralLogger.sink.lines[0].contains('Hello world!')) |
| 73 | +const placeHolderResolver = new PlaceHolderResolver(new PlaceHolderSelector()); |
| 74 | +const config = new ValueResolvingConfig(ephemeralConfig,placeHolderResolver ); |
110 | 75 | ``` |
111 | 76 |
|
112 | 77 | <a name="license">License</a> |
|
0 commit comments