@holz/env-filter 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# `@holz/env-filter`
|
|
2
|
+
|
|
3
|
+
Builds on [`@holz/pattern-filter`](https://github.com/PsychoLlama/holz/tree/main/packages/holz-pattern-filter) to filter logs based on your current environment, loading the pattern from `process.env.DEBUG` or `localStorage.debug`.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { createEnvironmentFilter } from '@holz/env-filter';
|
|
9
|
+
import { createLogger } from '@holz/core';
|
|
10
|
+
|
|
11
|
+
createLogger(
|
|
12
|
+
createEnvironmentFilter({
|
|
13
|
+
processor: plugin,
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Configuration Options
|
|
19
|
+
|
|
20
|
+
`createEnvironmentFilter()` accepts the following configuration options:
|
|
21
|
+
|
|
22
|
+
- `processor`: The log processor to use. Required.
|
|
23
|
+
- `pattern`: The pattern to use for filtering logs. If not provided, the pattern will be loaded from the environment.
|
|
24
|
+
- `defaultPattern`: The default pattern to use if no pattern is provided or detected in the environment. Default is `'*'` (all logs).
|
|
25
|
+
- `localStorageKey`: The name of the key used to store the pattern in localStorage. Default is `'debug'`.
|
|
26
|
+
- `environmentVariable`: The name of the environment variable used to store the pattern. Default is `'DEBUG'`.
|
|
27
|
+
|
|
28
|
+
Note that the `pattern` option will override any pattern detected in the environment.
|
|
29
|
+
|
|
30
|
+
## How it Works
|
|
31
|
+
|
|
32
|
+
`createEnvironmentFilter()` loads the pattern from the environment using the following process:
|
|
33
|
+
|
|
34
|
+
- It looks for a pattern stored in `localStorage` with the key specified in `localStorageKey`. If found, it uses that pattern.
|
|
35
|
+
- If `localStorage` is not available or doesn't contain a pattern with the specified key, it looks for a pattern stored in `process.env` specified in `environmentVariable`. If found, it uses that pattern.
|
|
36
|
+
- If no pattern is found in either `localStorage` or the environment variable, it uses the default pattern specified in `defaultPattern`.
|
|
37
|
+
|
|
38
|
+
Once the pattern is loaded, `createEnvironmentFilter()` uses `createPatternFilter()` from [`@holz/pattern-filter`](https://github.com/PsychoLlama/holz/tree/main/packages/holz-pattern-filter) to create a log processor that filters logs based on the loaded pattern.
|
|
39
|
+
|
|
40
|
+
## Example
|
|
41
|
+
|
|
42
|
+
Suppose you want to filter logs in development that come from your app, but disable all logs in production. You can use `@holz/env-filter` to accomplish this by setting the `DEBUG` environment variable in development:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
DEBUG=your-app*
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
In production, you may want to disable all logging by default. This can be done by setting the default pattern to an empty string:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { createEnvironmentFilter } from '@holz/env-filter';
|
|
52
|
+
import { createLogger } from '@holz/core';
|
|
53
|
+
|
|
54
|
+
const logger = createLogger(
|
|
55
|
+
createEnvironmentFilter({
|
|
56
|
+
processor: yourProcessor,
|
|
57
|
+
defaultPattern: '',
|
|
58
|
+
})
|
|
59
|
+
);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
With this configuration, logs will be disabled by default in production, and only logs originating from the `your-app` namespace will be shown in development.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/env-filter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Filter logs based on the current environment.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/holz-env-filter.cjs",
|
|
@@ -40,10 +40,10 @@
|
|
|
40
40
|
"test:types": "tsc"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@holz/pattern-filter": "^0.
|
|
43
|
+
"@holz/pattern-filter": "^0.6.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@holz/core": "^0.
|
|
46
|
+
"@holz/core": "^0.6.0",
|
|
47
47
|
"@types/node": "^18.14.0",
|
|
48
48
|
"@vitest/coverage-c8": "^0.28.5",
|
|
49
49
|
"vite": "^4.0.0",
|