@fractal-solutions/qflow 1.0.54 → 1.0.55

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.
Files changed (2) hide show
  1. package/README.md +174 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -102,6 +102,9 @@ You can attach listeners to an `AsyncFlow` instance to subscribe to key lifecycl
102
102
  * **Payload:** `{ flowId, nodeClass, endTime, duration, status, result, error }`
103
103
 
104
104
 
105
+
106
+
107
+
105
108
  ## Basic Usage & Examples
106
109
 
107
110
  ### 1. Simple Node
@@ -371,6 +374,177 @@ await myFlow.runAsync({});
371
374
  ```
372
375
 
373
376
 
377
+
378
+
379
+
380
+ ## Logging
381
+
382
+ `qflow` provides a simple yet powerful logging mechanism that gives you per-node control over how and where log messages are sent.
383
+
384
+ ### Basic Usage
385
+
386
+ By default, all built-in nodes will log to the console. You don't need to configure anything for this to work.
387
+
388
+ ### Per-Node Logging Configuration
389
+
390
+ To customize the logging for a specific node, you can pass a `logging` object in its parameters. This gives you fine-grained control over the logging behavior of each node in your flow.
391
+
392
+ ```javascript
393
+ import { HttpRequestNode } from '@fractal-solutions/qflow/nodes';
394
+
395
+ const httpNode = new HttpRequestNode();
396
+
397
+ // This node will log to the console by default
398
+ httpNode.setParams({
399
+ url: 'https://api.example.com/data'
400
+ });
401
+
402
+ // This node will log to a file
403
+ const anotherHttpNode = new HttpRequestNode();
404
+ anotherHttpNode.setParams({
405
+ url: 'https://api.example.com/other-data',
406
+ logging: {
407
+ method: 'file',
408
+ params: { filePath: './logs/http.log' }
409
+ }
410
+ });
411
+ ```
412
+
413
+ ### Logging Methods
414
+
415
+ The following logging methods are available out of the box:
416
+
417
+ * **`console` (default):** Logs messages to the console.
418
+ * **`file`:** Appends log messages to a file.
419
+ * **params:** `{ filePath: string }`
420
+ * **`event`:** Emits log messages through an event emitter.
421
+ * **params:** `{ emitter: EventEmitter }`
422
+ * **`remote`:** Sends log messages to a remote server via a POST request.
423
+ * **params:** `{ url: string }`
424
+
425
+ ### Examples
426
+
427
+ #### Logging to a File
428
+
429
+ ```javascript
430
+ import { CodeInterpreterNode } from '@fractal-solutions/qflow/nodes';
431
+
432
+ const codeNode = new CodeInterpreterNode();
433
+ codeNode.setParams({
434
+ code: 'print("Hello from Python!")',
435
+ logging: {
436
+ method: 'file',
437
+ params: { filePath: './logs/code-interpreter.log' }
438
+ }
439
+ });
440
+ ```
441
+
442
+ #### Logging to an Event Emitter
443
+
444
+ This is useful for integrating `qflow`'s logging with your application's event system.
445
+
446
+ ```javascript
447
+ import { EventEmitter } from 'events';
448
+ import { MemoryNode } from '@fractal-solutions/qflow/nodes';
449
+
450
+ const myEmitter = new EventEmitter();
451
+ myEmitter.on('log', (logEntry) => {
452
+ console.log('Received log event:', logEntry);
453
+ });
454
+
455
+ const memoryNode = new MemoryNode();
456
+ memoryNode.setParams({
457
+ action: 'store',
458
+ content: 'This is a test memory.',
459
+ logging: {
460
+ method: 'event',
461
+ params: { emitter: myEmitter }
462
+ }
463
+ });
464
+ ```
465
+
466
+ #### Disabling Logging for a Node
467
+
468
+ You can disable logging for a specific node by setting the `type` to `none`.
469
+
470
+ ```javascript
471
+ const silentHttpNode = new HttpRequestNode();
472
+ silentHttpNode.setParams({
473
+ url: 'https://api.example.com/silent',
474
+ logging: {
475
+ type: 'none'
476
+ }
477
+ });
478
+ ```
479
+
480
+ ### Global Log Level
481
+
482
+ You can set a global log level to control the verbosity of all logs in your application. Only messages with a severity level equal to or higher than the current log level will be processed.
483
+
484
+ ```javascript
485
+ import { setLogLevel, LogLevel } from '@fractal-solutions/qflow/logger';
486
+
487
+ // Set the global log level to INFO.
488
+ // This will show INFO, WARN, and ERROR messages, but hide DEBUG messages.
489
+ setLogLevel(LogLevel.INFO);
490
+ ```
491
+
492
+ The available log levels are, in order of severity:
493
+
494
+ * `LogLevel.DEBUG`: Detailed information, useful for debugging.
495
+ * `LogLevel.INFO`: General information about the application's state.
496
+ * `LogLevel.WARN`: Indicates a potential problem that does not prevent the application from running.
497
+ * `LogLevel.ERROR`: Indicates a serious error that prevented a specific operation from completing.
498
+ * `LogLevel.NONE`: Disables all logging.
499
+
500
+ ### Making Your Custom Nodes Loggable
501
+
502
+ You can make your own custom nodes loggable by following the same pattern as the built-in nodes.
503
+
504
+ 1. **Import the `log` function:**
505
+ ```javascript
506
+ import { log } from '@fractal-solutions/qflow/logger';
507
+ ```
508
+
509
+ 2. **Call the `log` function:** In your node's methods, call the `log` function and pass `this.params.logging` as the second argument. This will allow users of your node to configure its logging behavior.
510
+
511
+ Here is an example of a custom node that implements loggable behavior:
512
+
513
+ ```javascript
514
+ import { AsyncNode } from '@fractal-solutions/qflow';
515
+ import { log } from '@fractal-solutions/qflow/logger';
516
+
517
+ class MyCustomNode extends AsyncNode {
518
+ async execAsync() {
519
+ log('Starting execution of MyCustomNode', this.params.logging);
520
+ // ... your node's logic ...
521
+
522
+ log('Finished execution of MyCustomNode', this.params.logging);
523
+
524
+ return 'default';
525
+ }
526
+ }
527
+
528
+ // --- User of your node can now configure logging ---
529
+
530
+ const myNode = new MyCustomNode();
531
+
532
+ // This will log to the console (default)
533
+ myNode.setParams({ /* ... */ });
534
+
535
+ // This will log to a file
536
+ const anotherNode = new MyCustomNode();
537
+ anotherNode.setParams({
538
+ /* ... */,
539
+ logging: {
540
+ method: 'file',
541
+ params: { filePath: './my-custom-node.log' }
542
+ }
543
+ });
544
+ ```
545
+
546
+
547
+
374
548
  ## Agents
375
549
 
376
550
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fractal-solutions/qflow",
3
- "version": "1.0.54",
3
+ "version": "1.0.55",
4
4
  "description": "A lightweight and flexible JavaScript library for creating and managing workflows and agents.",
5
5
  "main": "dist/qflow.js",
6
6
  "types": "dist/qflow.d.ts",