@push.rocks/smartlog 3.1.1 → 3.1.3

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.
@@ -3,7 +3,7 @@
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@push.rocks/smartlog',
6
- version: '3.1.1',
6
+ version: '3.1.3',
7
7
  description: 'A minimalistic, distributed, and extensible logging tool supporting centralized log management.'
8
8
  };
9
9
  //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSxzQkFBc0I7SUFDNUIsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLGlHQUFpRztDQUMvRyxDQUFBIn0=
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@push.rocks/smartlog",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "private": false,
5
5
  "description": "A minimalistic, distributed, and extensible logging tool supporting centralized log management.",
6
6
  "keywords": [
@@ -39,8 +39,7 @@
39
39
  "@git.zone/tsbuild": "^2.5.1",
40
40
  "@git.zone/tsbundle": "^2.2.5",
41
41
  "@git.zone/tsrun": "^1.3.3",
42
- "@git.zone/tstest": "^1.4.0",
43
- "@push.rocks/tapbundle": "^6.0.3",
42
+ "@git.zone/tstest": "^1.7.0",
44
43
  "@types/node": "^22.15.18"
45
44
  },
46
45
  "dependencies": {
package/readme.hints.md CHANGED
@@ -1 +1,53 @@
1
-
1
+ # SmartLog - Development Hints & Notes
2
+
3
+ This document contains notes and findings about the SmartLog library to help with development and understanding of the codebase.
4
+
5
+ ## Key Components
6
+
7
+ - **Smartlog**: Main logger class that handles logging operations
8
+ - **LogRouter**: Routes logs to various destinations
9
+ - **LogGroup**: Groups related logs for better traceability
10
+ - **ConsoleLog**: Destination that logs to the console
11
+ - **ILogDestination**: Interface for implementing custom log destinations
12
+
13
+ ## Core Concepts
14
+
15
+ - **Log Context**: Metadata about the environment (company, environment, runtime, etc.)
16
+ - **Log Levels**: 'silly', 'info', 'debug', 'note', 'ok', 'success', 'warn', 'error', 'lifecycle'
17
+ - **Log Types**: 'log', 'increment', 'gauge', 'error', 'success', 'value', 'finance', 'compliance'
18
+ - **Log Correlation**: Used to link related logs together (group, transaction, instance)
19
+
20
+ ## Interactive Console Features
21
+
22
+ - **SmartlogSourceInteractive**: Creates interactive spinners for CLI applications
23
+ - **SmartlogProgressBar**: Creates progress bars for CLI applications
24
+ - Both automatically detect non-interactive environments and provide fallback behavior
25
+
26
+ ## Environment Detection
27
+
28
+ The library uses feature detection to adapt to different environments:
29
+ - Checks for TTY capability
30
+ - Detects CI/CD environments (GitHub Actions, Jenkins, GitLab CI, Travis, CircleCI)
31
+ - Provides appropriate output based on the environment
32
+
33
+ ## Available Destinations
34
+
35
+ - Console (built-in)
36
+ - File (ts_destination_file)
37
+ - Local (ts_destination_local)
38
+ - Clickhouse (ts_destination_clickhouse)
39
+ - Developer Tools (ts_destination_devtools)
40
+ - Receiver (ts_destination_receiver)
41
+
42
+ ## Advanced Features
43
+
44
+ - **Increment Logging**: For metrics and counters
45
+ - **Console Capture**: Option to capture all console output through Smartlog
46
+ - **Custom Destinations**: Extend with custom log destinations
47
+
48
+ ## Tests
49
+
50
+ Test files are organized by environment compatibility:
51
+ - *.both.ts: Tests for both browser and Node.js
52
+ - *.node.ts: Tests for Node.js only
53
+ - *.browser.ts: Tests for browser only
package/readme.md CHANGED
@@ -1,103 +1,215 @@
1
1
  # @push.rocks/smartlog
2
2
 
3
- minimalistic distributed and extensible logging tool
3
+ Minimalistic distributed and extensible logging tool for TypeScript and JavaScript applications.
4
4
 
5
5
  ## Install
6
6
 
7
- You can install `@push.rocks/smartlog` using npm:
7
+ Install `@push.rocks/smartlog` using pnpm (recommended), npm, or yarn:
8
8
 
9
9
  ```sh
10
+ # Using pnpm (recommended)
11
+ pnpm add @push.rocks/smartlog
12
+
13
+ # Using npm
10
14
  npm install @push.rocks/smartlog --save
15
+
16
+ # Using yarn
17
+ yarn add @push.rocks/smartlog
11
18
  ```
12
19
 
13
- Ensure that you have TypeScript and node.js installed in your development environment, as this module is intended to be used with TypeScript.
20
+ Ensure you have TypeScript and Node.js installed for TypeScript projects.
21
+
22
+ ## Package Exports
23
+
24
+ The package provides the following exports:
25
+
26
+ ```javascript
27
+ // Main module
28
+ import { Smartlog, LogGroup, ConsoleLog } from '@push.rocks/smartlog';
29
+
30
+ // Type definitions and interfaces
31
+ import { ILogPackage, ILogDestination, TLogLevel } from '@push.rocks/smartlog/interfaces';
32
+
33
+ // Interactive console features (spinners, progress bars)
34
+ import { SmartlogSourceInteractive, SmartlogProgressBar } from '@push.rocks/smartlog/source-interactive';
35
+
36
+ // Context management
37
+ import { ... } from '@push.rocks/smartlog/context';
38
+
39
+ // Log destinations
40
+ import { SmartlogDestinationClickhouse } from '@push.rocks/smartlog/destination-clickhouse';
41
+ import { SmartlogDestinationDevtools } from '@push.rocks/smartlog/destination-devtools';
42
+ import { SmartlogDestinationFile } from '@push.rocks/smartlog/destination-file';
43
+ import { DestinationLocal } from '@push.rocks/smartlog/destination-local';
44
+ import { SmartlogDestinationReceiver } from '@push.rocks/smartlog/destination-receiver';
45
+
46
+ // Receiver functionality
47
+ import { SmartlogReceiver } from '@push.rocks/smartlog/receiver';
48
+ ```
14
49
 
15
50
  ## Usage
16
51
 
17
- `@push.rocks/smartlog` is a flexible and extensible logging tool designed to provide a minimalistic yet powerful logging solution across different environments, making it especially useful in distributed systems. This documentation aims to guide you through its capabilities, setup, and how to integrate it seamlessly into your TypeScript projects.
52
+ `@push.rocks/smartlog` is a flexible, extensible logging tool designed for distributed systems. It provides a consistent logging interface across different environments while being lightweight and customizable.
18
53
 
19
54
  ### Creating a Logger Instance
20
55
 
21
- Start by importing `Smartlog` and create a logger instance by providing a context that describes your logging environment:
56
+ Start by importing `Smartlog` and create a logger instance:
22
57
 
23
58
  ```typescript
24
59
  import { Smartlog } from '@push.rocks/smartlog';
25
60
 
26
61
  const logger = new Smartlog({
27
62
  logContext: {
28
- company: 'My awesome company',
29
- companyunit: 'my awesome cloud team',
30
- containerName: 'awesome-container',
31
- environment: 'kubernetes-production',
32
- runtime: 'node',
33
- zone: 'zone x',
63
+ company: 'My Company',
64
+ companyunit: 'Cloud Team',
65
+ containerName: 'api-service',
66
+ environment: 'production', // 'local', 'test', 'staging', 'production'
67
+ runtime: 'node', // 'node', 'chrome', 'rust', 'deno', 'cloudflare_workers'
68
+ zone: 'us-west',
34
69
  },
70
+ minimumLogLevel: 'info', // Optional, defaults to 'silly'
35
71
  });
72
+
73
+ // Enable console output
74
+ logger.enableConsole();
36
75
  ```
37
76
 
38
- This context enriches your logs with valuable information, making them easier to filter and analyze in a distributed system.
77
+ The context enriches logs with valuable information for filtering and analysis across distributed systems.
39
78
 
40
79
  ### Logging Messages
41
80
 
42
- Logging is straightforward; you can log messages at various levels such as `info`, `warn`, `error`, `silly`, etc.:
43
-
44
81
  ```typescript
45
- logger.log('info', 'This is an info message');
46
- logger.log('error', 'This is an error message with details', { errorCode: 123 });
82
+ // Basic logging
83
+ logger.log('info', 'User authenticated successfully');
84
+ logger.log('error', 'Database connection failed', { errorCode: 'DB_CONN_ERR', retryCount: 3 });
85
+ logger.log('warn', 'Rate limit approaching', { currentRate: 95, limit: 100 });
86
+
87
+ // Log levels: 'silly', 'info', 'debug', 'note', 'ok', 'success', 'warn', 'error', 'lifecycle'
47
88
  ```
48
89
 
49
- The logging method accepts additional data as the third parameter, allowing you to attach more context to each log message, which is immensely useful for debugging.
90
+ The third parameter accepts any additional data to attach to the log entry.
50
91
 
51
- ### Using the Default Logger
92
+ ### Default Logger
52
93
 
53
- For convenience, `@push.rocks/smartlog` provides a default logger that you can use out of the box:
94
+ For simple cases, use the built-in default logger:
54
95
 
55
96
  ```typescript
56
97
  import { defaultLogger } from '@push.rocks/smartlog';
57
98
 
58
- defaultLogger.log('warn', 'This is a warning message using the default logger');
99
+ defaultLogger.log('info', 'Application started');
59
100
  ```
60
101
 
61
- This is particularly helpful for simple applications or for initial project setup.
102
+ ### Log Groups
62
103
 
63
- ### Interactive Console Features
104
+ Group related logs for better traceability:
105
+
106
+ ```typescript
107
+ // Create a log group with optional transaction ID
108
+ const requestGroup = logger.createLogGroup('tx-123456');
109
+
110
+ // Logs within this group will be correlated
111
+ requestGroup.log('info', 'Processing payment request');
112
+ requestGroup.log('debug', 'Validating payment details');
113
+ requestGroup.log('success', 'Payment processed successfully');
114
+ ```
64
115
 
65
- Smartlog provides interactive console features through the `@push.rocks/smartlog/source-interactive` module:
116
+ ### Custom Log Destinations
66
117
 
67
- #### Spinners
118
+ Extend logging capabilities by adding custom destinations:
119
+
120
+ ```typescript
121
+ import { Smartlog, ILogDestination } from '@push.rocks/smartlog';
122
+
123
+ class DatabaseLogDestination implements ILogDestination {
124
+ async handleLog(logPackage) {
125
+ // Store log in database
126
+ await db.logs.insert({
127
+ timestamp: new Date(logPackage.timestamp),
128
+ level: logPackage.level,
129
+ message: logPackage.message,
130
+ data: logPackage.data,
131
+ context: logPackage.context
132
+ });
133
+ }
134
+ }
135
+
136
+ // Add the custom destination to your logger
137
+ logger.addLogDestination(new DatabaseLogDestination());
138
+ ```
139
+
140
+ ### Built-in Destinations
141
+
142
+ SmartLog comes with several built-in destinations for various logging needs:
143
+
144
+ ```typescript
145
+ // Log to a file
146
+ import { SmartlogDestinationFile } from '@push.rocks/smartlog/destination-file';
147
+ logger.addLogDestination(new SmartlogDestinationFile('/path/to/logfile.log'));
148
+
149
+ // Colorful local console logging
150
+ import { DestinationLocal } from '@push.rocks/smartlog/destination-local';
151
+ logger.addLogDestination(new DestinationLocal());
152
+
153
+ // Browser DevTools with colored formatting
154
+ import { SmartlogDestinationDevtools } from '@push.rocks/smartlog/destination-devtools';
155
+ logger.addLogDestination(new SmartlogDestinationDevtools());
156
+
157
+ // ClickHouse database logging
158
+ import { SmartlogDestinationClickhouse } from '@push.rocks/smartlog/destination-clickhouse';
159
+ const clickhouse = await SmartlogDestinationClickhouse.createAndStart({
160
+ host: 'clickhouse.example.com',
161
+ port: 8123,
162
+ user: 'username',
163
+ password: 'password',
164
+ database: 'logs_db'
165
+ });
166
+ logger.addLogDestination(clickhouse);
167
+
168
+ // Remote receiver logging
169
+ import { SmartlogDestinationReceiver } from '@push.rocks/smartlog/destination-receiver';
170
+ logger.addLogDestination(new SmartlogDestinationReceiver({
171
+ endpoint: 'https://logs.example.com/api/logs'
172
+ }));
173
+ ```
68
174
 
69
- Use spinners to show progress for operations:
175
+ ### Interactive Console Features
176
+
177
+ For CLI applications, use the interactive console features:
70
178
 
71
179
  ```typescript
72
- import { SmartlogSourceInteractive } from '@push.rocks/smartlog/source-interactive';
180
+ import { SmartlogSourceInteractive, SmartlogProgressBar } from '@push.rocks/smartlog/source-interactive';
181
+ ```
182
+
183
+ #### Spinners
73
184
 
185
+ ```typescript
74
186
  const spinner = new SmartlogSourceInteractive();
187
+
188
+ // Start a spinner with text
75
189
  spinner.text('Loading data...');
76
190
 
77
- // Later, when the operation completes:
78
- spinner.finishSuccess('Data loaded successfully!');
79
- // Or if it fails:
80
- spinner.finishFail('Failed to load data');
191
+ // Customize appearance
192
+ spinner.setSpinnerStyle('dots'); // 'dots', 'line', 'star', 'simple'
193
+ spinner.setColor('blue'); // 'red', 'green', 'yellow', 'blue', etc.
194
+ spinner.setSpeed(80); // Animation speed in milliseconds
81
195
 
82
- // You can chain operations:
83
- spinner.text('Connecting to server');
84
- spinner.successAndNext('Fetching records');
85
- spinner.successAndNext('Processing data');
86
- spinner.finishSuccess('All done!');
196
+ // Update spinner status
197
+ spinner.text('Processing records...');
87
198
 
88
- // Customize appearance:
89
- spinner.setSpinnerStyle('line'); // 'dots', 'line', 'star', or 'simple'
90
- spinner.setColor('green'); // 'red', 'green', 'yellow', 'blue', etc.
91
- spinner.setSpeed(100); // Animation speed in milliseconds
199
+ // Complete with success or failure
200
+ spinner.finishSuccess('Data loaded successfully!');
201
+ spinner.finishFail('Failed to load data!');
202
+
203
+ // Chain operations
204
+ spinner.text('Connecting to server')
205
+ .successAndNext('Fetching records')
206
+ .successAndNext('Processing data')
207
+ .finishSuccess('All done!');
92
208
  ```
93
209
 
94
210
  #### Progress Bars
95
211
 
96
- Create progress bars for tracking operation progress:
97
-
98
212
  ```typescript
99
- import { SmartlogProgressBar } from '@push.rocks/smartlog/source-interactive';
100
-
101
213
  const progressBar = new SmartlogProgressBar({
102
214
  total: 100, // Total number of items
103
215
  width: 40, // Width of the progress bar
@@ -109,72 +221,64 @@ const progressBar = new SmartlogProgressBar({
109
221
  });
110
222
 
111
223
  // Update progress
112
- progressBar.update(50); // Set to 50% progress
224
+ progressBar.update(25); // Set to 25% progress
113
225
 
114
- // Or increment
115
- progressBar.increment(10); // Increase by 10 units
226
+ // Increment progress
227
+ progressBar.increment(5); // Increase by 5 units
116
228
 
117
229
  // Change color
118
230
  progressBar.setColor('blue');
119
231
 
120
232
  // Complete the progress bar
121
- progressBar.update(100); // or progressBar.complete();
233
+ progressBar.complete();
122
234
  ```
123
235
 
124
236
  #### Non-Interactive Environments
125
237
 
126
- Both spinners and progress bars automatically detect non-interactive environments (CI/CD, piped output, non-TTY) and provide fallback text-based output:
238
+ Both spinners and progress bars automatically detect non-interactive environments (CI/CD, piped output) and fallback to plain text logging:
127
239
 
128
240
  ```
129
- [Loading] Loading data...
241
+ [Loading] Connecting to server
242
+ [Success] Connected to server
243
+ [Loading] Fetching records
130
244
  Progress: 50% (50/100)
131
245
  Progress: 100% (100/100)
132
- [Success] Data loaded successfully!
246
+ [Success] Fetching complete
133
247
  ```
134
248
 
135
- ### Extending With Log Destinations
249
+ ## Advanced Usage
136
250
 
137
- One of the core strengths of `@push.rocks/smartlog` is its ability to work with multiple log destinations, enabling you to log messages not just to the console but also to external logging services or custom destinations.
251
+ ### Capturing All Console Output
138
252
 
139
- To add a log destination, you create a class that implements the `ILogDestination` interface and then add it to the logger:
253
+ Capture all `console.log` and `console.error` output through Smartlog:
140
254
 
141
255
  ```typescript
142
- import { Smartlog, ILogDestination } from '@push.rocks/smartlog';
143
-
144
- class MyCustomLogDestination implements ILogDestination {
145
- async handleLog(logPackage) {
146
- // Implement your custom logging logic here
147
- console.log(`Custom log: ${logPackage.message}`);
148
- }
149
- }
150
-
151
- const logger = new Smartlog({
152
- logContext: {
153
- /* your context */
154
- },
155
- });
156
- logger.addLogDestination(new MyCustomLogDestination());
256
+ logger.enableConsole({ captureAll: true });
157
257
  ```
158
258
 
159
- After adding your custom log destination(s), every log message sent through `Smartlog` will also be routed to them according to their implementation.
259
+ ### Increment Logging
160
260
 
161
- ### Integration with Logging Services
261
+ For metrics and counters:
162
262
 
163
- `@push.rocks/smartlog` is designed to be extensible; you can integrate it with various logging services like Scalyr, Elasticsearch, LogDNA, etc., by developing or using existing log destinations conforming to those services.
164
-
165
- Check the npm registry or GitHub for community-contributed log destinations that can seamlessly integrate `@push.rocks/smartlog` into your preferred logging infrastructure.
263
+ ```typescript
264
+ logger.increment('info', 'api_requests', { endpoint: '/users' });
265
+ ```
166
266
 
167
- ### Advanced Usage
267
+ ### Log Correlation
168
268
 
169
- - **Log Groups**: You can use log groups to associate related log messages, which is especially handy for tracking logs across distributed systems.
170
- - **Custom Log Levels**: Beyond the standard log levels, you can define custom log levels that suit your project needs.
171
- - **Dynamic Log Contexts**: The log context can be dynamically adjusted to reflect different stages or aspects of your application logic.
269
+ Correlate logs across services with correlation IDs:
172
270
 
173
- ### Conclusion
271
+ ```typescript
272
+ logger.log('info', 'Request received', { userId: 'user-123' }, {
273
+ id: 'req-abc-123',
274
+ type: 'service',
275
+ transaction: 'tx-payment-456'
276
+ });
277
+ ```
174
278
 
175
- `@push.rocks/smartlog` empowers you to implement a robust logging solution tailored to your needs with minimal effort. Its design promotes clarity, flexibility, and integration ease, making it an excellent choice for projects of any scale.
279
+ ## API Documentation
176
280
 
177
- Remember to refer to the official documentation and the type definitions for detailed information on all available methods and configurations. Happy logging!
281
+ For detailed API documentation, see the [API.md](API.md) file.
178
282
 
179
283
  ## License and Legal Information
180
284
 
@@ -3,6 +3,6 @@
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@push.rocks/smartlog',
6
- version: '3.1.1',
6
+ version: '3.1.3',
7
7
  description: 'A minimalistic, distributed, and extensible logging tool supporting centralized log management.'
8
8
  }