unnbound-logger-sdk 3.0.10 → 3.0.12
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 +78 -17
- package/dist/axios.d.ts +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/internal.d.ts +1 -3
- package/dist/internal.js +6 -1
- package/dist/logger.js +20 -1
- package/dist/types.d.ts +13 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,19 +5,20 @@ A structured logging library with TypeScript support built on Pino. Provides con
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install unnbound-logger
|
|
8
|
+
npm install unnbound-logger-sdk
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Basic Usage
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
|
-
import { logger } from 'unnbound-logger';
|
|
14
|
+
import { logger } from 'unnbound-logger-sdk';
|
|
15
15
|
|
|
16
16
|
// Log with string messages
|
|
17
17
|
logger.info('Application started');
|
|
18
18
|
logger.warn('Resource usage high');
|
|
19
19
|
logger.error({ err: new Error('Database connection failed') }, 'Something bad happened');
|
|
20
20
|
logger.debug('Debug information');
|
|
21
|
+
logger.trace('Trace information');
|
|
21
22
|
|
|
22
23
|
// Log with object messages (merged into top level)
|
|
23
24
|
logger.info({ event: 'user_login', userId: '123' }, 'Event received.');
|
|
@@ -34,17 +35,20 @@ All logs follow a standardized format based on Pino with additional context:
|
|
|
34
35
|
|
|
35
36
|
```typescript
|
|
36
37
|
interface Log<T extends LogType = 'general'> {
|
|
37
|
-
|
|
38
|
+
logId: string; // Automatically generated unique ID for each log entry
|
|
39
|
+
level: LogLevel; // "info" | "debug" | "error" | "warn" | "trace"
|
|
38
40
|
message: string;
|
|
41
|
+
type: T; // "general" | "http" | "sftp"
|
|
39
42
|
traceId?: string; // Automatically included when in trace context
|
|
40
43
|
spanId?: string; // Automatically included when in span context
|
|
41
|
-
type?: T; // "general" | "http" | "sftp" | "dbQueryTransaction"
|
|
42
44
|
serviceId?: string; // From UNNBOUND_SERVICE_ID environment variable
|
|
43
45
|
deploymentId?: string; // From UNNBOUND_DEPLOYMENT_ID environment variable
|
|
44
46
|
workflowId?: string; // From UNNBOUND_WORKFLOW_ID environment variable
|
|
45
47
|
environment?: string; // From ENVIRONMENT environment variable
|
|
46
48
|
err?: unknown; // Only present for Error objects
|
|
47
49
|
duration?: number; // Duration in milliseconds for span operations
|
|
50
|
+
http?: T extends 'http' ? HttpPayload : never;
|
|
51
|
+
sftp?: T extends 'sftp' ? SftpPayload : never;
|
|
48
52
|
}
|
|
49
53
|
```
|
|
50
54
|
|
|
@@ -68,7 +72,7 @@ UNNBOUND_SERVICE_ID=order-service
|
|
|
68
72
|
|
|
69
73
|
```typescript
|
|
70
74
|
// Import the logger - workflowId and serviceId are automatically set from environment
|
|
71
|
-
import { logger } from 'unnbound-logger';
|
|
75
|
+
import { logger } from 'unnbound-logger-sdk';
|
|
72
76
|
```
|
|
73
77
|
|
|
74
78
|
### Deployment Tracking
|
|
@@ -128,7 +132,7 @@ This follows Pino's standard behavior where all object properties are merged int
|
|
|
128
132
|
## HTTP Request/Response Logging
|
|
129
133
|
|
|
130
134
|
```typescript
|
|
131
|
-
import { logger, traceMiddleware } from 'unnbound-logger';
|
|
135
|
+
import { logger, traceMiddleware } from 'unnbound-logger-sdk';
|
|
132
136
|
import express from 'express';
|
|
133
137
|
|
|
134
138
|
const app = express();
|
|
@@ -169,7 +173,7 @@ export UNNBOUND_WORKFLOW_URL="https://api.yourservice.com"
|
|
|
169
173
|
|
|
170
174
|
```typescript
|
|
171
175
|
import express from 'express';
|
|
172
|
-
import { traceMiddleware } from 'unnbound-logger';
|
|
176
|
+
import { traceMiddleware } from 'unnbound-logger-sdk';
|
|
173
177
|
|
|
174
178
|
const app = express();
|
|
175
179
|
|
|
@@ -190,6 +194,45 @@ app.post('/webhooks/github', (req, res) => {
|
|
|
190
194
|
|
|
191
195
|
This ensures webhook logs contain the complete URL for easy debugging and monitoring.
|
|
192
196
|
|
|
197
|
+
## SFTP Operations Logging
|
|
198
|
+
|
|
199
|
+
The logger supports structured logging for SFTP operations with automatic span tracking:
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
import { logger, startSpan } from 'unnbound-logger-sdk';
|
|
203
|
+
|
|
204
|
+
// Example SFTP operation with automatic logging
|
|
205
|
+
const uploadFile = async (filePath: string, content: string) => {
|
|
206
|
+
return await startSpan(
|
|
207
|
+
'SFTP upload operation',
|
|
208
|
+
async () => {
|
|
209
|
+
// Your SFTP upload logic here
|
|
210
|
+
logger.info('Uploading file', { filePath, contentLength: content.length });
|
|
211
|
+
return { success: true, filePath };
|
|
212
|
+
},
|
|
213
|
+
(result) => ({
|
|
214
|
+
type: 'sftp',
|
|
215
|
+
sftp: {
|
|
216
|
+
host: 'sftp.example.com',
|
|
217
|
+
operation: 'upload',
|
|
218
|
+
path: filePath,
|
|
219
|
+
content: content,
|
|
220
|
+
bytes: content.length,
|
|
221
|
+
},
|
|
222
|
+
})
|
|
223
|
+
);
|
|
224
|
+
};
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
The SFTP logging automatically captures:
|
|
228
|
+
|
|
229
|
+
- Host information
|
|
230
|
+
- Operation type (connect, upload, download, list, delete, etc.)
|
|
231
|
+
- File paths and content
|
|
232
|
+
- Byte counts for transfers
|
|
233
|
+
- Operation duration
|
|
234
|
+
- Success/failure status
|
|
235
|
+
|
|
193
236
|
## Middleware Usage
|
|
194
237
|
|
|
195
238
|
### Express Trace Middleware
|
|
@@ -197,7 +240,7 @@ This ensures webhook logs contain the complete URL for easy debugging and monito
|
|
|
197
240
|
The library provides a comprehensive trace middleware for Express applications that automatically handles trace context and HTTP logging:
|
|
198
241
|
|
|
199
242
|
```typescript
|
|
200
|
-
import { traceMiddleware } from 'unnbound-logger';
|
|
243
|
+
import { traceMiddleware } from 'unnbound-logger-sdk';
|
|
201
244
|
import express from 'express';
|
|
202
245
|
|
|
203
246
|
const app = express();
|
|
@@ -221,7 +264,7 @@ The trace middleware automatically:
|
|
|
221
264
|
For comprehensive logging of outgoing HTTP requests made with Axios:
|
|
222
265
|
|
|
223
266
|
```typescript
|
|
224
|
-
import { traceAxios } from 'unnbound-logger';
|
|
267
|
+
import { traceAxios } from 'unnbound-logger-sdk';
|
|
225
268
|
import axios from 'axios';
|
|
226
269
|
|
|
227
270
|
// Create an axios instance and wrap it with tracing
|
|
@@ -245,7 +288,7 @@ The Axios middleware:
|
|
|
245
288
|
In case the function doesn't run inside an HTTP handler (for example a cron job), you can assign a trace id manually by using the `withTrace` function:
|
|
246
289
|
|
|
247
290
|
```typescript
|
|
248
|
-
import { withTrace } from 'unnbound-logger';
|
|
291
|
+
import { withTrace } from 'unnbound-logger-sdk';
|
|
249
292
|
|
|
250
293
|
const operation = async (value: number) => {
|
|
251
294
|
// This will log a { traceId, value }
|
|
@@ -261,7 +304,7 @@ setInterval(() => withTrace(() => operation(13)), 1000);
|
|
|
261
304
|
The `startSpan` function allows you to wrap any async operation with automatic span tracking and logging. This is particularly useful for maintaining consistent trace IDs across async operations and distributed systems:
|
|
262
305
|
|
|
263
306
|
```typescript
|
|
264
|
-
import { logger, startSpan } from 'unnbound-logger';
|
|
307
|
+
import { logger, startSpan } from 'unnbound-logger-sdk';
|
|
265
308
|
|
|
266
309
|
// Example: Wrapping a function with span tracking
|
|
267
310
|
const operation = async (value: number) => {
|
|
@@ -320,17 +363,22 @@ The main logger instance that provides all logging functionality using Pino.
|
|
|
320
363
|
#### Usage
|
|
321
364
|
|
|
322
365
|
```typescript
|
|
323
|
-
import { logger } from 'unnbound-logger';
|
|
366
|
+
import { logger } from 'unnbound-logger-sdk';
|
|
324
367
|
```
|
|
325
368
|
|
|
326
369
|
The logger is a Pino instance with additional context automatically included from environment variables and trace context.
|
|
327
370
|
|
|
328
371
|
#### Methods
|
|
329
372
|
|
|
373
|
+
- `logger.trace(object: {}, message: string): void`
|
|
374
|
+
- `logger.trace(message: string): void`
|
|
375
|
+
- `logger.debug(object: {}, message: string): void`
|
|
376
|
+
- `logger.debug(message: string): void`
|
|
330
377
|
- `logger.info(object: {}, message: string): void`
|
|
378
|
+
- `logger.info(message: string): void`
|
|
331
379
|
- `logger.warn(object: {}, message: string): void`
|
|
332
|
-
- `logger.
|
|
333
|
-
- `logger.
|
|
380
|
+
- `logger.warn(message: string): void`
|
|
381
|
+
- `logger.error<O extends { err: unknown }>(object: O, message: string): void`
|
|
334
382
|
|
|
335
383
|
### traceMiddleware
|
|
336
384
|
|
|
@@ -339,7 +387,7 @@ Express middleware for automatic HTTP request/response logging and trace context
|
|
|
339
387
|
#### Usage
|
|
340
388
|
|
|
341
389
|
```typescript
|
|
342
|
-
import { traceMiddleware } from 'unnbound-logger';
|
|
390
|
+
import { traceMiddleware } from 'unnbound-logger-sdk';
|
|
343
391
|
|
|
344
392
|
app.use(traceMiddleware(options?: HttpOptions));
|
|
345
393
|
```
|
|
@@ -356,7 +404,7 @@ Wraps an Axios instance with automatic request/response logging and trace contex
|
|
|
356
404
|
#### Usage
|
|
357
405
|
|
|
358
406
|
```typescript
|
|
359
|
-
import { traceAxios } from 'unnbound-logger';
|
|
407
|
+
import { traceAxios } from 'unnbound-logger-sdk';
|
|
360
408
|
import axios from 'axios';
|
|
361
409
|
|
|
362
410
|
const client = traceAxios(axios.create(), options?: HttpOptions);
|
|
@@ -369,7 +417,7 @@ Creates a span for tracking async operations with automatic logging and duration
|
|
|
369
417
|
#### Usage
|
|
370
418
|
|
|
371
419
|
```typescript
|
|
372
|
-
import { startSpan } from 'unnbound-logger';
|
|
420
|
+
import { startSpan } from 'unnbound-logger-sdk';
|
|
373
421
|
|
|
374
422
|
const result = await startSpan<T>(
|
|
375
423
|
spanName: string,
|
|
@@ -384,6 +432,19 @@ const result = await startSpan<T>(
|
|
|
384
432
|
- `callback`: The async function to execute within the span
|
|
385
433
|
- `getter`: Optional function to generate log payload based on operation result/error
|
|
386
434
|
|
|
435
|
+
### getTraceId
|
|
436
|
+
|
|
437
|
+
Generates a new trace ID for manual trace context management.
|
|
438
|
+
|
|
439
|
+
#### Usage
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
import { getTraceId } from 'unnbound-logger-sdk';
|
|
443
|
+
|
|
444
|
+
const traceId = getTraceId();
|
|
445
|
+
console.log(traceId); // "550e8400-e29b-41d4-a716-446655440000"
|
|
446
|
+
```
|
|
447
|
+
|
|
387
448
|
### Environment Variables
|
|
388
449
|
|
|
389
450
|
- `UNNBOUND_WORKFLOW_ID` - Workflow identifier (included in all logs)
|
package/dist/axios.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from 'axios';
|
|
2
2
|
import { HttpOptions } from './types';
|
|
3
3
|
type GetPayload = (config: AxiosRequestConfig, res?: AxiosResponse) => object;
|
|
4
4
|
/**
|
|
@@ -7,5 +7,5 @@ type GetPayload = (config: AxiosRequestConfig, res?: AxiosResponse) => object;
|
|
|
7
7
|
* @param options - Configuration options for HTTP tracing
|
|
8
8
|
* @returns The wrapped axios instance with span tracking
|
|
9
9
|
*/
|
|
10
|
-
export declare const traceAxios: (client:
|
|
10
|
+
export declare const traceAxios: (client: AxiosInstance, { ignoreTraceRoutes, traceHeaderKey, getPayload, }?: HttpOptions<GetPayload>) => AxiosInstance;
|
|
11
11
|
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* A structured logging library built on Pino with TypeScript support.
|
|
5
5
|
* Provides consistent, well-typed logging across different operational contexts.
|
|
6
6
|
*/
|
|
7
|
-
export type { LogLevel, LogType, HttpMethod, Log, HttpPayload, SftpPayload } from './types';
|
|
7
|
+
export type { LogLevel, LogType, HttpMethod, Log, HttpPayload, SftpPayload, EdiPayload, EdiOperation, EdiFormat, EdiX12Payload, EdiX12Operation, } from './types';
|
|
8
8
|
export type { UnnboundLogger } from './logger';
|
|
9
9
|
export { logger } from './logger';
|
|
10
10
|
export { withTrace, getTraceId } from './trace';
|
package/dist/internal.d.ts
CHANGED
package/dist/internal.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.internal = void 0;
|
|
4
|
-
const
|
|
4
|
+
const o = { _internal: true };
|
|
5
|
+
// Internal logs should be hidden in sandbox
|
|
6
|
+
// TODO: Find a better way to detect sandbox
|
|
7
|
+
if (process.env.UNNBOUND_IDLE_TIMEOUT)
|
|
8
|
+
o._hide = true;
|
|
9
|
+
const internal = () => o;
|
|
5
10
|
exports.internal = internal;
|
package/dist/logger.js
CHANGED
|
@@ -7,8 +7,10 @@ exports.logger = void 0;
|
|
|
7
7
|
const pino_1 = __importDefault(require("pino"));
|
|
8
8
|
const uuid_1 = require("uuid");
|
|
9
9
|
const storage_1 = require("./storage");
|
|
10
|
+
const levels = new Set(['debug', 'info', 'warn', 'error']);
|
|
11
|
+
const hidden = (data) => '_hide' in data && data._hide;
|
|
10
12
|
exports.logger = (0, pino_1.default)({
|
|
11
|
-
level: process.env.LOG_LEVEL ?? '
|
|
13
|
+
level: process.env.LOG_LEVEL ?? 'debug',
|
|
12
14
|
base: {
|
|
13
15
|
environment: process.env.ENVIRONMENT,
|
|
14
16
|
workflowId: process.env.UNNBOUND_WORKFLOW_ID,
|
|
@@ -28,6 +30,23 @@ exports.logger = (0, pino_1.default)({
|
|
|
28
30
|
formatters: {
|
|
29
31
|
level: (level) => ({ level }),
|
|
30
32
|
},
|
|
33
|
+
hooks: {
|
|
34
|
+
logMethod(args, method) {
|
|
35
|
+
const firstArg = args[0];
|
|
36
|
+
if (!!firstArg && typeof firstArg === 'object') {
|
|
37
|
+
if (hidden(firstArg))
|
|
38
|
+
return;
|
|
39
|
+
// Allow dynamic log level
|
|
40
|
+
if ('level' in firstArg &&
|
|
41
|
+
typeof firstArg.level === 'string' &&
|
|
42
|
+
levels.has(firstArg.level)) {
|
|
43
|
+
method = this[firstArg.level];
|
|
44
|
+
firstArg.level = undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
method.apply(this, args);
|
|
48
|
+
},
|
|
49
|
+
},
|
|
31
50
|
redact: {
|
|
32
51
|
paths: [
|
|
33
52
|
'http.request.headers.authorization',
|
package/dist/types.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export interface Log<T extends LogType = 'general'> {
|
|
|
25
25
|
workflowId?: string;
|
|
26
26
|
http: T extends 'http' ? HttpPayload : never;
|
|
27
27
|
sftp: T extends 'sftp' ? SftpPayload : never;
|
|
28
|
+
edi: T extends 'edi' ? EdiPayload : never;
|
|
28
29
|
duration?: number;
|
|
29
30
|
err?: unknown;
|
|
30
31
|
}
|
|
@@ -53,6 +54,18 @@ export interface SftpPayload {
|
|
|
53
54
|
content?: string;
|
|
54
55
|
exists?: string | false;
|
|
55
56
|
}
|
|
57
|
+
export type EdiX12Operation = 'fromX12' | 'toX12' | 'validateX12' | 'acknowledgeX12';
|
|
58
|
+
export type EdiOperation = EdiX12Operation;
|
|
59
|
+
export type EdiFormat = 'x12';
|
|
60
|
+
export interface EdiX12Payload {
|
|
61
|
+
input: unknown;
|
|
62
|
+
output: unknown;
|
|
63
|
+
}
|
|
64
|
+
export interface EdiPayload {
|
|
65
|
+
operation: EdiOperation;
|
|
66
|
+
type: EdiFormat;
|
|
67
|
+
x12?: EdiX12Payload;
|
|
68
|
+
}
|
|
56
69
|
export interface HttpOptions<G extends Function> {
|
|
57
70
|
ignoreTraceRoutes?: string[];
|
|
58
71
|
traceHeaderKey?: string;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unnbound-logger-sdk",
|
|
3
3
|
"description": "A structured logging library with TypeScript support using Pino. Provides consistent, well-typed logging with automatic logId, workflowId, traceId, and deploymentId tracking across operational contexts.",
|
|
4
|
-
"version": "3.0.
|
|
4
|
+
"version": "3.0.12",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"keywords": [
|