pino-sentry
Advanced tools
Comparing version
@@ -19,4 +19,7 @@ #!/usr/bin/env node | ||
.option('-mb, --maxBreadcrumbs <maxBreadcrumbs>', 'Total amount of breadcrumbs that should be captured') | ||
.option('-di, --dist <dist>', 'Sets the distribution for all events') | ||
.option('--maxValueLength <maxValueLength>', 'Maximum number of chars a single value can have before it will be truncated.') | ||
.option('--release <release>', 'The release identifier used when uploading respective source maps.') | ||
.action(function (_a) { | ||
var dsn = _a.dsn, serverName = _a.serverName, environment = _a.environment, debug = _a.debug, sampleRate = _a.sampleRate, maxBreadcrumbs = _a.maxBreadcrumbs; | ||
var dsn = _a.dsn, serverName = _a.serverName, environment = _a.environment, debug = _a.debug, sampleRate = _a.sampleRate, maxBreadcrumbs = _a.maxBreadcrumbs, dist = _a.dist, logLevel = _a.logLevel, maxValueLength = _a.maxValueLength, release = _a.release; | ||
return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
@@ -36,2 +39,6 @@ var writeStream, error_1; | ||
maxBreadcrumbs: maxBreadcrumbs, | ||
dist: dist, | ||
logLevel: logLevel, | ||
maxValueLength: maxValueLength, | ||
release: release, | ||
})]; | ||
@@ -38,0 +45,0 @@ case 1: |
/// <reference types="node" /> | ||
import stream from 'stream'; | ||
export declare function createWriteStreamAsync(options?: any): Promise<any>; | ||
export declare function createWriteStream(options?: any): stream.Transform; | ||
import * as Sentry from '@sentry/node'; | ||
export declare class PinoSentryTransport { | ||
private SEVERITIES_MAP; | ||
constructor(options?: Sentry.NodeOptions); | ||
getLogSeverity(level: number): Sentry.Severity; | ||
readonly sentry: typeof Sentry; | ||
parse(line: any): void; | ||
transformer(): stream.Transform; | ||
prepareAndGo(chunk: any, cb: any): void; | ||
private withDefaults; | ||
private isObject; | ||
private shouldLogException; | ||
} | ||
export declare function createWriteStreamAsync(options?: Sentry.NodeOptions): PromiseLike<stream.Transform>; | ||
export declare function createWriteStream(options?: Sentry.NodeOptions): stream.Transform & { | ||
transport: PinoSentryTransport; | ||
}; |
@@ -19,2 +19,22 @@ "use strict"; | ||
}(Error)); | ||
function defaults(target, source) { | ||
var e_1, _a; | ||
var ret = tslib_1.__assign({}, target); | ||
try { | ||
for (var _b = tslib_1.__values(Object.keys(source)), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var key = _c.value; | ||
if (target[key] === undefined) { | ||
ret[key] = source[key]; | ||
} | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return ret; | ||
} | ||
var PinoSentryTransport = /** @class */ (function () { | ||
@@ -29,2 +49,10 @@ function PinoSentryTransport(options) { | ||
60: Sentry.Severity.Fatal, | ||
// Support for useLevelLabels | ||
// https://github.com/pinojs/pino/blob/master/docs/api.md#uselevellabels-boolean | ||
trace: Sentry.Severity.Debug, | ||
debug: Sentry.Severity.Debug, | ||
info: Sentry.Severity.Info, | ||
warning: Sentry.Severity.Warning, | ||
error: Sentry.Severity.Error, | ||
fatal: Sentry.Severity.Fatal, | ||
}; | ||
@@ -34,3 +62,3 @@ Sentry.init(this.withDefaults(options || {})); | ||
PinoSentryTransport.prototype.getLogSeverity = function (level) { | ||
return this.SEVERITIES_MAP[level]; | ||
return this.SEVERITIES_MAP[level] || Sentry.Severity.Info; | ||
}; | ||
@@ -46,3 +74,4 @@ Object.defineProperty(PinoSentryTransport.prototype, "sentry", { | ||
var chunk = JSON.parse(line); | ||
var cb = function () { }; | ||
var cb = function () { | ||
}; | ||
this.prepareAndGo(chunk, cb); | ||
@@ -94,10 +123,13 @@ }; | ||
PinoSentryTransport.prototype.withDefaults = function (options) { | ||
return { | ||
dsn: options.dsn || process.env.SENTRY_DSN || '', | ||
serverName: options && options.serverName || 'pino-sentry', | ||
environment: options && options.environment || process.env.SENTRY_ENVIRONMENT || process.env.NODE_ENV || 'production', | ||
debug: options && options.debug || !!process.env.SENTRY_DEBUG || false, | ||
sampleRate: options && options.sampleRate || 1.0, | ||
maxBreadcrumbs: options && options.maxBreadcrumbs || 100, | ||
}; | ||
if (!options) { | ||
options = {}; | ||
} | ||
return defaults(options, { | ||
dsn: process.env.SENTRY_DSN || '', | ||
serverName: 'pino-sentry', | ||
environment: process.env.SENTRY_ENVIRONMENT || process.env.NODE_ENV || 'production', | ||
debug: !!process.env.SENTRY_DEBUG || false, | ||
sampleRate: 1.0, | ||
maxBreadcrumbs: 100, | ||
}); | ||
}; | ||
@@ -113,2 +145,3 @@ PinoSentryTransport.prototype.isObject = function (obj) { | ||
}()); | ||
exports.PinoSentryTransport = PinoSentryTransport; | ||
; | ||
@@ -120,3 +153,2 @@ function createWriteStreamAsync(options) { | ||
} | ||
; | ||
var transport = new PinoSentryTransport(options); | ||
@@ -141,6 +173,5 @@ var sentryTransformer = transport.transformer(); | ||
} | ||
; | ||
var transport = new PinoSentryTransport(options); | ||
var sentryParse = transport.parse.bind(transport); | ||
return split2_1.default(sentryParse); | ||
return Object.assign(split2_1.default(sentryParse), { transport: transport }); | ||
} | ||
@@ -147,0 +178,0 @@ exports.createWriteStream = createWriteStream; |
{ | ||
"name": "pino-sentry", | ||
"description": "@sentry/node transport for pino logger", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"author": "Andrew Avdeev <andrewww.avdeev@gmail.com>", | ||
@@ -17,2 +17,3 @@ "keywords": [ | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
@@ -55,3 +56,3 @@ "/dist", | ||
"@typescript-eslint/parser": "^2.3.2", | ||
"eslint": "^6.5.1", | ||
"eslint": "^6.8.0", | ||
"pino": "^5.13.1", | ||
@@ -58,0 +59,0 @@ "typescript": "^3.6.3" |
@@ -9,3 +9,3 @@ #!/usr/bin/env node | ||
// main cli logic | ||
function main () { | ||
function main() { | ||
program | ||
@@ -19,3 +19,6 @@ // .version(pkg.version) | ||
.option('-mb, --maxBreadcrumbs <maxBreadcrumbs>', 'Total amount of breadcrumbs that should be captured') | ||
.action(async ({ dsn, serverName, environment, debug, sampleRate, maxBreadcrumbs }) => { | ||
.option('-di, --dist <dist>', 'Sets the distribution for all events') | ||
.option('--maxValueLength <maxValueLength>', 'Maximum number of chars a single value can have before it will be truncated.') | ||
.option('--release <release>', 'The release identifier used when uploading respective source maps.') | ||
.action(async ({ dsn, serverName, environment, debug, sampleRate, maxBreadcrumbs, dist, logLevel, maxValueLength, release }) => { | ||
try { | ||
@@ -30,2 +33,6 @@ console.info('start'); | ||
maxBreadcrumbs, | ||
dist, | ||
logLevel, | ||
maxValueLength, | ||
release, | ||
}); | ||
@@ -32,0 +39,0 @@ process.stdin.pipe(writeStream); |
@@ -1,2 +0,2 @@ | ||
import stream from 'stream'; | ||
import stream from 'stream'; | ||
import split from 'split2'; | ||
@@ -17,3 +17,13 @@ import pump from 'pump'; | ||
class PinoSentryTransport { | ||
function defaults<T>(target: Partial<T>, source: Partial<T>): Partial<T> { | ||
const ret: Partial<T> = { ...target }; | ||
for (const key of (Object.keys(source) as (keyof T)[])) { | ||
if (target[key] === undefined) { | ||
ret[key] = source[key]; | ||
} | ||
} | ||
return ret; | ||
} | ||
export class PinoSentryTransport { | ||
private SEVERITIES_MAP = { | ||
@@ -26,10 +36,18 @@ 10: Sentry.Severity.Debug, // pino: trace | ||
60: Sentry.Severity.Fatal, // pino: fatal | ||
// Support for useLevelLabels | ||
// https://github.com/pinojs/pino/blob/master/docs/api.md#uselevellabels-boolean | ||
trace: Sentry.Severity.Debug, | ||
debug: Sentry.Severity.Debug, | ||
info: Sentry.Severity.Info, | ||
warning: Sentry.Severity.Warning, | ||
error: Sentry.Severity.Error, | ||
fatal: Sentry.Severity.Fatal, | ||
}; | ||
public constructor (options?: Sentry.NodeOptions) { | ||
public constructor(options?: Sentry.NodeOptions) { | ||
Sentry.init(this.withDefaults(options || {})); | ||
} | ||
public getLogSeverity(level: number): Sentry.Severity { | ||
return (this.SEVERITIES_MAP as any)[level]; | ||
public getLogSeverity(level: number): Sentry.Severity { | ||
return (this.SEVERITIES_MAP as any)[level] || Sentry.Severity.Info; | ||
} | ||
@@ -43,3 +61,4 @@ | ||
const chunk = JSON.parse(line); | ||
const cb = () => {}; | ||
const cb = () => { | ||
}; | ||
@@ -55,3 +74,3 @@ this.prepareAndGo(chunk, cb); | ||
public prepareAndGo(chunk: any, cb: any) { | ||
public prepareAndGo(chunk: any, cb: any): void { | ||
const severity = this.getLogSeverity(chunk.level); | ||
@@ -100,14 +119,17 @@ const tags = chunk.tags || {}; | ||
private withDefaults(options: Sentry.NodeOptions) { | ||
return { | ||
dsn: options.dsn || process.env.SENTRY_DSN || '', | ||
serverName: options && options.serverName || 'pino-sentry', | ||
environment: options && options.environment || process.env.SENTRY_ENVIRONMENT || process.env.NODE_ENV || 'production', | ||
debug: options && options.debug || !!process.env.SENTRY_DEBUG || false, | ||
sampleRate: options && options.sampleRate || 1.0, | ||
maxBreadcrumbs: options && options.maxBreadcrumbs || 100, | ||
}; | ||
private withDefaults(options: Sentry.NodeOptions): Sentry.NodeOptions { | ||
if (!options) { | ||
options = {}; | ||
} | ||
return defaults(options, { | ||
dsn: process.env.SENTRY_DSN || '', | ||
serverName: 'pino-sentry', | ||
environment: process.env.SENTRY_ENVIRONMENT || process.env.NODE_ENV || 'production', | ||
debug: !!process.env.SENTRY_DEBUG || false, | ||
sampleRate: 1.0, | ||
maxBreadcrumbs: 100, | ||
}); | ||
} | ||
private isObject(obj: any) { | ||
private isObject(obj: any): boolean { | ||
const type = typeof obj; | ||
@@ -117,3 +139,3 @@ return type === 'function' || type === 'object' && !!obj; | ||
private shouldLogException(level: Sentry.Severity) { | ||
private shouldLogException(level: Sentry.Severity): boolean { | ||
return level === Sentry.Severity.Fatal || level === Sentry.Severity.Error; | ||
@@ -123,6 +145,6 @@ } | ||
export function createWriteStreamAsync(options: any = {}) { | ||
export function createWriteStreamAsync(options: Sentry.NodeOptions = {}): PromiseLike<stream.Transform> { | ||
if (!options.dsn && !process.env.SENTRY_DSN) { | ||
throw Error('Sentry DSN missing'); | ||
}; | ||
} | ||
@@ -136,3 +158,3 @@ const transport = new PinoSentryTransport(options); | ||
return JSON.parse(line); | ||
} catch(e) { | ||
} catch (e) { | ||
throw Error('logs should be in json format'); | ||
@@ -144,6 +166,6 @@ } | ||
export function createWriteStream(options: any = {}) { | ||
export function createWriteStream(options: Sentry.NodeOptions = {}): stream.Transform & { transport: PinoSentryTransport } { | ||
if (!options.dsn && !process.env.SENTRY_DSN) { | ||
throw Error('Sentry DSN missing'); | ||
}; | ||
} | ||
@@ -153,3 +175,3 @@ const transport = new PinoSentryTransport(options); | ||
return split(sentryParse); | ||
return Object.assign(split(sentryParse), { transport }); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
33974
18.36%437
22.75%