tracelog-client-sdk 0.2.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -46
- package/dist/core/TraceLog.d.ts +17 -0
- package/dist/core/TraceLog.js +54 -0
- package/dist/index.d.ts +2 -9
- package/dist/index.js +5 -44
- package/dist/middlewares/express.d.ts +3 -0
- package/dist/middlewares/express.js +26 -0
- package/dist/utils/redactor.d.ts +1 -0
- package/dist/utils/redactor.js +22 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,77 +1,146 @@
|
|
|
1
1
|
# tracelog-client-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The official, high-performance Node.js client library for the TraceLog platform.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
TraceLog SDK goes beyond simple logging. It automatically tracks HTTP request durations, redacts sensitive data (like passwords) before they leave your server, and provides global context management for advanced filtering.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
---
|
|
9
|
-
-Simple and intuitive API (.info(), .error())
|
|
7
|
+
Say goodbye to complex manual logging and missing performance metrics!
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
## Features
|
|
12
10
|
|
|
13
|
-
-
|
|
11
|
+
- **Express HTTP Middleware:** Automatically tracks HTTP method, URL, status code, client IP, and exact request duration (ms).
|
|
12
|
+
- **Auto-Redaction (Security First):** Automatically masks sensitive keys (e.g., `password`, `token`, `credit_card`) with `[REDACTED]` before sending data to the backend.
|
|
13
|
+
- **Global Context:** Inject custom data (like environment, app version, or user IDs) into all logs automatically.
|
|
14
|
+
- **Graceful Shutdown:** Catches `SIGTERM/SIGINT` to securely log system shutdowns before your Node.js process exits.
|
|
15
|
+
- **TypeScript Ready:** Built with TypeScript, providing out-of-the-box type safety, intelligent autocompletion, and zero friction.
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
---
|
|
16
18
|
|
|
17
19
|
## Installation
|
|
20
|
+
|
|
18
21
|
You can install the package in your project using NPM or Yarn:
|
|
19
|
-
|
|
22
|
+
|
|
23
|
+
```bash
|
|
20
24
|
npm install tracelog-client-sdk
|
|
21
25
|
```
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
## Quick Start
|
|
27
|
+
1. Initialization
|
|
28
|
+
Import the SDK and initialize it with your Project ID and API Key.
|
|
29
|
+
|
|
30
|
+
### JavaScript (CommonJS):
|
|
24
31
|
|
|
25
|
-
JavaScript
|
|
26
|
-
--
|
|
32
|
+
### JavaScript
|
|
27
33
|
```
|
|
34
|
+
const { TraceLog } = require('tracelog-client-sdk');
|
|
28
35
|
|
|
29
|
-
|
|
30
|
-
|
|
36
|
+
const logger = new TraceLog({
|
|
37
|
+
projectId: 'YOUR_PROJECT_ID',
|
|
38
|
+
apiKey: 'YOUR_API_KEY',
|
|
39
|
+
backendUrl: '[https://api.yourdomain.com](https://api.yourdomain.com)' // Optional: Defaults to localhost for local testing
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
### TypeScript (ES Modules):
|
|
31
43
|
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
### TypeScript
|
|
45
|
+
```
|
|
46
|
+
import { TraceLog } from 'tracelog-client-sdk';
|
|
34
47
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
48
|
+
const logger = new TraceLog({
|
|
49
|
+
projectId: 'YOUR_PROJECT_ID',
|
|
50
|
+
apiKey: 'YOUR_API_KEY'
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
---
|
|
54
|
+
### 2. Basic Logging
|
|
55
|
+
You can log messages with standard levels (info, warn, error, fatal, debug) and attach custom metadata.
|
|
38
56
|
|
|
39
|
-
|
|
57
|
+
### JavaScript
|
|
58
|
+
```
|
|
59
|
+
// A log at the information level
|
|
60
|
+
logger.log('info', 'User successfully authenticated', {
|
|
61
|
+
userId: 'user_123',
|
|
62
|
+
method: 'OAuth2'
|
|
63
|
+
});
|
|
40
64
|
|
|
41
|
-
//
|
|
42
|
-
logger.
|
|
43
|
-
|
|
44
|
-
|
|
65
|
+
// The SDK automatically masks sensitive fields!
|
|
66
|
+
logger.log('warn', 'Failed login attempt', {
|
|
67
|
+
email: 'test@test.com',
|
|
68
|
+
password: 'my_secret_password' // This will securely be sent as [REDACTED]
|
|
45
69
|
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Advanced Features
|
|
73
|
+
Auto HTTP Tracking (Express Middleware)
|
|
74
|
+
Track every single request in your Express app without writing manual logs. It calculates the exact response time (durationMs) and detects errors automatically.
|
|
46
75
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
76
|
+
### JavaScript
|
|
77
|
+
```
|
|
78
|
+
const express = require('express');
|
|
79
|
+
const { TraceLog, expressMiddleware } = require('tracelog-client-sdk');
|
|
80
|
+
|
|
81
|
+
const app = express();
|
|
82
|
+
const logger = new TraceLog({ projectId: '123', apiKey: 'sk_...' });
|
|
83
|
+
|
|
84
|
+
// Add this before your routes to track all incoming traffic!
|
|
85
|
+
app.use(expressMiddleware(logger));
|
|
86
|
+
|
|
87
|
+
app.get('/', (req, res) => {
|
|
88
|
+
res.send('TraceLog is watching this route!');
|
|
55
89
|
});
|
|
56
|
-
|
|
90
|
+
|
|
91
|
+
app.listen(8080);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Global Context Management
|
|
95
|
+
If you want to attach specific data (like the current environment or app version) to every log sent from your application, use the context manager.
|
|
96
|
+
|
|
97
|
+
### JavaScript
|
|
98
|
+
```
|
|
99
|
+
// Set it once (e.g., on app startup or user login)
|
|
100
|
+
logger.setContext({
|
|
101
|
+
environment: 'production',
|
|
102
|
+
appVersion: '2.1.0'
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// All subsequent logs will automatically include { environment: 'production', appVersion: '2.1.0' }.
|
|
106
|
+
logger.log('info', 'Database connected successfully');
|
|
107
|
+
|
|
108
|
+
// To clear context when needed:
|
|
109
|
+
// logger.clearContext();
|
|
57
110
|
```
|
|
58
|
-
Sending logs is done asynchronously in the background and does not prevent your main application from running.
|
|
59
111
|
|
|
60
112
|
## API Reference
|
|
61
|
-
|
|
62
|
-
|
|
113
|
+
```
|
|
114
|
+
new TraceLog(options)
|
|
115
|
+
```
|
|
116
|
+
### Starts the SDK client.
|
|
117
|
+
|
|
118
|
+
- ***projectId (string | number, Required):*** Your TraceLog project ID.
|
|
63
119
|
|
|
64
|
-
-apiKey (string, Required)
|
|
120
|
+
- ***apiKey (string, Required):*** Your secure API key obtained from your TraceLog panel.
|
|
65
121
|
|
|
66
|
-
|
|
67
|
-
|
|
122
|
+
- ***backendUrl (string, Optional):*** Custom backend URL for self-hosted instances.
|
|
123
|
+
```
|
|
124
|
+
logger.log(level, message, [metadata])
|
|
125
|
+
```
|
|
126
|
+
### Sends an asynchronous log to the TraceLog API.
|
|
127
|
+
---
|
|
128
|
+
- ***level (string, Required):*** 'info' | 'warn' | 'error' | 'fatal' | 'debug'
|
|
129
|
+
|
|
130
|
+
- ***message (string, Required):*** The main log message.
|
|
131
|
+
|
|
132
|
+
- ***metadata (object, Optional):*** Extra JSON data to be added to the log.
|
|
133
|
+
```
|
|
134
|
+
logger.setContext(contextData)
|
|
135
|
+
```
|
|
68
136
|
|
|
69
|
-
|
|
137
|
+
### Appends default metadata to all future logs.
|
|
138
|
+
---
|
|
139
|
+
- ***contextData (object, Required):*** JSON object containing the context data.
|
|
140
|
+
```
|
|
141
|
+
expressMiddleware(loggerInstance)
|
|
142
|
+
```
|
|
143
|
+
Returns an Express middleware function that auto-logs HTTP traffic, calculates durations, and maps HTTP status codes to log levels.
|
|
70
144
|
|
|
71
|
-
-metadata (object, Optional): Extra JSON data to be added to the log (eg: user ID, session ID, etc.).
|
|
72
|
-
-.error(message, [metadata])
|
|
73
|
-
It sends a log at error level.
|
|
74
145
|
|
|
75
|
-
-message (string, Required): Error message.
|
|
76
146
|
|
|
77
|
-
-metadata (object, Optional): Extra context information about the error (e.g. stack trace, request ID, etc.).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface TraceLogOptions {
|
|
2
|
+
projectId: string;
|
|
3
|
+
apiKey: string;
|
|
4
|
+
backendUrl?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class TraceLog {
|
|
7
|
+
private projectId;
|
|
8
|
+
private apiKey;
|
|
9
|
+
private backendUrl;
|
|
10
|
+
private globalContext;
|
|
11
|
+
private sensitiveKeys;
|
|
12
|
+
constructor(options: TraceLogOptions);
|
|
13
|
+
setContext(contextData: Record<string, any>): void;
|
|
14
|
+
clearContext(): void;
|
|
15
|
+
log(level: 'info' | 'warn' | 'error' | 'fatal' | 'debug', message: string, metadata?: any): Promise<void>;
|
|
16
|
+
private setupGracefulShutdown;
|
|
17
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TraceLog = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const redactor_1 = require("../utils/redactor");
|
|
9
|
+
class TraceLog {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.globalContext = {};
|
|
12
|
+
this.sensitiveKeys = ['password', 'token', 'secret', 'credit_card', 'authorization', 'ssn'];
|
|
13
|
+
this.projectId = options.projectId.toString();
|
|
14
|
+
this.apiKey = options.apiKey.replace('ApiKey ', '').trim();
|
|
15
|
+
this.backendUrl = options.backendUrl || 'http://localhost:3001';
|
|
16
|
+
this.setupGracefulShutdown();
|
|
17
|
+
}
|
|
18
|
+
setContext(contextData) {
|
|
19
|
+
this.globalContext = { ...this.globalContext, ...contextData };
|
|
20
|
+
}
|
|
21
|
+
clearContext() {
|
|
22
|
+
this.globalContext = {};
|
|
23
|
+
}
|
|
24
|
+
async log(level, message, metadata = {}) {
|
|
25
|
+
try {
|
|
26
|
+
const safeMetadata = (0, redactor_1.maskSensitiveData)({ ...this.globalContext, ...metadata }, this.sensitiveKeys);
|
|
27
|
+
await axios_1.default.post(`${this.backendUrl}/log/ingest`, {
|
|
28
|
+
level,
|
|
29
|
+
message,
|
|
30
|
+
metadata: safeMetadata,
|
|
31
|
+
timestamp: new Date().toISOString(),
|
|
32
|
+
projectId: this.projectId
|
|
33
|
+
}, {
|
|
34
|
+
headers: {
|
|
35
|
+
'Authorization': `ApiKey ${this.apiKey}`,
|
|
36
|
+
'x-project-id': this.projectId,
|
|
37
|
+
'Content-Type': 'application/json',
|
|
38
|
+
},
|
|
39
|
+
timeout: 5000,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error('[TraceLog SDK Error]: Log could not be sent:', error.message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
setupGracefulShutdown() {
|
|
47
|
+
const handleExit = async (signal) => {
|
|
48
|
+
await this.log('fatal', `System Shutdown Triggered: ${signal}`, { action: 'shutdown' });
|
|
49
|
+
};
|
|
50
|
+
process.on('SIGTERM', () => handleExit('SIGTERM'));
|
|
51
|
+
process.on('SIGINT', () => handleExit('SIGINT'));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.TraceLog = TraceLog;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
private httpClient;
|
|
4
|
-
private baseUrl;
|
|
5
|
-
constructor(apikey: string);
|
|
6
|
-
info(message: string, metadata?: object): void;
|
|
7
|
-
error(message: string, metadata?: object): void;
|
|
8
|
-
private sendLog;
|
|
9
|
-
}
|
|
1
|
+
export { TraceLog, TraceLogOptions } from './core/TraceLog';
|
|
2
|
+
export { expressMiddleware } from './middlewares/express';
|
package/dist/index.js
CHANGED
|
@@ -1,46 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (!apikey) {
|
|
12
|
-
throw new Error('[TraceLog] API key is required.');
|
|
13
|
-
}
|
|
14
|
-
this.apiKey = apikey;
|
|
15
|
-
this.httpClient = axios_1.default.create({
|
|
16
|
-
baseURL: this.baseUrl,
|
|
17
|
-
headers: {
|
|
18
|
-
"Authorization": `ApiKey ${this.apiKey}`,
|
|
19
|
-
"Content-Type": "application/json",
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
info(message, metadata = {}) {
|
|
24
|
-
this.sendLog('info', message, metadata);
|
|
25
|
-
}
|
|
26
|
-
error(message, metadata = {}) {
|
|
27
|
-
this.sendLog('error', message, metadata);
|
|
28
|
-
}
|
|
29
|
-
async sendLog(level, message, metadata) {
|
|
30
|
-
const payload = {
|
|
31
|
-
message,
|
|
32
|
-
level,
|
|
33
|
-
metadata,
|
|
34
|
-
timestamp: new Date().toISOString()
|
|
35
|
-
};
|
|
36
|
-
try {
|
|
37
|
-
await this.httpClient.post('/ingest', payload);
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
if (axios_1.default.isAxiosError(error)) {
|
|
41
|
-
console.error('[TraceLog Error] Sunucuya ulaşılamadı:', error.message);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
exports.LogStreamer = LogStreamer;
|
|
3
|
+
exports.expressMiddleware = exports.TraceLog = void 0;
|
|
4
|
+
var TraceLog_1 = require("./core/TraceLog");
|
|
5
|
+
Object.defineProperty(exports, "TraceLog", { enumerable: true, get: function () { return TraceLog_1.TraceLog; } });
|
|
6
|
+
var express_1 = require("./middlewares/express");
|
|
7
|
+
Object.defineProperty(exports, "expressMiddleware", { enumerable: true, get: function () { return express_1.expressMiddleware; } });
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.expressMiddleware = void 0;
|
|
4
|
+
const expressMiddleware = (logger) => {
|
|
5
|
+
return (req, res, next) => {
|
|
6
|
+
const startTime = Date.now();
|
|
7
|
+
res.on('finish', () => {
|
|
8
|
+
const duration = Date.now() - startTime;
|
|
9
|
+
let level = 'info';
|
|
10
|
+
if (res.statusCode >= 400 && res.statusCode < 500)
|
|
11
|
+
level = 'warn';
|
|
12
|
+
if (res.statusCode >= 500)
|
|
13
|
+
level = 'error';
|
|
14
|
+
logger.log(level, `HTTP ${req.method} ${req.originalUrl}`, {
|
|
15
|
+
http: {
|
|
16
|
+
method: req.method,
|
|
17
|
+
url: req.originalUrl,
|
|
18
|
+
statusCode: res.statusCode,
|
|
19
|
+
duration: duration,
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
next();
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
exports.expressMiddleware = expressMiddleware;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const maskSensitiveData: (data: any, sensitiveKeys: string[]) => any;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.maskSensitiveData = void 0;
|
|
4
|
+
const maskSensitiveData = (data, sensitiveKeys) => {
|
|
5
|
+
if (!data || typeof data !== 'object')
|
|
6
|
+
return data;
|
|
7
|
+
const maskedData = Array.isArray(data) ? [...data] : { ...data };
|
|
8
|
+
for (const key in maskedData) {
|
|
9
|
+
if (Object.prototype.hasOwnProperty.call(maskedData, key)) {
|
|
10
|
+
const lowerKey = key.toLocaleLowerCase();
|
|
11
|
+
const isSensitive = sensitiveKeys.some(sk => lowerKey.includes(sk));
|
|
12
|
+
if (isSensitive) {
|
|
13
|
+
maskedData[key] = '[REDACTED]';
|
|
14
|
+
}
|
|
15
|
+
else if (typeof maskedData[key] === 'object') {
|
|
16
|
+
maskedData[key] = (0, exports.maskSensitiveData)(maskedData[key], sensitiveKeys);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return maskedData;
|
|
21
|
+
};
|
|
22
|
+
exports.maskSensitiveData = maskSensitiveData;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tracelog-client-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Views of the official SDK for the TraceLog SaaS platform",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -24,9 +24,11 @@
|
|
|
24
24
|
"author": "Yakup Hadutoğlu <yakuphad2004@gmail.com>",
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"axios": "^1.13.2"
|
|
27
|
+
"axios": "^1.13.2",
|
|
28
|
+
"express": "^5.2.1"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
31
|
+
"@types/express": "^5.0.6",
|
|
30
32
|
"@types/node": "^24.10.1"
|
|
31
33
|
}
|
|
32
34
|
}
|