Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dbcfeba16d | |||
| d70954f3ef | |||
| aa4db8a8af | |||
| 0593c3e2d0 | |||
| 2d9a8e08f2 | |||
| 13ccb0f386 | |||
| e133da8076 | |||
| d502ab8cc9 | |||
| d1c05fb9ae | |||
| f81971d148 | |||
| aa6a27970a | |||
| b31d9f0c36 | |||
| e6cef68a26 | |||
| aa327efeac | |||
| 7cbc64ed8d | |||
| 2c49ef49c2 | |||
| 823784e6b6 | |||
| a98f48409d | |||
| a2ae8c0c83 | |||
| 6ef2d961a6 |
300
API.md
Normal file
300
API.md
Normal file
@@ -0,0 +1,300 @@
|
|||||||
|
# @push.rocks/smartlog API Reference
|
||||||
|
|
||||||
|
This document provides a detailed reference for all exports and APIs available in the @push.rocks/smartlog package and its submodules.
|
||||||
|
|
||||||
|
## Core Module (`@push.rocks/smartlog`)
|
||||||
|
|
||||||
|
The core module provides the main logging functionality.
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
#### `Smartlog`
|
||||||
|
|
||||||
|
Main logger class for creating, managing, and routing logs.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
|
||||||
|
const logger = new Smartlog({
|
||||||
|
logContext: {
|
||||||
|
company: 'MyCompany',
|
||||||
|
environment: 'production',
|
||||||
|
// ...other context properties
|
||||||
|
},
|
||||||
|
minimumLogLevel: 'info' // Optional, defaults to 'silly'
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
**Constructor Options:**
|
||||||
|
- `logContext`: Object containing context information about the environment
|
||||||
|
- `minimumLogLevel`: Optional minimum log level to process (defaults to 'silly')
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `log(logLevel, message, data?, correlation?)`: Log a message with optional data and correlation
|
||||||
|
- `increment(logLevel, message, data?, correlation?)`: Log an increment counter
|
||||||
|
- `addLogDestination(destination)`: Add a custom log destination
|
||||||
|
- `enableConsole(options?)`: Enable console logging (with optional stdout/stderr capture)
|
||||||
|
- `createLogGroup(transactionId?)`: Create a log group for related logs
|
||||||
|
- `handleLog(logPackage)`: Handle a pre-formatted log package
|
||||||
|
|
||||||
|
**Static Methods:**
|
||||||
|
- `createForCommitinfo(commitinfo)`: Create a logger with commit information
|
||||||
|
|
||||||
|
#### `LogGroup`
|
||||||
|
|
||||||
|
Groups related logs together for better traceability.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
|
||||||
|
const logger = new Smartlog({/*...*/});
|
||||||
|
const logGroup = logger.createLogGroup('transaction-123');
|
||||||
|
|
||||||
|
logGroup.log('info', 'Starting process');
|
||||||
|
// All logs in this group will share the same transaction ID
|
||||||
|
```
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `log(logLevel, message, data?)`: Log a message within this group
|
||||||
|
- `increment(logLevel, message, data?)`: Log an increment within this group
|
||||||
|
|
||||||
|
#### `ConsoleLog`
|
||||||
|
|
||||||
|
Implementation of `ILogDestination` that logs to console.
|
||||||
|
|
||||||
|
## Interfaces Module (`@push.rocks/smartlog/interfaces`)
|
||||||
|
|
||||||
|
This module provides all type definitions and interfaces used by Smartlog.
|
||||||
|
|
||||||
|
### Types
|
||||||
|
|
||||||
|
- `TLogType`: Available log types
|
||||||
|
- `'log' | 'increment' | 'gauge' | 'error' | 'success' | 'value' | 'finance' | 'compliance'`
|
||||||
|
|
||||||
|
- `TLogLevel`: Available log levels
|
||||||
|
- `'silly' | 'info' | 'debug' | 'note' | 'ok' | 'success' | 'warn' | 'error' | 'lifecycle'`
|
||||||
|
|
||||||
|
- `TEnvironment`: Available environments
|
||||||
|
- `'local' | 'test' | 'staging' | 'production'`
|
||||||
|
|
||||||
|
- `TRuntime`: Available runtime environments
|
||||||
|
- `'node' | 'chrome' | 'rust' | 'deno' | 'cloudflare_workers'`
|
||||||
|
|
||||||
|
### Interfaces
|
||||||
|
|
||||||
|
#### `ILogContext`
|
||||||
|
|
||||||
|
Metadata about the environment where logging occurs.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ILogContext {
|
||||||
|
commitinfo?: ICommitInfo;
|
||||||
|
company?: string;
|
||||||
|
companyunit?: string;
|
||||||
|
containerName?: string;
|
||||||
|
environment?: TEnvironment;
|
||||||
|
runtime?: TRuntime;
|
||||||
|
zone?: string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `ILogCorrelation`
|
||||||
|
|
||||||
|
Information for correlating related logs.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ILogCorrelation {
|
||||||
|
id: string;
|
||||||
|
type: 'none' | 'service' | 'build' | 'infrastructure' | 'cdn';
|
||||||
|
instance?: string;
|
||||||
|
group?: string;
|
||||||
|
transaction?: string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `ILogPackage<T = unknown>`
|
||||||
|
|
||||||
|
The standard log package format.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ILogPackage<T = unknown> {
|
||||||
|
timestamp: number;
|
||||||
|
type: TLogType;
|
||||||
|
context: ILogContext;
|
||||||
|
level: TLogLevel;
|
||||||
|
correlation: ILogCorrelation;
|
||||||
|
message: string;
|
||||||
|
data?: T;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `ILogDestination`
|
||||||
|
|
||||||
|
Interface for implementing custom log destinations.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ILogDestination {
|
||||||
|
handleLog: (logPackage: ILogPackage) => Promise<void>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Interactive Console Module (`@push.rocks/smartlog/source-interactive`)
|
||||||
|
|
||||||
|
This module provides interactive console components like spinners and progress bars.
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
#### `SmartlogSourceInteractive`
|
||||||
|
|
||||||
|
Creates interactive spinners for CLI applications.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { SmartlogSourceInteractive } from '@push.rocks/smartlog/source-interactive';
|
||||||
|
|
||||||
|
const spinner = new SmartlogSourceInteractive();
|
||||||
|
spinner.text('Loading data...');
|
||||||
|
// Later
|
||||||
|
spinner.finishSuccess('Data loaded successfully!');
|
||||||
|
```
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `text(message)`: Sets the spinner text and starts it if not running
|
||||||
|
- `finishSuccess(message?)`: Completes the spinner with success message
|
||||||
|
- `finishFail(message?)`: Completes the spinner with failure message
|
||||||
|
- `successAndNext(message)`: Marks success and starts a new spinner
|
||||||
|
- `failAndNext(message)`: Marks failure and starts a new spinner
|
||||||
|
- `setSpinnerStyle(style)`: Sets spinner style ('dots', 'line', 'star', 'simple')
|
||||||
|
- `setColor(color)`: Sets the spinner color
|
||||||
|
- `setSpeed(ms)`: Sets the animation speed
|
||||||
|
|
||||||
|
#### `SmartlogProgressBar`
|
||||||
|
|
||||||
|
Creates progress bars for CLI applications.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { SmartlogProgressBar } from '@push.rocks/smartlog/source-interactive';
|
||||||
|
|
||||||
|
const bar = new SmartlogProgressBar({
|
||||||
|
total: 100,
|
||||||
|
width: 40,
|
||||||
|
showEta: true
|
||||||
|
});
|
||||||
|
|
||||||
|
bar.update(50); // Update to 50%
|
||||||
|
bar.increment(10); // Increment by 10 units
|
||||||
|
bar.complete(); // Complete the progress bar
|
||||||
|
```
|
||||||
|
|
||||||
|
**Constructor Options:**
|
||||||
|
- `total`: Total number of items/steps
|
||||||
|
- `width`: Width of the bar in characters
|
||||||
|
- `complete`: Character for completed section
|
||||||
|
- `incomplete`: Character for incomplete section
|
||||||
|
- `renderThrottle`: Minimum time between renders
|
||||||
|
- `clear`: Whether to clear the bar on completion
|
||||||
|
- `showEta`: Show estimated time remaining
|
||||||
|
- `showPercent`: Show percentage completed
|
||||||
|
- `showCount`: Show count of items
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `update(value)`: Update progress to a specific value
|
||||||
|
- `increment(value?)`: Increment progress by a value (default: 1)
|
||||||
|
- `complete()`: Mark the progress bar as complete
|
||||||
|
- `setColor(color)`: Set the color of the progress bar
|
||||||
|
|
||||||
|
## File Destination Module (`@push.rocks/smartlog/destination-file`)
|
||||||
|
|
||||||
|
This module provides a log destination that writes logs to a file.
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
#### `SmartlogDestinationFile`
|
||||||
|
|
||||||
|
Writes logs to a file with timestamps.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
import { SmartlogDestinationFile } from '@push.rocks/smartlog/destination-file';
|
||||||
|
|
||||||
|
const logger = new Smartlog({/*...*/});
|
||||||
|
const fileDestination = new SmartlogDestinationFile('/absolute/path/to/logfile.log');
|
||||||
|
logger.addLogDestination(fileDestination);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Constructor Parameters:**
|
||||||
|
- `filePath`: Absolute path to the log file
|
||||||
|
|
||||||
|
## Local Destination Module (`@push.rocks/smartlog/destination-local`)
|
||||||
|
|
||||||
|
This module provides a log destination with colorful formatting for local development.
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
#### `DestinationLocal`
|
||||||
|
|
||||||
|
Formats logs with colors and prefixes for better readability in the console.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
import { DestinationLocal } from '@push.rocks/smartlog/destination-local';
|
||||||
|
|
||||||
|
const logger = new Smartlog({/*...*/});
|
||||||
|
const localDestination = new DestinationLocal();
|
||||||
|
logger.addLogDestination(localDestination);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `handleLog(logPackage)`: Handles a log package
|
||||||
|
- `newLine(lines?)`: Adds empty lines to the console
|
||||||
|
- `logReduced(text, repeatEveryTimes?)`: Logs only when the message changes
|
||||||
|
|
||||||
|
## DevTools Destination Module (`@push.rocks/smartlog/destination-devtools`)
|
||||||
|
|
||||||
|
This module provides a log destination that formats logs for browser developer tools.
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
#### `SmartlogDestinationDevtools`
|
||||||
|
|
||||||
|
Formats logs with colors for browser developer tools.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
import { SmartlogDestinationDevtools } from '@push.rocks/smartlog/destination-devtools';
|
||||||
|
|
||||||
|
const logger = new Smartlog({/*...*/});
|
||||||
|
const devtoolsDestination = new SmartlogDestinationDevtools();
|
||||||
|
logger.addLogDestination(devtoolsDestination);
|
||||||
|
```
|
||||||
|
|
||||||
|
## ClickHouse Destination Module (`@push.rocks/smartlog/destination-clickhouse`)
|
||||||
|
|
||||||
|
This module provides a log destination that stores logs in a ClickHouse database.
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
#### `SmartlogDestinationClickhouse`
|
||||||
|
|
||||||
|
Stores logs in a ClickHouse database for analytics and querying.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
import { SmartlogDestinationClickhouse } from '@push.rocks/smartlog/destination-clickhouse';
|
||||||
|
|
||||||
|
const logger = new Smartlog({/*...*/});
|
||||||
|
const clickhouseDestination = await SmartlogDestinationClickhouse.createAndStart({
|
||||||
|
host: 'clickhouse.example.com',
|
||||||
|
port: 8123,
|
||||||
|
user: 'username',
|
||||||
|
password: 'password',
|
||||||
|
database: 'logs_db'
|
||||||
|
});
|
||||||
|
logger.addLogDestination(clickhouseDestination);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Static Methods:**
|
||||||
|
- `createAndStart(options)`: Create and initialize a ClickHouse destination
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `start()`: Initialize the connection to ClickHouse
|
||||||
|
- `handleLog(logPackage)`: Store a log in ClickHouse
|
||||||
71
changelog.md
71
changelog.md
@@ -1,5 +1,76 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2026-02-19 - 3.2.0 - feat(destination-buffer)
|
||||||
|
add SmartlogDestinationBuffer in-memory circular buffer destination with query/filter/pagination and tests
|
||||||
|
|
||||||
|
- Introduce SmartlogDestinationBuffer: an in-memory circular buffer implementing ILogDestination with configurable maxEntries (default 2000).
|
||||||
|
- Expose APIs: handleLog, getEntries (supports level filtering, search, since timestamp, limit/offset pagination, newest-first ordering), getEntryCount, and clear.
|
||||||
|
- Add package export './destination-buffer' and module entry points (index and plugins) to expose the new destination.
|
||||||
|
- Add tests covering storage, filtering by level and search, pagination, eviction when maxEntries exceeded, clearing, and default maxEntries behavior.
|
||||||
|
|
||||||
|
## 2026-02-14 - 3.1.11 - fix(destination-receiver)
|
||||||
|
return full webrequest response from SmartlogDestinationReceiver and migrate to WebrequestClient; update tests, dependencies, docs, and npmextra metadata
|
||||||
|
|
||||||
|
- SmartlogDestinationReceiver now uses plugins.webrequest.WebrequestClient instead of plugins.webrequest.WebRequest and returns the full response object instead of response.body — callers expecting the previous shape will need to adapt (breaking change)
|
||||||
|
- Tests updated to match the new webrequest.postJson return shape
|
||||||
|
- Bumped several devDependencies and dependencies (@git.zone/* toolchain, @push.rocks/* libs) to newer major/minor versions
|
||||||
|
- Removed tsbundle from the build script and adjusted build tooling invocation
|
||||||
|
- Added pnpm.onlyBuiltDependencies and updated npmextra.json namespace keys and release/tsdoc metadata
|
||||||
|
- Documentation (readme.md) updated: examples changed to async/await usage, expanded legal and issue-reporting sections
|
||||||
|
|
||||||
|
## 2025-09-22 - 3.1.10 - fix(tests)
|
||||||
|
Bump dependency versions and adjust test to use enableConsole() default
|
||||||
|
|
||||||
|
- Update devDependencies: @git.zone/tsbuild -> ^2.6.8, @git.zone/tsbundle -> ^2.5.1, @git.zone/tstest -> ^2.3.6
|
||||||
|
- Update runtime dependencies: @push.rocks/consolecolor -> ^2.0.3, @push.rocks/smartfile -> ^11.2.7, @push.rocks/smarthash -> ^3.2.3
|
||||||
|
- Simplify test invocation in test/test.ts: call testSmartLog.enableConsole() without the captureAll option
|
||||||
|
|
||||||
|
## 2025-09-01 - 3.1.9 - fix(docs)
|
||||||
|
Update README: expand documentation, examples and usage guides
|
||||||
|
|
||||||
|
- Fully rewrote README to a comprehensive documentation page with badges, motivation and feature overview.
|
||||||
|
- Added Quick Start, detailed usage examples and code snippets for Smartlog, destinations, and custom destinations.
|
||||||
|
- Documented interactive console features (spinners, progress bars) including non-interactive fallbacks and configuration options.
|
||||||
|
- Expanded sections on built-in destinations (console, file, devtools, ClickHouse, receiver) with practical examples and env-driven configuration.
|
||||||
|
- Added integration examples (PM2, Docker), CLI usage, advanced features (context management, scoped logging, minimum log levels) and best practices.
|
||||||
|
- Included API reference pointers and contributing / license information.
|
||||||
|
|
||||||
|
## 2025-05-20 - 3.1.8 - fix(devDependencies)
|
||||||
|
Update devDependencies for tstest and Node types
|
||||||
|
|
||||||
|
- Bump @git.zone/tstest from ^1.7.0 to ^1.9.0
|
||||||
|
- Bump @types/node from ^22.15.18 to ^22.15.20
|
||||||
|
|
||||||
|
## 2025-05-20 - 3.1.7 - fix(ts_destination_local)
|
||||||
|
Update debug log color: set textColor to 'pink' in DestinationLocal.
|
||||||
|
|
||||||
|
- Changed debug log text color from 'gray' to 'pink' for improved consistency in log output
|
||||||
|
|
||||||
|
## 2025-05-20 - 3.1.6 - fix(ts_destination_local)
|
||||||
|
Update debug prefix color in DestinationLocal: change from gray to pink for improved visibility.
|
||||||
|
|
||||||
|
- Adjusted the 'debug' log prefix color in classes.destinationlocal.ts to use 'pink' instead of 'gray'.
|
||||||
|
|
||||||
|
## 2025-05-20 - 3.1.5 - fix(core)
|
||||||
|
Maintain and verify project metadata and commit info consistency
|
||||||
|
|
||||||
|
- No code changes; confirming commit info files and documentation remain aligned
|
||||||
|
- Ensured consistent versioning across submodules and package metadata
|
||||||
|
|
||||||
|
## 2025-05-20 - 3.1.4 - fix(DestinationLocal)
|
||||||
|
Fix debug log rendering, add fallback for unknown log levels, and correct error prefix typo in local destination
|
||||||
|
|
||||||
|
- Added tests for debug and unknown log levels in DestinationLocal
|
||||||
|
- Refactored log string generation to use a fallback style for undefined levels
|
||||||
|
- Fixed typo: replaced non-existent 'errorPrefix' with 'error.prefix'
|
||||||
|
|
||||||
|
## 2025-05-19 - 3.1.3 - fix(documentation)
|
||||||
|
Update API reference and enhance README with detailed examples and usage instructions
|
||||||
|
|
||||||
|
- Added comprehensive API.md with reference details for core modules, interfaces, and interactive console features
|
||||||
|
- Updated README.md with improved installation steps, usage examples, and log group information
|
||||||
|
- Revised development hints to reflect environment detection and test file organization
|
||||||
|
|
||||||
## 2025-05-16 - 3.1.2 - fix(tests)
|
## 2025-05-16 - 3.1.2 - fix(tests)
|
||||||
Update test imports and devDependencies to use @git.zone/tstest/tapbundle
|
Update test imports and devDependencies to use @git.zone/tstest/tapbundle
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"gitzone": {
|
"@git.zone/cli": {
|
||||||
"projectType": "npm",
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "code.foss.global",
|
"githost": "code.foss.global",
|
||||||
@@ -25,13 +25,19 @@
|
|||||||
"error tracking",
|
"error tracking",
|
||||||
"development tools"
|
"development tools"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"release": {
|
||||||
|
"registries": [
|
||||||
|
"https://verdaccio.lossless.digital",
|
||||||
|
"https://registry.npmjs.org"
|
||||||
|
],
|
||||||
|
"accessLevel": "public"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"npmci": {
|
"@git.zone/tsdoc": {
|
||||||
"npmGlobalTools": [],
|
|
||||||
"npmAccessLevel": "public"
|
|
||||||
},
|
|
||||||
"tsdoc": {
|
|
||||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||||
|
},
|
||||||
|
"@ship.zone/szci": {
|
||||||
|
"npmGlobalTools": []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
33
package.json
33
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartlog",
|
"name": "@push.rocks/smartlog",
|
||||||
"version": "3.1.2",
|
"version": "3.2.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "A minimalistic, distributed, and extensible logging tool supporting centralized log management.",
|
"description": "A minimalistic, distributed, and extensible logging tool supporting centralized log management.",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
@@ -30,6 +30,7 @@
|
|||||||
"./destination-devtools": "./dist_ts_destination_devtools/index.js",
|
"./destination-devtools": "./dist_ts_destination_devtools/index.js",
|
||||||
"./destination-file": "./dist_ts_destination_file/index.js",
|
"./destination-file": "./dist_ts_destination_file/index.js",
|
||||||
"./destination-local": "./dist_ts_destination_local/index.js",
|
"./destination-local": "./dist_ts_destination_local/index.js",
|
||||||
|
"./destination-buffer": "./dist_ts_destination_buffer/index.js",
|
||||||
"./destination-receiver": "./dist_ts_destination_receiver/index.js",
|
"./destination-receiver": "./dist_ts_destination_receiver/index.js",
|
||||||
"./receiver": "./dist_ts_receiver/index.js"
|
"./receiver": "./dist_ts_receiver/index.js"
|
||||||
},
|
},
|
||||||
@@ -37,28 +38,28 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/**/*.ts --verbose)",
|
"test": "(tstest test/**/*.ts --verbose)",
|
||||||
"build": "(tsbuild tsfolders --allowimplicitany && tsbundle npm)",
|
"build": "(tsbuild tsfolders)",
|
||||||
"format": "(gitzone format)",
|
"format": "(gitzone format)",
|
||||||
"buildDocs": "tsdoc"
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsbuild": "^2.5.1",
|
"@git.zone/tsbuild": "^4.1.2",
|
||||||
"@git.zone/tsbundle": "^2.2.5",
|
"@git.zone/tsbundle": "^2.8.3",
|
||||||
"@git.zone/tsrun": "^1.3.3",
|
"@git.zone/tsrun": "^2.0.1",
|
||||||
"@git.zone/tstest": "^1.7.0",
|
"@git.zone/tstest": "^3.1.8",
|
||||||
"@types/node": "^22.15.18"
|
"@types/node": "^22.15.20"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@api.global/typedrequest-interfaces": "^3.0.19",
|
"@api.global/typedrequest-interfaces": "^3.0.19",
|
||||||
"@push.rocks/consolecolor": "^2.0.2",
|
"@push.rocks/consolecolor": "^2.0.3",
|
||||||
"@push.rocks/isounique": "^1.0.4",
|
"@push.rocks/isounique": "^1.0.5",
|
||||||
"@push.rocks/smartclickhouse": "^2.0.17",
|
"@push.rocks/smartclickhouse": "^2.0.17",
|
||||||
"@push.rocks/smartfile": "^11.2.0",
|
"@push.rocks/smartfile": "^11.2.7",
|
||||||
"@push.rocks/smarthash": "^3.0.4",
|
"@push.rocks/smarthash": "^3.2.6",
|
||||||
"@push.rocks/smartpromise": "^4.2.3",
|
"@push.rocks/smartpromise": "^4.2.3",
|
||||||
"@push.rocks/smarttime": "^4.1.1",
|
"@push.rocks/smarttime": "^4.1.1",
|
||||||
"@push.rocks/webrequest": "^3.0.37",
|
"@push.rocks/webrequest": "^4.0.1",
|
||||||
"@tsclass/tsclass": "^9.2.0"
|
"@tsclass/tsclass": "^9.3.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@@ -86,6 +87,10 @@
|
|||||||
"url": "https://code.foss.global/push.rocks/smartlog/issues"
|
"url": "https://code.foss.global/push.rocks/smartlog/issues"
|
||||||
},
|
},
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
"overrides": {}
|
"overrides": {},
|
||||||
|
"onlyBuiltDependencies": [
|
||||||
|
"esbuild",
|
||||||
|
"puppeteer"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7938
pnpm-lock.yaml
generated
7938
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1 +1,53 @@
|
|||||||
|
# SmartLog - Development Hints & Notes
|
||||||
|
|
||||||
|
This document contains notes and findings about the SmartLog library to help with development and understanding of the codebase.
|
||||||
|
|
||||||
|
## Key Components
|
||||||
|
|
||||||
|
- **Smartlog**: Main logger class that handles logging operations
|
||||||
|
- **LogRouter**: Routes logs to various destinations
|
||||||
|
- **LogGroup**: Groups related logs for better traceability
|
||||||
|
- **ConsoleLog**: Destination that logs to the console
|
||||||
|
- **ILogDestination**: Interface for implementing custom log destinations
|
||||||
|
|
||||||
|
## Core Concepts
|
||||||
|
|
||||||
|
- **Log Context**: Metadata about the environment (company, environment, runtime, etc.)
|
||||||
|
- **Log Levels**: 'silly', 'info', 'debug', 'note', 'ok', 'success', 'warn', 'error', 'lifecycle'
|
||||||
|
- **Log Types**: 'log', 'increment', 'gauge', 'error', 'success', 'value', 'finance', 'compliance'
|
||||||
|
- **Log Correlation**: Used to link related logs together (group, transaction, instance)
|
||||||
|
|
||||||
|
## Interactive Console Features
|
||||||
|
|
||||||
|
- **SmartlogSourceInteractive**: Creates interactive spinners for CLI applications
|
||||||
|
- **SmartlogProgressBar**: Creates progress bars for CLI applications
|
||||||
|
- Both automatically detect non-interactive environments and provide fallback behavior
|
||||||
|
|
||||||
|
## Environment Detection
|
||||||
|
|
||||||
|
The library uses feature detection to adapt to different environments:
|
||||||
|
- Checks for TTY capability
|
||||||
|
- Detects CI/CD environments (GitHub Actions, Jenkins, GitLab CI, Travis, CircleCI)
|
||||||
|
- Provides appropriate output based on the environment
|
||||||
|
|
||||||
|
## Available Destinations
|
||||||
|
|
||||||
|
- Console (built-in)
|
||||||
|
- File (ts_destination_file)
|
||||||
|
- Local (ts_destination_local)
|
||||||
|
- Clickhouse (ts_destination_clickhouse)
|
||||||
|
- Developer Tools (ts_destination_devtools)
|
||||||
|
- Receiver (ts_destination_receiver)
|
||||||
|
|
||||||
|
## Advanced Features
|
||||||
|
|
||||||
|
- **Increment Logging**: For metrics and counters
|
||||||
|
- **Console Capture**: Option to capture all console output through Smartlog
|
||||||
|
- **Custom Destinations**: Extend with custom log destinations
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
Test files are organized by environment compatibility:
|
||||||
|
- *.both.ts: Tests for both browser and Node.js
|
||||||
|
- *.node.ts: Tests for Node.js only
|
||||||
|
- *.browser.ts: Tests for browser only
|
||||||
700
readme.md
700
readme.md
@@ -1,196 +1,704 @@
|
|||||||
# @push.rocks/smartlog
|
# @push.rocks/smartlog 🚀
|
||||||
|
*The ultimate TypeScript logging solution for modern applications*
|
||||||
|
|
||||||
minimalistic distributed and extensible logging tool
|
[](https://www.npmjs.com/package/@push.rocks/smartlog)
|
||||||
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
## Install
|
> **smartlog** is a powerful, distributed, and extensible logging system designed for the cloud-native era. Whether you're debugging locally, monitoring production systems, or building complex microservices, smartlog adapts to your needs with style. 🎯
|
||||||
|
|
||||||
You can install `@push.rocks/smartlog` using npm:
|
## Issue Reporting and Security
|
||||||
|
|
||||||
```sh
|
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
|
||||||
npm install @push.rocks/smartlog --save
|
|
||||||
|
## 🌟 Why smartlog?
|
||||||
|
|
||||||
|
- **🎨 Beautiful Console Output**: Color-coded, formatted logs that are actually readable
|
||||||
|
- **🔌 Extensible Architecture**: Plug in any destination — databases, files, remote servers
|
||||||
|
- **🌍 Distributed by Design**: Built for microservices with correlation and context tracking
|
||||||
|
- **⚡ Zero-Config Start**: Works out of the box, scales when you need it
|
||||||
|
- **🎭 Interactive CLI Tools**: Spinners and progress bars that handle non-TTY environments gracefully
|
||||||
|
- **📊 Structured Logging**: JSON-based for easy parsing and analysis
|
||||||
|
- **🔍 Smart Filtering**: Log levels and context-based filtering
|
||||||
|
|
||||||
|
## 📦 Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Using pnpm (recommended)
|
||||||
|
pnpm add @push.rocks/smartlog
|
||||||
|
|
||||||
|
# Using npm
|
||||||
|
npm install @push.rocks/smartlog
|
||||||
```
|
```
|
||||||
|
|
||||||
Ensure that you have TypeScript and node.js installed in your development environment, as this module is intended to be used with TypeScript.
|
## 🚀 Quick Start
|
||||||
|
|
||||||
## Usage
|
### Your First Logger
|
||||||
|
|
||||||
`@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.
|
|
||||||
|
|
||||||
### Creating a Logger Instance
|
|
||||||
|
|
||||||
Start by importing `Smartlog` and create a logger instance by providing a context that describes your logging environment:
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Smartlog } from '@push.rocks/smartlog';
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
|
||||||
|
// Create a logger with context
|
||||||
const logger = new Smartlog({
|
const logger = new Smartlog({
|
||||||
logContext: {
|
logContext: {
|
||||||
company: 'My awesome company',
|
company: 'MyStartup',
|
||||||
companyunit: 'my awesome cloud team',
|
companyunit: 'Backend Team',
|
||||||
containerName: 'awesome-container',
|
containerName: 'api-gateway',
|
||||||
environment: 'kubernetes-production',
|
environment: 'production',
|
||||||
runtime: 'node',
|
runtime: 'node',
|
||||||
zone: 'zone x',
|
zone: 'eu-central'
|
||||||
},
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Enable console output
|
||||||
|
logger.enableConsole();
|
||||||
|
|
||||||
|
// Start logging!
|
||||||
|
await logger.log('info', '🎉 Application started successfully');
|
||||||
|
await logger.log('error', '💥 Database connection failed', {
|
||||||
|
errorCode: 'DB_TIMEOUT',
|
||||||
|
attemptCount: 3
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
This context enriches your logs with valuable information, making them easier to filter and analyze in a distributed system.
|
### Using `createForCommitinfo`
|
||||||
|
|
||||||
### Logging Messages
|
If you're integrating with a build system that provides commit info, you can use the static factory method:
|
||||||
|
|
||||||
Logging is straightforward; you can log messages at various levels such as `info`, `warn`, `error`, `silly`, etc.:
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
logger.log('info', 'This is an info message');
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
logger.log('error', 'This is an error message with details', { errorCode: 123 });
|
|
||||||
|
const logger = Smartlog.createForCommitinfo({
|
||||||
|
name: 'my-app',
|
||||||
|
version: '1.0.0',
|
||||||
|
description: 'My application'
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.enableConsole();
|
||||||
|
await logger.log('lifecycle', '🔄 App starting...');
|
||||||
```
|
```
|
||||||
|
|
||||||
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.
|
## 📚 Core Concepts
|
||||||
|
|
||||||
### Using the Default Logger
|
### Log Levels
|
||||||
|
|
||||||
For convenience, `@push.rocks/smartlog` provides a default logger that you can use out of the box:
|
smartlog supports semantic log levels for different scenarios:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { defaultLogger } from '@push.rocks/smartlog';
|
// Lifecycle events
|
||||||
|
await logger.log('lifecycle', '🔄 Container starting up...');
|
||||||
|
|
||||||
defaultLogger.log('warn', 'This is a warning message using the default logger');
|
// Success states
|
||||||
|
await logger.log('success', '✅ Payment processed');
|
||||||
|
await logger.log('ok', '👍 Health check passed');
|
||||||
|
|
||||||
|
// Information and debugging
|
||||||
|
await logger.log('info', '📋 User profile updated');
|
||||||
|
await logger.log('note', '📌 Cache invalidated');
|
||||||
|
await logger.log('debug', '🔍 Query execution plan', { sql: 'SELECT * FROM users' });
|
||||||
|
|
||||||
|
// Warnings and errors
|
||||||
|
await logger.log('warn', '⚠️ Memory usage above 80%');
|
||||||
|
await logger.log('error', '❌ Failed to send email');
|
||||||
|
|
||||||
|
// Verbose output
|
||||||
|
await logger.log('silly', '🔬 Entering function processPayment()');
|
||||||
```
|
```
|
||||||
|
|
||||||
This is particularly helpful for simple applications or for initial project setup.
|
Available levels (from most to least verbose): `silly`, `debug`, `info`, `note`, `ok`, `success`, `warn`, `error`, `lifecycle`
|
||||||
|
|
||||||
### Interactive Console Features
|
### Log Types
|
||||||
|
|
||||||
Smartlog provides interactive console features through the `@push.rocks/smartlog/source-interactive` module:
|
Each log entry has a type that describes what kind of data it represents:
|
||||||
|
|
||||||
#### Spinners
|
| Type | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `log` | Standard log message |
|
||||||
|
| `increment` | Counter/metric increment |
|
||||||
|
| `gauge` | Gauge measurement |
|
||||||
|
| `error` | Error event |
|
||||||
|
| `success` | Success event |
|
||||||
|
| `value` | Value recording |
|
||||||
|
| `finance` | Financial transaction |
|
||||||
|
| `compliance` | Compliance event |
|
||||||
|
|
||||||
Use spinners to show progress for operations:
|
### Log Groups for Correlation
|
||||||
|
|
||||||
|
Perfect for tracking request flows through your system:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Track a user request through multiple operations
|
||||||
|
const requestGroup = logger.createLogGroup('req-7f3a2b');
|
||||||
|
|
||||||
|
requestGroup.log('info', 'Received POST /api/users');
|
||||||
|
requestGroup.log('debug', 'Validating request body');
|
||||||
|
requestGroup.log('info', 'Creating user in database');
|
||||||
|
requestGroup.log('success', 'User created successfully', { userId: 'usr_123' });
|
||||||
|
|
||||||
|
// All logs in the group share the same group ID and transaction ID
|
||||||
|
```
|
||||||
|
|
||||||
|
### Log Context
|
||||||
|
|
||||||
|
Every log carries contextual information about the environment it was created in:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ILogContext {
|
||||||
|
commitinfo?: ICommitInfo; // Build/version info
|
||||||
|
company?: string; // Company name
|
||||||
|
companyunit?: string; // Team or department
|
||||||
|
containerName?: string; // Container/service name
|
||||||
|
environment?: 'local' | 'test' | 'staging' | 'production';
|
||||||
|
runtime?: 'node' | 'chrome' | 'rust' | 'deno' | 'cloudflare_workers';
|
||||||
|
zone?: string; // Deployment zone/region
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🎯 Log Destinations
|
||||||
|
|
||||||
|
smartlog routes log packages to any number of destinations simultaneously. Each destination implements the `ILogDestination` interface with a single `handleLog` method.
|
||||||
|
|
||||||
|
### Built-in Destinations
|
||||||
|
|
||||||
|
#### 🖥️ Local Console (Enhanced)
|
||||||
|
|
||||||
|
Beautiful, color-coded output for local development:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { DestinationLocal } from '@push.rocks/smartlog/destination-local';
|
||||||
|
|
||||||
|
const localDestination = new DestinationLocal();
|
||||||
|
logger.addLogDestination(localDestination);
|
||||||
|
|
||||||
|
// Output is color-coded per log level:
|
||||||
|
// 🔵 info: blue prefix, white text
|
||||||
|
// 🔴 error: red prefix, red text
|
||||||
|
// 🟢 ok/success: green prefix, green text
|
||||||
|
// 🟠 warn: orange prefix, orange text
|
||||||
|
// 🟣 note/debug: pink prefix, pink text
|
||||||
|
// 🔵 silly: blue background, blue text
|
||||||
|
```
|
||||||
|
|
||||||
|
The `DestinationLocal` also supports **reduced logging** — a mode where repeated identical log messages are suppressed:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const localDest = new DestinationLocal();
|
||||||
|
|
||||||
|
localDest.logReduced('Waiting for connection...'); // Logged
|
||||||
|
localDest.logReduced('Waiting for connection...'); // Suppressed
|
||||||
|
localDest.logReduced('Waiting for connection...'); // Suppressed
|
||||||
|
localDest.logReduced('Connected!'); // Logged (new message)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 📁 File Logging
|
||||||
|
|
||||||
|
Persist logs to files with timestamped entries:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { SmartlogDestinationFile } from '@push.rocks/smartlog/destination-file';
|
||||||
|
|
||||||
|
// Path MUST be absolute
|
||||||
|
const fileDestination = new SmartlogDestinationFile('/var/log/myapp/app.log');
|
||||||
|
logger.addLogDestination(fileDestination);
|
||||||
|
|
||||||
|
// Log entries are written as timestamped lines:
|
||||||
|
// 2024-01-15T10:30:00.000Z: Application started
|
||||||
|
// 2024-01-15T10:30:01.123Z: Processing request
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 🌐 Browser DevTools
|
||||||
|
|
||||||
|
Optimized for browser environments with styled console output:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { SmartlogDestinationDevtools } from '@push.rocks/smartlog/destination-devtools';
|
||||||
|
|
||||||
|
const devtools = new SmartlogDestinationDevtools();
|
||||||
|
logger.addLogDestination(devtools);
|
||||||
|
|
||||||
|
// Uses CSS styling in browser console for beautiful, categorized output
|
||||||
|
// Different colors for error (red), info (pink), ok (green), success (green),
|
||||||
|
// warn (orange), and note (blue) levels
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 📊 ClickHouse Analytics
|
||||||
|
|
||||||
|
Store logs in ClickHouse for powerful time-series analytics:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { SmartlogDestinationClickhouse } from '@push.rocks/smartlog/destination-clickhouse';
|
||||||
|
|
||||||
|
const clickhouse = await SmartlogDestinationClickhouse.createAndStart({
|
||||||
|
url: 'https://analytics.example.com:8123',
|
||||||
|
database: 'logs',
|
||||||
|
username: 'logger',
|
||||||
|
password: process.env.CLICKHOUSE_PASSWORD
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.addLogDestination(clickhouse);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 🔗 Remote Receiver
|
||||||
|
|
||||||
|
Send logs to a centralized logging service with authenticated transport:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { SmartlogDestinationReceiver } from '@push.rocks/smartlog/destination-receiver';
|
||||||
|
|
||||||
|
const receiver = new SmartlogDestinationReceiver({
|
||||||
|
passphrase: process.env.LOG_PASSPHRASE,
|
||||||
|
receiverEndpoint: 'https://logs.mycompany.com/ingest'
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.addLogDestination(receiver);
|
||||||
|
```
|
||||||
|
|
||||||
|
Logs are sent as authenticated JSON payloads with SHA-256 hashed passphrases.
|
||||||
|
|
||||||
|
### 🛠️ Custom Destinations
|
||||||
|
|
||||||
|
Build your own destination for any logging backend by implementing the `ILogDestination` interface:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import type { ILogDestination, ILogPackage } from '@push.rocks/smartlog/interfaces';
|
||||||
|
|
||||||
|
class ElasticsearchDestination implements ILogDestination {
|
||||||
|
async handleLog(logPackage: ILogPackage): Promise<void> {
|
||||||
|
await this.client.index({
|
||||||
|
index: `logs-${new Date().toISOString().split('T')[0]}`,
|
||||||
|
body: {
|
||||||
|
'@timestamp': logPackage.timestamp,
|
||||||
|
level: logPackage.level,
|
||||||
|
message: logPackage.message,
|
||||||
|
context: logPackage.context,
|
||||||
|
data: logPackage.data,
|
||||||
|
correlation: logPackage.correlation
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.addLogDestination(new ElasticsearchDestination());
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🎨 Interactive Console Features
|
||||||
|
|
||||||
|
### Spinners
|
||||||
|
|
||||||
|
Create beautiful loading animations that degrade gracefully in CI/CD:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { SmartlogSourceInteractive } from '@push.rocks/smartlog/source-interactive';
|
import { SmartlogSourceInteractive } from '@push.rocks/smartlog/source-interactive';
|
||||||
|
|
||||||
const spinner = new SmartlogSourceInteractive();
|
const spinner = new SmartlogSourceInteractive();
|
||||||
spinner.text('Loading data...');
|
|
||||||
|
|
||||||
// Later, when the operation completes:
|
// Basic usage
|
||||||
spinner.finishSuccess('Data loaded successfully!');
|
spinner.text('🔄 Fetching data from API...');
|
||||||
// Or if it fails:
|
// ... perform async operation
|
||||||
spinner.finishFail('Failed to load data');
|
spinner.finishSuccess('✅ Data fetched successfully!');
|
||||||
|
|
||||||
// You can chain operations:
|
// Chain success and move to next task
|
||||||
spinner.text('Connecting to server');
|
spinner.text('📡 Connecting to database');
|
||||||
spinner.successAndNext('Fetching records');
|
// ... connect
|
||||||
spinner.successAndNext('Processing data');
|
spinner.successAndNext('🔍 Running migrations');
|
||||||
spinner.finishSuccess('All done!');
|
// ... migrate
|
||||||
|
spinner.finishSuccess('🎉 Database ready!');
|
||||||
|
|
||||||
// Customize appearance:
|
// Customize appearance
|
||||||
spinner.setSpinnerStyle('line'); // 'dots', 'line', 'star', or 'simple'
|
spinner
|
||||||
spinner.setColor('green'); // 'red', 'green', 'yellow', 'blue', etc.
|
.setSpinnerStyle('dots') // 'dots' | 'line' | 'star' | 'simple'
|
||||||
spinner.setSpeed(100); // Animation speed in milliseconds
|
.setColor('cyan') // any terminal color
|
||||||
|
.setSpeed(80); // animation speed in ms
|
||||||
|
|
||||||
|
spinner.text('🚀 Deploying application...');
|
||||||
|
|
||||||
|
// Handle failures
|
||||||
|
try {
|
||||||
|
await deployApp();
|
||||||
|
spinner.finishSuccess('✅ Deployed!');
|
||||||
|
} catch (error) {
|
||||||
|
spinner.finishFail('❌ Deployment failed');
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Progress Bars
|
### Progress Bars
|
||||||
|
|
||||||
Create progress bars for tracking operation progress:
|
Track long-running operations with style:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { SmartlogProgressBar } from '@push.rocks/smartlog/source-interactive';
|
import { SmartlogProgressBar } from '@push.rocks/smartlog/source-interactive';
|
||||||
|
|
||||||
const progressBar = new SmartlogProgressBar({
|
// Create a progress bar
|
||||||
total: 100, // Total number of items
|
const progress = new SmartlogProgressBar({
|
||||||
width: 40, // Width of the progress bar
|
total: 100,
|
||||||
complete: '█', // Character for completed section
|
width: 40, // Bar width in characters
|
||||||
incomplete: '░', // Character for incomplete section
|
complete: '█', // Fill character
|
||||||
|
incomplete: '░', // Empty character
|
||||||
showEta: true, // Show estimated time remaining
|
showEta: true, // Show estimated time remaining
|
||||||
showPercent: true, // Show percentage
|
showPercent: true, // Show percentage
|
||||||
showCount: true // Show count (e.g., "50/100")
|
showCount: true // Show current/total count
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update progress
|
// Update progress
|
||||||
progressBar.update(50); // Set to 50% progress
|
for (let i = 0; i <= 100; i++) {
|
||||||
|
progress.update(i);
|
||||||
|
await someAsyncWork();
|
||||||
|
}
|
||||||
|
|
||||||
// Or increment
|
progress.complete();
|
||||||
progressBar.increment(10); // Increase by 10 units
|
|
||||||
|
|
||||||
// Change color
|
// Or use increment for simpler tracking
|
||||||
progressBar.setColor('blue');
|
const files = await getFiles();
|
||||||
|
const fileProgress = new SmartlogProgressBar({ total: files.length });
|
||||||
|
|
||||||
// Complete the progress bar
|
for (const file of files) {
|
||||||
progressBar.update(100); // or progressBar.complete();
|
await processFile(file);
|
||||||
|
fileProgress.increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
fileProgress.complete();
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Non-Interactive Environments
|
### Non-Interactive Fallback
|
||||||
|
|
||||||
Both spinners and progress bars automatically detect non-interactive environments (CI/CD, piped output, non-TTY) and provide fallback text-based output:
|
Both spinners and progress bars automatically detect non-interactive environments (CI/CD, Docker logs, piped output) and fall back to simple text output:
|
||||||
|
|
||||||
```
|
```
|
||||||
[Loading] Loading data...
|
[Loading] Connecting to database
|
||||||
|
[Success] Connected to database
|
||||||
|
[Loading] Running migrations
|
||||||
|
Progress: 25% (25/100)
|
||||||
Progress: 50% (50/100)
|
Progress: 50% (50/100)
|
||||||
Progress: 100% (100/100)
|
Progress: 100% (100/100)
|
||||||
[Success] Data loaded successfully!
|
Completed: 100% (100/100)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Extending With Log Destinations
|
Detection checks for: TTY capability, CI environment variables (GitHub Actions, Jenkins, GitLab CI, Travis, CircleCI), and `TERM=dumb`.
|
||||||
|
|
||||||
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.
|
### Backward Compatibility
|
||||||
|
|
||||||
To add a log destination, you create a class that implements the `ILogDestination` interface and then add it to the logger:
|
The `SmartlogSourceOra` class extends `SmartlogSourceInteractive` and provides a compatibility layer for code that previously used the `ora` npm package:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Smartlog, ILogDestination } from '@push.rocks/smartlog';
|
import { SmartlogSourceOra } from '@push.rocks/smartlog/source-interactive';
|
||||||
|
|
||||||
class MyCustomLogDestination implements ILogDestination {
|
const ora = new SmartlogSourceOra();
|
||||||
async handleLog(logPackage) {
|
ora.oraInstance.start();
|
||||||
// Implement your custom logging logic here
|
ora.oraInstance.succeed('Done!');
|
||||||
console.log(`Custom log: ${logPackage.message}`);
|
```
|
||||||
|
|
||||||
|
## 🔧 Advanced Features
|
||||||
|
|
||||||
|
### Minimum Log Level
|
||||||
|
|
||||||
|
Set a minimum log level that destinations can use to filter messages:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const logger = new Smartlog({
|
||||||
|
logContext: { environment: 'production', runtime: 'node' },
|
||||||
|
minimumLogLevel: 'warn' // Destinations can check this to filter
|
||||||
|
});
|
||||||
|
|
||||||
|
// The minimumLogLevel is available as a public property
|
||||||
|
console.log(logger.minimumLogLevel); // 'warn'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Increment Logging
|
||||||
|
|
||||||
|
Track metrics and counters alongside your logs:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Track API calls
|
||||||
|
logger.increment('info', 'api.requests', { endpoint: '/users', method: 'GET' });
|
||||||
|
|
||||||
|
// Track error types
|
||||||
|
logger.increment('error', 'payment.failed', { reason: 'insufficient_funds' });
|
||||||
|
```
|
||||||
|
|
||||||
|
Increment logs are routed to all destinations with `type: 'increment'` so analytics backends can aggregate them.
|
||||||
|
|
||||||
|
### Capture All Console Output
|
||||||
|
|
||||||
|
Redirect all `console.log` and `console.error` through smartlog:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
logger.enableConsole({
|
||||||
|
captureAll: true
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('This goes through smartlog as info!');
|
||||||
|
console.error('This goes through smartlog as error!');
|
||||||
|
|
||||||
|
// Strings containing "Error:" are automatically classified as error level
|
||||||
|
// All captured output is prefixed with "LOG =>" to prevent recursion
|
||||||
|
```
|
||||||
|
|
||||||
|
### Log Receiver Server
|
||||||
|
|
||||||
|
Accept authenticated log packages from remote services:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { SmartlogReceiver } from '@push.rocks/smartlog/receiver';
|
||||||
|
|
||||||
|
const receiver = new SmartlogReceiver({
|
||||||
|
smartlogInstance: logger,
|
||||||
|
passphrase: 'shared-secret',
|
||||||
|
validatorFunction: async (logPackage) => {
|
||||||
|
// Custom validation logic
|
||||||
|
return logPackage.context.company === 'MyCompany';
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
// Handle incoming authenticated log packages (e.g., from an HTTP endpoint)
|
||||||
|
app.post('/logs', async (req, res) => {
|
||||||
|
const result = await receiver.handleAuthenticatedLog(req.body);
|
||||||
|
res.json(result); // { status: 'ok' } or { status: 'error' }
|
||||||
|
});
|
||||||
|
|
||||||
|
// Or handle batches
|
||||||
|
app.post('/logs/batch', async (req, res) => {
|
||||||
|
await receiver.handleManyAuthenticatedLogs(req.body);
|
||||||
|
res.json({ status: 'ok' });
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🏗️ Real-World Examples
|
||||||
|
|
||||||
|
### Microservice with Distributed Tracing
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
import { SmartlogDestinationClickhouse } from '@push.rocks/smartlog/destination-clickhouse';
|
||||||
|
import { DestinationLocal } from '@push.rocks/smartlog/destination-local';
|
||||||
|
|
||||||
|
// Initialize logger with service context
|
||||||
|
const logger = new Smartlog({
|
||||||
|
logContext: {
|
||||||
|
company: 'TechCorp',
|
||||||
|
companyunit: 'Platform',
|
||||||
|
containerName: 'user-service',
|
||||||
|
environment: 'production',
|
||||||
|
runtime: 'node',
|
||||||
|
zone: 'eu-central'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add ClickHouse for analytics
|
||||||
|
const clickhouse = await SmartlogDestinationClickhouse.createAndStart({
|
||||||
|
url: process.env.CLICKHOUSE_URL,
|
||||||
|
database: 'microservices_logs'
|
||||||
|
});
|
||||||
|
logger.addLogDestination(clickhouse);
|
||||||
|
|
||||||
|
// Add local console for container stdout
|
||||||
|
logger.addLogDestination(new DestinationLocal());
|
||||||
|
|
||||||
|
// Express middleware for request tracking
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
const logGroup = logger.createLogGroup(req.headers['x-request-id'] || 'unknown');
|
||||||
|
|
||||||
|
logGroup.log('info', 'Incoming request', {
|
||||||
|
method: req.method,
|
||||||
|
path: req.path,
|
||||||
|
ip: req.ip
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('finish', () => {
|
||||||
|
logGroup.log('info', 'Request completed', {
|
||||||
|
statusCode: res.statusCode,
|
||||||
|
duration: Date.now() - req.startTime
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### CLI Tool with Progress Tracking
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
import { SmartlogSourceInteractive, SmartlogProgressBar } from '@push.rocks/smartlog/source-interactive';
|
||||||
|
|
||||||
const logger = new Smartlog({
|
const logger = new Smartlog({
|
||||||
logContext: {
|
logContext: {
|
||||||
/* your context */
|
containerName: 'migration-tool',
|
||||||
},
|
environment: 'local',
|
||||||
|
runtime: 'node'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
logger.addLogDestination(new MyCustomLogDestination());
|
|
||||||
|
logger.enableConsole();
|
||||||
|
|
||||||
|
async function migrateDatabase() {
|
||||||
|
const spinner = new SmartlogSourceInteractive();
|
||||||
|
|
||||||
|
spinner.text('🔌 Connecting to database...');
|
||||||
|
await connectDB();
|
||||||
|
spinner.finishSuccess('✅ Connected to database');
|
||||||
|
|
||||||
|
spinner.text('📋 Loading migrations...');
|
||||||
|
const migrations = await getMigrations();
|
||||||
|
spinner.finishSuccess(`✅ Found ${migrations.length} migrations`);
|
||||||
|
|
||||||
|
const progress = new SmartlogProgressBar({
|
||||||
|
total: migrations.length,
|
||||||
|
width: 40,
|
||||||
|
showEta: true
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const [index, migration] of migrations.entries()) {
|
||||||
|
await logger.log('info', `Running migration: ${migration.name}`);
|
||||||
|
await runMigration(migration);
|
||||||
|
progress.update(index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
progress.complete();
|
||||||
|
await logger.log('success', '🎉 All migrations completed successfully!');
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
After adding your custom log destination(s), every log message sent through `Smartlog` will also be routed to them according to their implementation.
|
### Production Logging with Multiple Destinations
|
||||||
|
|
||||||
### Integration with Logging Services
|
```typescript
|
||||||
|
import { Smartlog } from '@push.rocks/smartlog';
|
||||||
|
import { DestinationLocal } from '@push.rocks/smartlog/destination-local';
|
||||||
|
import { SmartlogDestinationFile } from '@push.rocks/smartlog/destination-file';
|
||||||
|
import { SmartlogDestinationReceiver } from '@push.rocks/smartlog/destination-receiver';
|
||||||
|
|
||||||
`@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.
|
const logger = new Smartlog({
|
||||||
|
logContext: {
|
||||||
|
company: 'Enterprise Corp',
|
||||||
|
containerName: 'payment-processor',
|
||||||
|
environment: 'production',
|
||||||
|
runtime: 'node',
|
||||||
|
zone: 'us-east-1'
|
||||||
|
},
|
||||||
|
minimumLogLevel: 'info'
|
||||||
|
});
|
||||||
|
|
||||||
Check the npm registry or GitHub for community-contributed log destinations that can seamlessly integrate `@push.rocks/smartlog` into your preferred logging infrastructure.
|
// Color-coded console for container logs
|
||||||
|
logger.addLogDestination(new DestinationLocal());
|
||||||
|
|
||||||
### Advanced Usage
|
// File for audit trail
|
||||||
|
logger.addLogDestination(new SmartlogDestinationFile('/var/log/app/audit.log'));
|
||||||
|
|
||||||
- **Log Groups**: You can use log groups to associate related log messages, which is especially handy for tracking logs across distributed systems.
|
// Central logging service
|
||||||
- **Custom Log Levels**: Beyond the standard log levels, you can define custom log levels that suit your project needs.
|
logger.addLogDestination(new SmartlogDestinationReceiver({
|
||||||
- **Dynamic Log Contexts**: The log context can be dynamically adjusted to reflect different stages or aspects of your application logic.
|
passphrase: process.env.LOG_PASSPHRASE,
|
||||||
|
receiverEndpoint: 'https://logs.enterprise.com/ingest'
|
||||||
|
}));
|
||||||
|
|
||||||
### Conclusion
|
// Custom inline destination for critical alerts
|
||||||
|
logger.addLogDestination({
|
||||||
|
async handleLog(logPackage) {
|
||||||
|
if (logPackage.level === 'error' && logPackage.data?.critical) {
|
||||||
|
await sendSlackAlert(`🚨 Critical error: ${logPackage.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
`@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.
|
## 📖 API Reference
|
||||||
|
|
||||||
Remember to refer to the official documentation and the type definitions for detailed information on all available methods and configurations. Happy logging!
|
### Smartlog Class
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
class Smartlog {
|
||||||
|
// Static factory
|
||||||
|
static createForCommitinfo(commitinfo: ICommitInfo): Smartlog;
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
constructor(options: {
|
||||||
|
logContext: ILogContext;
|
||||||
|
minimumLogLevel?: TLogLevel; // default: 'silly'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Logging
|
||||||
|
log(level: TLogLevel, message: string, data?: any, correlation?: ILogCorrelation): Promise<void>;
|
||||||
|
increment(level: TLogLevel, message: string, data?: any, correlation?: ILogCorrelation): void;
|
||||||
|
|
||||||
|
// Configuration
|
||||||
|
enableConsole(options?: { captureAll: boolean }): void;
|
||||||
|
addLogDestination(destination: ILogDestination): void;
|
||||||
|
|
||||||
|
// Correlation
|
||||||
|
createLogGroup(transactionId?: string): LogGroup;
|
||||||
|
|
||||||
|
// Forwarding (for receiver pattern)
|
||||||
|
handleLog(logPackage: ILogPackage): Promise<void>;
|
||||||
|
|
||||||
|
// Instance identity
|
||||||
|
uniInstanceId: string;
|
||||||
|
logContext: ILogContext;
|
||||||
|
minimumLogLevel: TLogLevel;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### LogGroup Class
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
class LogGroup {
|
||||||
|
groupId: string; // Auto-generated unique ID
|
||||||
|
transactionId: string; // The transaction ID passed at creation
|
||||||
|
smartlogRef: Smartlog; // Reference to the parent Smartlog
|
||||||
|
|
||||||
|
log(level: TLogLevel, message: string, data?: any): void;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ILogDestination Interface
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ILogDestination {
|
||||||
|
handleLog(logPackage: ILogPackage): Promise<void>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ILogPackage Interface
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ILogPackage<T = unknown> {
|
||||||
|
timestamp: number; // Unix timestamp in milliseconds
|
||||||
|
type: TLogType; // 'log' | 'increment' | 'gauge' | ...
|
||||||
|
context: ILogContext; // Environment context
|
||||||
|
level: TLogLevel; // Log severity
|
||||||
|
correlation: ILogCorrelation; // Correlation metadata
|
||||||
|
message: string; // The log message
|
||||||
|
data?: T; // Optional structured data
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Available Log Levels
|
||||||
|
|
||||||
|
| Level | Description | Use Case |
|
||||||
|
|-------|-------------|----------|
|
||||||
|
| `silly` | Ultra-verbose | Function entry/exit, variable dumps |
|
||||||
|
| `debug` | Debug info | Development-time diagnostics |
|
||||||
|
| `info` | Informational | Standard operational messages |
|
||||||
|
| `note` | Notable events | Important but non-critical events |
|
||||||
|
| `ok` | Success confirmation | Health checks, validations |
|
||||||
|
| `success` | Major success | Completed operations |
|
||||||
|
| `warn` | Warnings | Degraded performance, approaching limits |
|
||||||
|
| `error` | Errors | Failures requiring attention |
|
||||||
|
| `lifecycle` | Lifecycle events | Start, stop, restart, deploy |
|
||||||
|
|
||||||
## License and Legal Information
|
## License and Legal Information
|
||||||
|
|
||||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
||||||
|
|
||||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
### Trademarks
|
### Trademarks
|
||||||
|
|
||||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
||||||
|
|
||||||
|
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
||||||
|
|
||||||
### Company Information
|
### Company Information
|
||||||
|
|
||||||
Task Venture Capital GmbH
|
Task Venture Capital GmbH
|
||||||
Registered at District court Bremen HRB 35230 HB, Germany
|
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||||
|
|
||||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
||||||
|
|
||||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||||
117
test/test.destination-buffer.node.ts
Normal file
117
test/test.destination-buffer.node.ts
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||||
|
import { SmartlogDestinationBuffer } from '../ts_destination_buffer/index.js';
|
||||||
|
import type { ILogPackage, TLogLevel } from '../ts_interfaces/index.js';
|
||||||
|
|
||||||
|
const createMockLogPackage = (level: TLogLevel, message: string): ILogPackage => {
|
||||||
|
return {
|
||||||
|
timestamp: Date.now(),
|
||||||
|
type: 'log',
|
||||||
|
level,
|
||||||
|
message,
|
||||||
|
context: {
|
||||||
|
environment: 'test',
|
||||||
|
runtime: 'node',
|
||||||
|
zone: 'test-zone',
|
||||||
|
},
|
||||||
|
correlation: {
|
||||||
|
id: '123',
|
||||||
|
type: 'none',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
let buffer: SmartlogDestinationBuffer;
|
||||||
|
|
||||||
|
tap.test('should create a buffer destination instance', async () => {
|
||||||
|
buffer = new SmartlogDestinationBuffer({ maxEntries: 100 });
|
||||||
|
expect(buffer).toBeTruthy();
|
||||||
|
expect(buffer.getEntryCount()).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should store log entries via handleLog', async () => {
|
||||||
|
await buffer.handleLog(createMockLogPackage('info', 'Hello world'));
|
||||||
|
await buffer.handleLog(createMockLogPackage('error', 'Something failed'));
|
||||||
|
await buffer.handleLog(createMockLogPackage('warn', 'Watch out'));
|
||||||
|
|
||||||
|
expect(buffer.getEntryCount()).toEqual(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should retrieve entries newest-first', async () => {
|
||||||
|
const entries = buffer.getEntries();
|
||||||
|
expect(entries.length).toEqual(3);
|
||||||
|
expect(entries[0].message).toEqual('Watch out');
|
||||||
|
expect(entries[2].message).toEqual('Hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should filter entries by level', async () => {
|
||||||
|
const errorEntries = buffer.getEntries({ level: 'error' });
|
||||||
|
expect(errorEntries.length).toEqual(1);
|
||||||
|
expect(errorEntries[0].message).toEqual('Something failed');
|
||||||
|
|
||||||
|
const multiLevel = buffer.getEntries({ level: ['info', 'warn'] });
|
||||||
|
expect(multiLevel.length).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should filter entries by search string', async () => {
|
||||||
|
const results = buffer.getEntries({ search: 'hello' });
|
||||||
|
expect(results.length).toEqual(1);
|
||||||
|
expect(results[0].message).toEqual('Hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should support limit and offset pagination', async () => {
|
||||||
|
const page1 = buffer.getEntries({ limit: 2, offset: 0 });
|
||||||
|
expect(page1.length).toEqual(2);
|
||||||
|
expect(page1[0].message).toEqual('Watch out');
|
||||||
|
|
||||||
|
const page2 = buffer.getEntries({ limit: 2, offset: 2 });
|
||||||
|
expect(page2.length).toEqual(1);
|
||||||
|
expect(page2[0].message).toEqual('Hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should filter by since timestamp', async () => {
|
||||||
|
const now = Date.now();
|
||||||
|
const freshBuffer = new SmartlogDestinationBuffer();
|
||||||
|
|
||||||
|
const oldPkg = createMockLogPackage('info', 'Old message');
|
||||||
|
oldPkg.timestamp = now - 60000;
|
||||||
|
await freshBuffer.handleLog(oldPkg);
|
||||||
|
|
||||||
|
const newPkg = createMockLogPackage('info', 'New message');
|
||||||
|
newPkg.timestamp = now;
|
||||||
|
await freshBuffer.handleLog(newPkg);
|
||||||
|
|
||||||
|
const results = freshBuffer.getEntries({ since: now - 1000 });
|
||||||
|
expect(results.length).toEqual(1);
|
||||||
|
expect(results[0].message).toEqual('New message');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should enforce circular buffer max entries', async () => {
|
||||||
|
const smallBuffer = new SmartlogDestinationBuffer({ maxEntries: 5 });
|
||||||
|
|
||||||
|
for (let i = 0; i < 10; i++) {
|
||||||
|
await smallBuffer.handleLog(createMockLogPackage('info', `Message ${i}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(smallBuffer.getEntryCount()).toEqual(5);
|
||||||
|
|
||||||
|
// Should have kept the latest 5 (messages 5-9)
|
||||||
|
const entries = smallBuffer.getEntries({ limit: 10 });
|
||||||
|
expect(entries[0].message).toEqual('Message 9');
|
||||||
|
expect(entries[4].message).toEqual('Message 5');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should clear all entries', async () => {
|
||||||
|
expect(buffer.getEntryCount()).toBeGreaterThan(0);
|
||||||
|
buffer.clear();
|
||||||
|
expect(buffer.getEntryCount()).toEqual(0);
|
||||||
|
expect(buffer.getEntries().length).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should use default maxEntries of 2000', async () => {
|
||||||
|
const defaultBuffer = new SmartlogDestinationBuffer();
|
||||||
|
// Just verify it was created without error
|
||||||
|
expect(defaultBuffer).toBeTruthy();
|
||||||
|
expect(defaultBuffer.getEntryCount()).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default tap.start();
|
||||||
@@ -92,5 +92,23 @@ tap.test('should create new line(s)', async () => {
|
|||||||
// Multiple lines
|
// Multiple lines
|
||||||
testDestination.newLine(3);
|
testDestination.newLine(3);
|
||||||
});
|
});
|
||||||
|
// Test debug level rendering and fallback for unknown levels
|
||||||
|
tap.test('should handle debug and unknown log levels', async () => {
|
||||||
|
testDestination = new DestinationLocal();
|
||||||
|
let out = '';
|
||||||
|
const originalLog = console.log;
|
||||||
|
console.log = (msg: string) => { out += msg; };
|
||||||
|
|
||||||
|
// debug level should render message correctly
|
||||||
|
await testDestination.handleLog(createMockLogPackage('debug', 'debug 🎉'));
|
||||||
|
expect(out).toContain('debug 🎉');
|
||||||
|
|
||||||
|
// fallback for unknown level should still render message
|
||||||
|
out = '';
|
||||||
|
await testDestination.handleLog(createMockLogPackage('verbose' as any, 'verbose ⚠️'));
|
||||||
|
expect(out).toContain('verbose ⚠️');
|
||||||
|
|
||||||
|
console.log = originalLog;
|
||||||
|
});
|
||||||
|
|
||||||
export default tap.start();
|
export default tap.start();
|
||||||
@@ -53,10 +53,7 @@ tap.test('should attempt to send logs to the receiver endpoint', async () => {
|
|||||||
// Create a mock version of the webrequest.postJson method to avoid actual HTTP calls
|
// Create a mock version of the webrequest.postJson method to avoid actual HTTP calls
|
||||||
const originalPostJson = testDestination['webrequest'].postJson;
|
const originalPostJson = testDestination['webrequest'].postJson;
|
||||||
testDestination['webrequest'].postJson = async () => {
|
testDestination['webrequest'].postJson = async () => {
|
||||||
return {
|
return { status: 'ok' };
|
||||||
body: { status: 'ok' },
|
|
||||||
statusCode: 200
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -23,9 +23,7 @@ tap.test('should produce instance of Smartlog', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should enable console logging', async () => {
|
tap.test('should enable console logging', async () => {
|
||||||
testSmartLog.enableConsole({
|
testSmartLog.enableConsole();
|
||||||
captureAll: true,
|
|
||||||
});
|
|
||||||
console.log('this is a normal log that should be captured');
|
console.log('this is a normal log that should be captured');
|
||||||
console.log(new Error('hi there'));
|
console.log(new Error('hi there'));
|
||||||
testSmartLog.log('info', 'this should only be printed once');
|
testSmartLog.log('info', 'this should only be printed once');
|
||||||
|
|||||||
@@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartlog',
|
name: '@push.rocks/smartlog',
|
||||||
version: '3.1.2',
|
version: '3.2.0',
|
||||||
description: 'A minimalistic, distributed, and extensible logging tool supporting centralized log management.'
|
description: 'A minimalistic, distributed, and extensible logging tool supporting centralized log management.'
|
||||||
}
|
}
|
||||||
|
|||||||
67
ts_destination_buffer/classes.destinationbuffer.ts
Normal file
67
ts_destination_buffer/classes.destinationbuffer.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import type { ILogDestination, ILogPackage, TLogLevel } from '../dist_ts_interfaces/index.js';
|
||||||
|
|
||||||
|
export interface IDestinationBufferOptions {
|
||||||
|
maxEntries?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBufferQueryOptions {
|
||||||
|
level?: TLogLevel | TLogLevel[];
|
||||||
|
search?: string;
|
||||||
|
limit?: number;
|
||||||
|
offset?: number;
|
||||||
|
since?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SmartlogDestinationBuffer implements ILogDestination {
|
||||||
|
private logPackages: ILogPackage[] = [];
|
||||||
|
private maxEntries: number;
|
||||||
|
|
||||||
|
constructor(options?: IDestinationBufferOptions) {
|
||||||
|
this.maxEntries = options?.maxEntries ?? 2000;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async handleLog(logPackage: ILogPackage): Promise<void> {
|
||||||
|
this.logPackages.push(logPackage);
|
||||||
|
if (this.logPackages.length > this.maxEntries) {
|
||||||
|
this.logPackages.shift();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getEntries(options?: IBufferQueryOptions): ILogPackage[] {
|
||||||
|
const limit = options?.limit ?? 100;
|
||||||
|
const offset = options?.offset ?? 0;
|
||||||
|
|
||||||
|
let results = this.logPackages;
|
||||||
|
|
||||||
|
// Filter by level
|
||||||
|
if (options?.level) {
|
||||||
|
const levels = Array.isArray(options.level) ? options.level : [options.level];
|
||||||
|
results = results.filter((pkg) => levels.includes(pkg.level));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter by search (message content)
|
||||||
|
if (options?.search) {
|
||||||
|
const searchLower = options.search.toLowerCase();
|
||||||
|
results = results.filter((pkg) => pkg.message.toLowerCase().includes(searchLower));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter by timestamp
|
||||||
|
if (options?.since) {
|
||||||
|
results = results.filter((pkg) => pkg.timestamp >= options.since);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return newest-first, with pagination
|
||||||
|
return results
|
||||||
|
.slice()
|
||||||
|
.reverse()
|
||||||
|
.slice(offset, offset + limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getEntryCount(): number {
|
||||||
|
return this.logPackages.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public clear(): void {
|
||||||
|
this.logPackages = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
1
ts_destination_buffer/index.ts
Normal file
1
ts_destination_buffer/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { SmartlogDestinationBuffer, type IDestinationBufferOptions, type IBufferQueryOptions } from './classes.destinationbuffer.js';
|
||||||
3
ts_destination_buffer/plugins.ts
Normal file
3
ts_destination_buffer/plugins.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import * as smartlogInterfaces from '../dist_ts_interfaces/index.js';
|
||||||
|
|
||||||
|
export { smartlogInterfaces };
|
||||||
@@ -61,19 +61,22 @@ export class DestinationLocal implements ILogDestination {
|
|||||||
|
|
||||||
// default logging
|
// default logging
|
||||||
private logToConsole(logPackageArg: ILogPackage) {
|
private logToConsole(logPackageArg: ILogPackage) {
|
||||||
let logString: string;
|
|
||||||
try {
|
try {
|
||||||
logString =
|
const style = this.localBl[logPackageArg.level] ?? this.localBl.info;
|
||||||
this.localBl[logPackageArg.level].prefix +
|
const logString =
|
||||||
|
style.prefix +
|
||||||
plugins.consolecolor.coloredString(
|
plugins.consolecolor.coloredString(
|
||||||
logPackageArg.message,
|
logPackageArg.message,
|
||||||
this.localBl[logPackageArg.level].textColor
|
style.textColor
|
||||||
);
|
);
|
||||||
console.log(logString);
|
console.log(logString);
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// typo fix: use the defined error.prefix, not a non-existent errorPrefix
|
||||||
console.log(
|
console.log(
|
||||||
this.localBl.errorPrefix + 'You seem to have tried logging something strange' + error
|
this.localBl.error.prefix +
|
||||||
|
'You seem to have tried logging something strange ' +
|
||||||
|
error
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -89,6 +92,10 @@ export class DestinationLocal implements ILogDestination {
|
|||||||
prefix: plugins.consolecolor.coloredString(' silly ', 'white', 'blue') + ' ',
|
prefix: plugins.consolecolor.coloredString(' silly ', 'white', 'blue') + ' ',
|
||||||
textColor: 'blue',
|
textColor: 'blue',
|
||||||
},
|
},
|
||||||
|
debug: {
|
||||||
|
prefix: plugins.consolecolor.coloredString(' debug ', 'pink', 'black') + ' ',
|
||||||
|
textColor: 'pink',
|
||||||
|
},
|
||||||
error: {
|
error: {
|
||||||
prefix:
|
prefix:
|
||||||
plugins.consolecolor.coloredString(' ', 'red', 'red') +
|
plugins.consolecolor.coloredString(' ', 'red', 'red') +
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export interface ISmartlogDestinationReceiverConstructorOptions {
|
|||||||
|
|
||||||
export class SmartlogDestinationReceiver implements ILogDestination {
|
export class SmartlogDestinationReceiver implements ILogDestination {
|
||||||
private options: ISmartlogDestinationReceiverConstructorOptions;
|
private options: ISmartlogDestinationReceiverConstructorOptions;
|
||||||
private webrequest = new plugins.webrequest.WebRequest();
|
private webrequest = new plugins.webrequest.WebrequestClient();
|
||||||
|
|
||||||
constructor(optionsArg: ISmartlogDestinationReceiverConstructorOptions) {
|
constructor(optionsArg: ISmartlogDestinationReceiverConstructorOptions) {
|
||||||
this.options = optionsArg;
|
this.options = optionsArg;
|
||||||
@@ -30,6 +30,6 @@ export class SmartlogDestinationReceiver implements ILogDestination {
|
|||||||
}
|
}
|
||||||
this.errorCounter++;
|
this.errorCounter++;
|
||||||
});
|
});
|
||||||
return response.body;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user