@stonyx/logs 1.0.1-beta.1 → 1.0.1-beta.10
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 +96 -57
- package/package.json +2 -2
- package/src/color.js +1 -1
- package/src/index.js +33 -4
package/README.md
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
[](https://github.com/abofs/stonyx-logs/actions/workflows/ci.yml)
|
|
2
|
+
[](https://www.npmjs.com/package/@stonyx/logs)
|
|
3
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
4
|
+
|
|
1
5
|
<h1 align="center">
|
|
2
6
|
<br>
|
|
3
7
|
<br>
|
|
4
|
-
<img width="560" src="https://github.com/abofs/stonyx-logs/raw/
|
|
8
|
+
<img width="560" src="https://github.com/abofs/stonyx-logs/raw/main/media/logo.png" alt="Stonyx Logs">
|
|
5
9
|
<br>
|
|
6
10
|
<br>
|
|
7
11
|
<br>
|
|
@@ -9,7 +13,7 @@
|
|
|
9
13
|
|
|
10
14
|
> Simplified logging for node applications
|
|
11
15
|
|
|
12
|
-

|
|
13
17
|
|
|
14
18
|
<br>
|
|
15
19
|
|
|
@@ -27,10 +31,10 @@
|
|
|
27
31
|
|
|
28
32
|
---
|
|
29
33
|
|
|
30
|
-
**
|
|
31
|
-
This project is not directly associated with chalk other than chalk being a core dependency of **
|
|
34
|
+
**Log** is built on top of all the great work done by "Sindre Sorhus" and other collaborators of the [chalk](https://www.npmjs.com/package/chalk) project.
|
|
35
|
+
This project is not directly associated with chalk other than chalk being a core dependency of **Log**.
|
|
32
36
|
|
|
33
|
-
**IMPORTANT**: Please note that although **
|
|
37
|
+
**IMPORTANT**: Please note that although **Log** can be configured to any color through chalk, your output is subject to your terminal's color limitations.
|
|
34
38
|
|
|
35
39
|
## Highlights
|
|
36
40
|
|
|
@@ -41,27 +45,27 @@ This project is not directly associated with chalk other than chalk being a core
|
|
|
41
45
|
## Install
|
|
42
46
|
|
|
43
47
|
```sh
|
|
44
|
-
npm install
|
|
48
|
+
npm install @stonyx/logs
|
|
45
49
|
```
|
|
46
50
|
|
|
47
51
|
## Usage
|
|
48
52
|
|
|
49
53
|
```js
|
|
50
|
-
import
|
|
54
|
+
import Log from '@stonyx/logs';
|
|
51
55
|
|
|
52
|
-
const
|
|
56
|
+
const log = new Log();
|
|
53
57
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
log.info('Info: sample application has started');
|
|
59
|
+
log.warn('Warning: this is just a sample');
|
|
60
|
+
log.error('Error: no application logic detected', true); // logs to logs/error.log file
|
|
57
61
|
```
|
|
58
62
|
|
|
59
63
|
Easily define your own logging mechanism and color-coding preference:
|
|
60
64
|
|
|
61
65
|
```js
|
|
62
|
-
import
|
|
66
|
+
import Log from '@stonyx/logs';
|
|
63
67
|
|
|
64
|
-
const
|
|
68
|
+
const log = new Log({
|
|
65
69
|
systemLogs: {
|
|
66
70
|
blue: '#007cae', // indigo blue
|
|
67
71
|
yellow: '#ae8f00', // bright orange
|
|
@@ -69,17 +73,17 @@ const chronicle = new Chronicle({
|
|
|
69
73
|
},
|
|
70
74
|
});
|
|
71
75
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
log.blue('Info: using custom method blue, sample application has started');
|
|
77
|
+
log.yellow('Warning: using custom method yellow, this is just a sample');
|
|
78
|
+
log.red('Error: using custom method red, no application logic detected', false);
|
|
75
79
|
```
|
|
76
80
|
|
|
77
81
|
Customize logging options to best suit your project
|
|
78
82
|
|
|
79
83
|
```js
|
|
80
|
-
import
|
|
84
|
+
import Log from '@stonyx/logs';
|
|
81
85
|
|
|
82
|
-
const
|
|
86
|
+
const log = new Log({
|
|
83
87
|
logToFileByDefault: true,
|
|
84
88
|
logTimestamp: true,
|
|
85
89
|
path: 'custom-logs', // <project root>/custom-logs/*.log
|
|
@@ -87,31 +91,31 @@ const chronicle = new Chronicle({
|
|
|
87
91
|
suffix: '\n=============================================================== \n',
|
|
88
92
|
});
|
|
89
93
|
|
|
90
|
-
|
|
94
|
+
log.info('Info: sample application has started');
|
|
91
95
|
```
|
|
92
|
-

|
|
93
97
|
|
|
94
98
|
|
|
95
99
|
Add additional log types extending the default options of "info", "warn", "error" and "debug"
|
|
96
100
|
|
|
97
101
|
```js
|
|
98
|
-
import
|
|
102
|
+
import Log from '@stonyx/logs';
|
|
99
103
|
|
|
100
|
-
const
|
|
104
|
+
const log = new Log({ additionalLogs: { question: 'green' } });
|
|
101
105
|
|
|
102
106
|
// create additional log with direct chalk configuration
|
|
103
|
-
|
|
107
|
+
log.defineType('query', log.chalk().black.bgGreen);
|
|
104
108
|
|
|
105
|
-
|
|
106
|
-
await
|
|
109
|
+
log.question('What will a fully custom chalk color function look like?');
|
|
110
|
+
await log.query('This is what a custom chalk color setting looks like', true);
|
|
107
111
|
```
|
|
108
|
-

|
|
109
113
|
|
|
110
114
|
## API
|
|
111
115
|
|
|
112
116
|
### Defining Logs & Colors
|
|
113
117
|
|
|
114
|
-
By default, **
|
|
118
|
+
By default, **Log** is instantiated with the following options:
|
|
115
119
|
|
|
116
120
|
```js
|
|
117
121
|
additionalLogs: {},
|
|
@@ -122,10 +126,10 @@ By default, **Chronicle** is instantiated with the following options:
|
|
|
122
126
|
},
|
|
123
127
|
```
|
|
124
128
|
|
|
125
|
-
You can add to a new log/color setting by passing the `additionalLogs` option to the **
|
|
129
|
+
You can add to a new log/color setting by passing the `additionalLogs` option to the **Log** constructor. Any setting that already exists in `systemLogs` will be replaced, otherwise they will be added.
|
|
126
130
|
|
|
127
131
|
```js
|
|
128
|
-
const
|
|
132
|
+
const log = new Log({ additionalLogs: { info: 'green', custom: 'cyan' } });
|
|
129
133
|
|
|
130
134
|
// output configuration:
|
|
131
135
|
{
|
|
@@ -136,13 +140,13 @@ You can add to a new log/color setting by passing the `additionalLogs` option to
|
|
|
136
140
|
}
|
|
137
141
|
```
|
|
138
142
|
|
|
139
|
-
**
|
|
143
|
+
**Log** will generate convenience methods for all keys provided, with the corresponding color settings. The example above would create the following convenience methods, for logging:
|
|
140
144
|
|
|
141
145
|
```js
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
+
log.info() // green output
|
|
147
|
+
log.warn() // yellow output
|
|
148
|
+
log.error() // red output
|
|
149
|
+
log.custom() // cyan output
|
|
146
150
|
```
|
|
147
151
|
|
|
148
152
|
These methods can then be called in your application with [logging parameters](#logging-parameters).
|
|
@@ -153,7 +157,7 @@ Additionally, these methods return a promise when `logToFile` is true, allowing
|
|
|
153
157
|
|
|
154
158
|
```js
|
|
155
159
|
async method() {
|
|
156
|
-
await
|
|
160
|
+
await log.error('error message', true);
|
|
157
161
|
|
|
158
162
|
// do something after logs/error.log (default) is created
|
|
159
163
|
}
|
|
@@ -161,7 +165,7 @@ async method() {
|
|
|
161
165
|
|
|
162
166
|
### The Debug Method
|
|
163
167
|
|
|
164
|
-
**
|
|
168
|
+
**Log** allows for the `log.debug()` method to be overridden by a color setting. However, by default we do not define a color for debug and debug is handled differently. For console logging, all **debug** does is output the following:
|
|
165
169
|
|
|
166
170
|
```js
|
|
167
171
|
// For logging to console:
|
|
@@ -171,12 +175,12 @@ console.dir(content);
|
|
|
171
175
|
JSON.stringify(content, null, 2);
|
|
172
176
|
```
|
|
173
177
|
|
|
174
|
-
We believe that when wanting to output complicated objects or debug **typescript** applications, there are better methods than utilizing this **
|
|
178
|
+
We believe that when wanting to output complicated objects or debug **typescript** applications, there are better methods than utilizing this **Log** package. But for anyone who's fully incorporated **Log** into their project, this function offers some convenience.
|
|
175
179
|
|
|
176
180
|
### Logging Parameters
|
|
177
181
|
|
|
178
182
|
```js
|
|
179
|
-
|
|
183
|
+
log.error('error message', true, false); // content, logToFile, overwrite
|
|
180
184
|
```
|
|
181
185
|
|
|
182
186
|
| Parameter | Type | Default | Description |
|
|
@@ -188,15 +192,16 @@ chronicle.error('error message', true, false); // content, logToFile, overwrite
|
|
|
188
192
|
**logToFile** will log to *<project-root>/logs* unless [configured](#configuration) differently during instantiation. <br>
|
|
189
193
|
### Configuration
|
|
190
194
|
|
|
191
|
-
When instantiating **
|
|
195
|
+
When instantiating **Log**, you can pass an object to customize your settings. Below is the default configuration:
|
|
192
196
|
|
|
193
197
|
```js
|
|
194
|
-
const
|
|
198
|
+
const log = new Log({
|
|
195
199
|
logToFileByDefault: false,
|
|
196
200
|
logTimestamp: false,
|
|
197
201
|
path: 'logs/',
|
|
198
202
|
prefix: '',
|
|
199
203
|
suffix: '',
|
|
204
|
+
filename: '',
|
|
200
205
|
additionalLogs: {},
|
|
201
206
|
systemLogs: {
|
|
202
207
|
info: 'cyan',
|
|
@@ -213,27 +218,28 @@ const chronicle = new Chronicle({
|
|
|
213
218
|
| `path` | **String** | *'logs/'* | Path in which to store log files. This setting is relative to your project's root directory. |
|
|
214
219
|
| `prefix` | **String** | *''* | Prefix string to prepend all log messages for all log types with the exception of *debug*. |
|
|
215
220
|
| `suffix` | **String** | *''* | Suffix string to tack on to all log messages for all log types with the exception of *debug*. |
|
|
221
|
+
| `filename` | **String** | *''* | Template for log file names with variable support. Defaults to `{type}.log` when empty. See [dynamic file names](#dynamic-file-names). |
|
|
216
222
|
| `additionalLogs` | **Object** | | Key value pair object containing log type to color setting for logs that will be merged with `systemLogs` |
|
|
217
|
-
| `systemLogs` | **Object** | | Key value pair object containing log type to color setting for main **
|
|
223
|
+
| `systemLogs` | **Object** | | Key value pair object containing log type to color setting for main **Log** logs available in application |
|
|
218
224
|
|
|
219
225
|
`additionalLogs` and `systemLogs` are explained with more detail in the [defining logs and colors](#defining-logs) section.
|
|
220
226
|
|
|
221
227
|
### Advanced Configuration
|
|
222
228
|
|
|
223
|
-
You may want to do more than just pick a basic color for your output. **chalk** offers a variety of different options, and can be configured via `defineType()`. **
|
|
229
|
+
You may want to do more than just pick a basic color for your output. **chalk** offers a variety of different options, and can be configured via `defineType()`. **Log** exposes the chalk instance via `chalk()` so that you don't have to import **chalk** directly into your project. Here is an example of how you can use this method to fully customize your log color setting:
|
|
224
230
|
|
|
225
231
|
```js
|
|
226
|
-
const
|
|
232
|
+
const log = new Log();
|
|
227
233
|
|
|
228
|
-
|
|
229
|
-
|
|
234
|
+
log.defineType('critical', log.chalk().bold.red);
|
|
235
|
+
log.critical('This is a critical error');
|
|
230
236
|
```
|
|
231
237
|
|
|
232
238
|
Additionally, any [configuration](#configuration) that can be set during instantiation, can also be applied exclusively to any given type by passing in a third **options** parameter.
|
|
233
239
|
|
|
234
240
|
```js
|
|
235
241
|
// params: type, setting, options
|
|
236
|
-
|
|
242
|
+
log.definetype('notice', '#c0c0c0', {
|
|
237
243
|
prefix: '--------------------------------------------------------------- \n',
|
|
238
244
|
suffix: '\n=============================================================== \n'
|
|
239
245
|
});
|
|
@@ -248,29 +254,62 @@ chronicle.definetype('notice', '#c0c0c0', {
|
|
|
248
254
|
|
|
249
255
|
|
|
250
256
|
```js
|
|
251
|
-
const
|
|
257
|
+
const log = new Log();
|
|
252
258
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
259
|
+
log.defineType('info', log.chalk().black.bgCyan);
|
|
260
|
+
log.defineType('critical', log.chalk().bold.red);
|
|
261
|
+
log.defineType('dialog', 'magentaBright');
|
|
262
|
+
log.definetype('notice', '#c0c0c0', {
|
|
257
263
|
prefix: '--------------------------------------------------------------- \n',
|
|
258
264
|
suffix: '\n=============================================================== \n'
|
|
259
265
|
});
|
|
260
266
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
267
|
+
log.info('This pre-existing log now has a cyan background and black foreground');
|
|
268
|
+
log.critical('This new log is bold and red');
|
|
269
|
+
log.dialog('This new dialog is bright magenta');
|
|
270
|
+
log.notice('This new log is the hex "#c0c0c0" share of gray');
|
|
265
271
|
```
|
|
266
272
|
|
|
267
273
|
`defineType()` can also be used as an alternative to populating the `additionalLogs` setting in the constructor, as if the setting doesn't already exist, it will then be created.
|
|
268
274
|
|
|
275
|
+
### Dynamic File Names
|
|
276
|
+
|
|
277
|
+
The `filename` option supports template variables that are resolved at write-time, allowing each log type to produce uniquely named files.
|
|
278
|
+
|
|
279
|
+
#### Supported Variables
|
|
280
|
+
|
|
281
|
+
| Variable | Resolves To | Example Output |
|
|
282
|
+
| :---: | :--- | :--- |
|
|
283
|
+
| `{date}` | Current date in YYYY-MM-DD format | `2026-04-04` |
|
|
284
|
+
| `{type}` | Log type name | `error` |
|
|
285
|
+
| `{pid}` | Current process ID | `12345` |
|
|
286
|
+
| `{hostname}` | Machine hostname | `my-server` |
|
|
287
|
+
|
|
288
|
+
#### Examples
|
|
289
|
+
|
|
290
|
+
```js
|
|
291
|
+
// Per-type filename via defineType
|
|
292
|
+
log.defineType('error', 'red', { filename: 'error-{date}.log' });
|
|
293
|
+
// writes to: logs/error-2026-04-04.log
|
|
294
|
+
|
|
295
|
+
// Per-type filename with multiple variables
|
|
296
|
+
log.defineType('info', 'cyan', { filename: '{type}-{hostname}-{date}.log' });
|
|
297
|
+
// writes to: logs/info-my-server-2026-04-04.log
|
|
298
|
+
|
|
299
|
+
// Global filename template via constructor
|
|
300
|
+
const log = new Log({ filename: '{type}-{date}.log' });
|
|
301
|
+
// all types write to: logs/<type>-2026-04-04.log
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
When no `filename` is configured, the default behavior of `{type}.log` is preserved for full backward compatibility.
|
|
305
|
+
|
|
306
|
+
Path traversal characters (`..`, `/`, `\`) are automatically stripped from resolved file names for security.
|
|
307
|
+
|
|
269
308
|
## Origin
|
|
270
309
|
|
|
271
|
-
As a team of developers who are constantly working on side projects, we often litter our codebase with TODOs to refactor convenience utils such as **
|
|
310
|
+
As a team of developers who are constantly working on side projects, we often litter our codebase with TODOs to refactor convenience utils such as **@stonyx/logs** into classes of their own, or projects of their own. This usually turns into internal tech debt that never gets addressed. Furthermore, we also often find ourselves going the *copy -> paste -> modify* route of previously written useful logic, which saves us time in new projects, but not as much as it would if all we had to do was run an `npm install` instead.
|
|
272
311
|
|
|
273
|
-
With that in mind, we are proud to release **
|
|
312
|
+
With that in mind, we are proud to release **@stonyx/logs** as an open source package, in hopes others will find this just as useful as we do in their own projects.
|
|
274
313
|
|
|
275
314
|
## Maintainers
|
|
276
315
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stonyx/logs",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.10",
|
|
4
4
|
"description": "Simplified logging for node applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"log",
|
|
25
25
|
"logging",
|
|
26
26
|
"color-coding",
|
|
27
|
-
"
|
|
27
|
+
"stonyx",
|
|
28
28
|
"history",
|
|
29
29
|
"documentation",
|
|
30
30
|
"document",
|
package/src/color.js
CHANGED
|
@@ -22,7 +22,7 @@ export default class Color {
|
|
|
22
22
|
// retrieves chalk color function, and fully validates output
|
|
23
23
|
settingToChalkColorFunction(setting) {
|
|
24
24
|
const errorMessage = 'Invalid chalk color function.'
|
|
25
|
-
+ 'For help with color settings, see https://github.com/abofs/
|
|
25
|
+
+ 'For help with color settings, see https://github.com/abofs/stonyx-logs#defining-logs--colors';
|
|
26
26
|
|
|
27
27
|
switch (typeof setting) {
|
|
28
28
|
case 'string':
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { mkdirSync, promises as fsp } from 'fs';
|
|
2
2
|
import { fileURLToPath } from 'url';
|
|
3
|
+
import { hostname } from 'os';
|
|
3
4
|
import projectPath from 'path';
|
|
4
5
|
import Color from './color.js';
|
|
5
6
|
|
|
@@ -9,6 +10,7 @@ const defaultOptions = {
|
|
|
9
10
|
path: 'logs/', // default log directory (relative to main project root directory)
|
|
10
11
|
prefix: '',
|
|
11
12
|
suffix: '',
|
|
13
|
+
filename: '', // template for log file name (e.g. 'error-{date}.log'), defaults to '{type}.log'
|
|
12
14
|
|
|
13
15
|
// log types with corresponding color settings
|
|
14
16
|
additionalLogs: {},
|
|
@@ -22,7 +24,7 @@ const defaultOptions = {
|
|
|
22
24
|
// used to sanitize defineType() options input
|
|
23
25
|
const optionKeys = Object.keys(defaultOptions);
|
|
24
26
|
|
|
25
|
-
export default class
|
|
27
|
+
export default class Log {
|
|
26
28
|
constructor(options = defaultOptions) {
|
|
27
29
|
options = {
|
|
28
30
|
...defaultOptions,
|
|
@@ -46,7 +48,7 @@ export default class Chronicle {
|
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
// records setting and options for log type, and
|
|
51
|
+
// records setting and options for log type, and creates convenience method ie: log.info()
|
|
50
52
|
defineType(type, setting, options = null) {
|
|
51
53
|
this.color.setLogColor(type, setting);
|
|
52
54
|
|
|
@@ -59,7 +61,7 @@ export default class Chronicle {
|
|
|
59
61
|
for (let option of Object.keys(options)) {
|
|
60
62
|
if (!optionKeys.includes(option)) {
|
|
61
63
|
throw `${option} is not a valid configuration object.`
|
|
62
|
-
+ '\n For a list of available options, see https://github.com/abofs/
|
|
64
|
+
+ '\n For a list of available options, see https://github.com/abofs/stonyx-logs#configuration';
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
// sanitize path input
|
|
@@ -129,7 +131,9 @@ export default class Chronicle {
|
|
|
129
131
|
|
|
130
132
|
async writeToFile(type, content, overwrite) {
|
|
131
133
|
const path = this.getOptionForType(type, 'path');
|
|
132
|
-
const
|
|
134
|
+
const filenameTemplate = this.getOptionForType(type, 'filename');
|
|
135
|
+
const resolvedName = this.resolveFilename(filenameTemplate, type);
|
|
136
|
+
const targetLog = `${path}${resolvedName}`;
|
|
133
137
|
await this.validateFileAndDirectory(path, targetLog);
|
|
134
138
|
|
|
135
139
|
const fileAction = overwrite ? fsp.writeFile : fsp.appendFile;
|
|
@@ -137,6 +141,31 @@ export default class Chronicle {
|
|
|
137
141
|
return fileAction(targetLog, content);
|
|
138
142
|
}
|
|
139
143
|
|
|
144
|
+
// resolves template variables in a filename string
|
|
145
|
+
resolveFilename(template, type) {
|
|
146
|
+
// default to '{type}.log' when no template is configured
|
|
147
|
+
if (!template) return `${type}.log`;
|
|
148
|
+
|
|
149
|
+
const now = new Date();
|
|
150
|
+
const yyyy = now.getFullYear();
|
|
151
|
+
const mm = String(now.getMonth() + 1).padStart(2, '0');
|
|
152
|
+
const dd = String(now.getDate()).padStart(2, '0');
|
|
153
|
+
|
|
154
|
+
const variables = {
|
|
155
|
+
date: `${yyyy}-${mm}-${dd}`,
|
|
156
|
+
type,
|
|
157
|
+
pid: process.pid,
|
|
158
|
+
hostname: hostname(),
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const resolved = template.replace(/\{(\w+)\}/g, (match, key) => {
|
|
162
|
+
return variables[key] !== undefined ? variables[key] : match;
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// sanitize: prevent path traversal and disallow directory separators
|
|
166
|
+
return resolved.replace(/\.\./g, '').replace(/[/\\]/g, '');
|
|
167
|
+
}
|
|
168
|
+
|
|
140
169
|
// attempts to create file and/or directory if they don't already exist
|
|
141
170
|
async validateFileAndDirectory(path, targetLog) {
|
|
142
171
|
const errorMethod = this.error || console.error; // prefer native method unless removed by user
|