pino-sns-transport
Advanced tools
Comparing version 1.3.1 to 1.3.3
@@ -1,9 +0,6 @@ | ||
import { SnsTransportOptions } from "./types"; | ||
import type { SonicBoom } from "sonic-boom"; | ||
export declare class ErrorHandler { | ||
private readonly opts; | ||
errorLogStream?: SonicBoom; | ||
constructor(opts: SnsTransportOptions); | ||
init(): Promise<void>; | ||
log(error: string, details: any): void; | ||
private liftErrorProperties; | ||
private log; | ||
error(error: string, errorDetails: any, log?: any): void; | ||
fatal(error: string, errorDetails: any, log?: any): void; | ||
} |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ErrorHandler = void 0; | ||
const node_events_1 = require("node:events"); | ||
const json_stringify_safe_1 = __importDefault(require("json-stringify-safe")); | ||
class ErrorHandler { | ||
constructor(opts) { | ||
this.opts = opts; | ||
liftErrorProperties(error) { | ||
return Object.assign({ name: error.name, message: error.message, stack: error.stack }, error); | ||
} | ||
init() { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const writeBackEnabled = (_a = this.opts.writeBackEnabled) !== null && _a !== void 0 ? _a : true; | ||
if (writeBackEnabled) { | ||
try { | ||
const { pino } = yield Promise.resolve().then(() => __importStar(require('pino'))); | ||
const errorLogStream = pino.destination({ | ||
fd: 1, | ||
sync: true, | ||
}); | ||
yield (0, node_events_1.once)(errorLogStream, 'ready'); | ||
this.errorLogStream = errorLogStream; | ||
} | ||
catch (error) { | ||
// Do nothing. It is what it is. | ||
} | ||
} | ||
}); | ||
} | ||
log(error, details) { | ||
var _a; | ||
log(error, errorDetails, level, log) { | ||
try { | ||
(_a = this.errorLogStream) === null || _a === void 0 ? void 0 : _a.write(JSON.stringify({ context: 'pino-sns-transport', error, details }) + '\n'); | ||
const time = Date.now().valueOf(); | ||
const logInfo = { | ||
msg: error, | ||
context: 'pino-sns-transport', | ||
level, | ||
time, | ||
error: this.liftErrorProperties(errorDetails), | ||
failedLog: log, | ||
}; | ||
console.log((0, json_stringify_safe_1.default)(logInfo)); | ||
} | ||
@@ -70,3 +29,9 @@ catch (error) { | ||
} | ||
error(error, errorDetails, log) { | ||
this.log(error, errorDetails, 'error', log); | ||
} | ||
fatal(error, errorDetails, log) { | ||
this.log(error, errorDetails, 'fatal', log); | ||
} | ||
} | ||
exports.ErrorHandler = ErrorHandler; |
@@ -30,4 +30,3 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const errorHandler = new error_handler_1.ErrorHandler(opts); | ||
yield errorHandler.init(); | ||
const errorHandler = new error_handler_1.ErrorHandler(); | ||
try { | ||
@@ -49,10 +48,10 @@ const logFilterer = new log_filter_1.LogFilterer(opts); | ||
_a = false; | ||
let obj = _d; | ||
let log = _d; | ||
try { | ||
if (logFilterer.check(obj)) { | ||
yield publisher.publish(messageFormatter.formatMessage(obj)); | ||
if (logFilterer.check(log)) { | ||
yield publisher.publish(messageFormatter.formatMessage(log)); | ||
} | ||
} | ||
catch (err) { | ||
errorHandler.log('Publishing Message Failed', { err, obj }); | ||
errorHandler.error('Publishing Message Failed', err, log); | ||
} | ||
@@ -72,3 +71,3 @@ } | ||
catch (err) { | ||
errorHandler.log('Transport Initialization Failed', err); | ||
errorHandler.fatal('Transport Initialization Failed', err); | ||
return (0, pino_abstract_transport_1.default)(function (source) { | ||
@@ -82,4 +81,4 @@ var _a, source_2, source_2_1; | ||
_a = false; | ||
let obj = _d; | ||
errorHandler.log('Transport Initialization Failed', { err, obj }); | ||
let log = _d; | ||
errorHandler.fatal('Transport Initialization Failed', err, log); | ||
} | ||
@@ -86,0 +85,0 @@ } |
@@ -19,3 +19,2 @@ import { SNSClientConfig } from "@aws-sdk/client-sns"; | ||
excludeLogs?: LogFilter[]; | ||
writeBackEnabled?: boolean; | ||
}; |
{ | ||
"name": "pino-sns-transport", | ||
"version": "1.3.1", | ||
"version": "1.3.3", | ||
"description": "Simple transport to publish logs to sns topics", | ||
@@ -32,16 +32,11 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"json-stringify-safe": "^5.0.1", | ||
"pino-abstract-transport": "^1.1.0" | ||
}, | ||
"peerDependencies": { | ||
"@aws-sdk/client-sns": "^3.0.0", | ||
"pino": ">=7.0.0" | ||
"@aws-sdk/client-sns": "^3.0.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"pino": { | ||
"optional": true | ||
} | ||
}, | ||
"devDependencies": { | ||
"@types/json-stringify-safe": "^5.0.3", | ||
"@types/node": "^14.18.63", | ||
"sonic-boom": "^3.8.0", | ||
"typescript": "^5.3.3" | ||
@@ -48,0 +43,0 @@ }, |
@@ -90,3 +90,2 @@ # pino-sns-transport | ||
excludeLogs?: LogFilter[]; | ||
writeBackEnabled?: boolean; | ||
} | ||
@@ -106,3 +105,2 @@ ``` | ||
- `excludeLogs` can be used to prevent certain logs from being published whose value at `key` matches the `pattern`. Unlike `excludeKeys`, this does not support dot notation for now | ||
- `writeBackEnabled`, default is true and works only if the pino optional peer dependency is met and there is a file descriptor present on std out. Any errors encountered during initialization of package or publishing messages are written to std out along with log payload. | ||
@@ -112,2 +110,3 @@ ### Nuances | ||
- If both `includeLogs` and `excludeLogs` are specified and a log matches both of them then it will be excluded. | ||
- Any errors encountered in initialization or publishing logs are printed to stdout with the `context` key as `pino-sns-transport` in pino style compatible with pino-pretty. Also includes the error and the original log that could not be published. | ||
@@ -114,0 +113,0 @@ ### Example |
22077
362
141
+ Addedjson-stringify-safe@^5.0.1
+ Addedjson-stringify-safe@5.0.1(transitive)
- Removedatomic-sleep@1.0.0(transitive)
- Removedfast-redact@3.5.0(transitive)
- Removedon-exit-leak-free@2.1.2(transitive)
- Removedpino@9.6.0(transitive)
- Removedpino-abstract-transport@2.0.0(transitive)
- Removedpino-std-serializers@7.0.0(transitive)
- Removedprocess-warning@4.0.1(transitive)
- Removedquick-format-unescaped@4.0.4(transitive)
- Removedreal-require@0.2.0(transitive)
- Removedsafe-stable-stringify@2.5.0(transitive)
- Removedsonic-boom@4.2.0(transitive)
- Removedthread-stream@3.1.0(transitive)