mentie 0.3.5 → 0.3.7
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 +11 -2
- package/modules/environment.js +18 -0
- package/modules/logging.js +9 -4
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -4,6 +4,15 @@ Mentor's favorite helpers.
|
|
|
4
4
|
|
|
5
5
|
## Logging
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Logging options:
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- `LOGLEVEL`/`?loglevel=`: Set the log level. Options:
|
|
10
|
+
- `info`: Keeps `log.info()`, `log.warn()`, `log.error()` visible
|
|
11
|
+
- `warn`: Keeps `log.warn()`, `log.error()` visible
|
|
12
|
+
- `error`: Keeps only `log.error()` visible
|
|
13
|
+
- Note that `log.warn()` adds a ⚠️ emoji and `log.error()` adds a 🚨 emoji.
|
|
14
|
+
- Note that `log()` only logs in development mode (detected in environment or through `NODE_ENV`/`?debug=true`).
|
|
15
|
+
- `LOG_ANNOTATIONS`/`?log_annotations`: Comma separates list of annotations. Options:
|
|
16
|
+
- `timestamp`: Add `Date.now()` to the log message.
|
|
17
|
+
- `isotime`: Add `new Date().toISOString()` to the log message.
|
|
18
|
+
- `stringify`: Stringifies log messages (useful in environments where variables references might we deleted before you inspect them)
|
package/modules/environment.js
CHANGED
|
@@ -77,6 +77,24 @@ env.node_loglevel = () => env.is_node() && process.env?.LOG_LEVEL
|
|
|
77
77
|
*/
|
|
78
78
|
env.loglevel = () => env.web_loglevel() || env.node_loglevel() || env.dev() ? 'info' : 'error'
|
|
79
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Retrieves the log annotations set via URL parameters in a web environment.
|
|
82
|
+
* @returns {Array<string>|boolean} An array of log annotations from URL parameters, or false if not set.
|
|
83
|
+
*/
|
|
84
|
+
env.web_log_annotations = () => env.is_web() && new URLSearchParams( location?.search ).get( 'log_annotations' ).split( ',' ).filter( Boolean ).map( annotation => annotation.trim() )
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Retrieves the log annotations set via environment variables in a Node.js environment.
|
|
88
|
+
* @returns {Array<string>|boolean} An array of log annotations from environment variables, or false if not set.
|
|
89
|
+
*/
|
|
90
|
+
env.node_log_annotations = () => env.is_node() && process.env?.LOG_ANNOTATIONS?.split( ',' ).filter( Boolean ).map( annotation => annotation.trim() )
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Retrieves the effective log annotations based on the environment. Defaults to an empty array if not set.
|
|
94
|
+
* @returns {Array<string>|boolean} An array of log annotations, or false if not set.
|
|
95
|
+
*/
|
|
96
|
+
env.log_annotations = () => env.web_log_annotations() || env.node_log_annotations() || []
|
|
97
|
+
|
|
80
98
|
/**
|
|
81
99
|
* Checks if the code is running in a web browser and the platform is Mac.
|
|
82
100
|
* @returns {boolean} True if the code is running in a web browser and the platform is Mac, otherwise false.
|
package/modules/logging.js
CHANGED
|
@@ -32,9 +32,8 @@ const add_trace = messages => {
|
|
|
32
32
|
// Do nothing if we are not in a browser
|
|
33
33
|
if( typeof window === 'undefined' ) return messages
|
|
34
34
|
|
|
35
|
-
// If there is no trace=true in the url,
|
|
36
|
-
if( !
|
|
37
|
-
|
|
35
|
+
// If there is no trace=true in the url, return
|
|
36
|
+
if( !new URLSearchParams( location?.search ).get( 'trace' ) ) return messages
|
|
38
37
|
|
|
39
38
|
// Get the stack trace
|
|
40
39
|
let { stack } = new Error()
|
|
@@ -65,9 +64,14 @@ const add_trace = messages => {
|
|
|
65
64
|
*/
|
|
66
65
|
const annotate_messages = messages => {
|
|
67
66
|
|
|
67
|
+
// Add annotations if requested
|
|
68
|
+
const annotations = env.log_annotations()
|
|
69
|
+
if( annotations.includes( 'timestamp' ) ) messages = [ Date.now(), ...messages ]
|
|
70
|
+
if( annotations.includes( 'isotime' ) ) messages = [ new Date().toISOString(), ...messages ]
|
|
71
|
+
|
|
68
72
|
|
|
69
73
|
// If we are running in cypress, stringify the messages because they become unavailable in the console
|
|
70
|
-
if( env.is_cypress() ) {
|
|
74
|
+
if( env.is_cypress() || annotations.includes( 'stringify' ) ) {
|
|
71
75
|
|
|
72
76
|
try {
|
|
73
77
|
messages = messages.map( message => stringify( message, null, 2 ) )
|
|
@@ -76,6 +80,7 @@ const annotate_messages = messages => {
|
|
|
76
80
|
}
|
|
77
81
|
}
|
|
78
82
|
|
|
83
|
+
|
|
79
84
|
// Annotate the provided messages
|
|
80
85
|
messages = add_trace( messages )
|
|
81
86
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mentie",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
4
4
|
"description": "Mentor's toolbelt",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
"husky": "^9.1.3"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
+
"hash.js": "^1.1.7",
|
|
31
32
|
"promise-parallel-throttle": "^3.5.0",
|
|
32
|
-
"promise-retry": "^2.0.1"
|
|
33
|
-
"hash.js": "^1.1.7"
|
|
33
|
+
"promise-retry": "^2.0.1"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"safe-stable-stringify": "^2.4.3"
|