Skip to content

Commit 2e5e45e

Browse files
author
craig
committed
@alt-javascript/config README.md
1 parent 5e9415b commit 2e5e45e

File tree

1 file changed

+46
-81
lines changed

1 file changed

+46
-81
lines changed

README.md

Lines changed: 46 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,77 @@
1-
A Simple Log Facade for JavaScript
2-
===================================
1+
An Extensible Wrapper for Node Config
2+
=====================================
33

4-
[![NPM](https://nodei.co/npm/@alt-javascript/logger.svg?downloads=true&downloadRank=true)](https://nodei.co/npm/@alt-javascript/logger/)
4+
[![NPM](https://nodei.co/npm/@alt-javascript/config.svg?downloads=true&downloadRank=true)](https://nodei.co/npm/@alt-javascript/config/)
55
<br/>
6-
![Language Badge](https://img.shields.io/github/languages/top/craigparra/alt-logger)
7-
![Package Badge](https://img.shields.io/npm/v/@alt-javascript/logger) <br/>
8-
[release notes](https://github.com/craigparra/alt-logger/blob/main/History.md)
6+
![Language Badge](https://img.shields.io/github/languages/top/craigparra/alt-config)
7+
![Package Badge](https://img.shields.io/npm/v/@alt-javascript/config) <br/>
8+
[release notes](https://github.com/craigparra/alt-config/blob/main/History.md)
99

1010
<a name="intro">Introduction</a>
1111
--------------------------------
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.
1414

1515
<a name="usage">Usage</a>
1616
-------------------------
1717

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
2020

2121
```javascript
22-
const {LoggerFactory} = require('@alt-javascript/logger');
23-
const logger = LoggerFactory.getLogger('@myorg/mypackage/MyModule');
22+
const config = require('@alt-javascript/config');
2423

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
2627
```
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.
3035

3136
`local-development.json`
3237
```json
3338
{
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"
3949
}
4050
}
4151
```
4252

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-
9453
<a name="testing">Testability</a>
9554
-------------------------
9655

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
9959

10060
```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');
10665

107-
//...
66+
const ephemeralConfig = new EphemeralConfig({
67+
key: 'value',
68+
nested: {
69+
key: 'value',
70+
},
71+
});
10872

109-
assert.isTrue(ephemeralLogger.sink.lines[0].contains('Hello world!'))
73+
const placeHolderResolver = new PlaceHolderResolver(new PlaceHolderSelector());
74+
const config = new ValueResolvingConfig(ephemeralConfig,placeHolderResolver );
11075
```
11176

11277
<a name="license">License</a>

0 commit comments

Comments
 (0)