Skip to content

Commit

Permalink
Separate sentry and transport options.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin658 committed Sep 24, 2019
1 parent 48069cf commit 95c8c9b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import { SentryTransport } from 'winston-sentry-javascript-node';
const logger = winston.createLogger({
transports: [
new SentryTransport({
dsn: 'MY_SENTRY_DSN',
sentry{
dsn: 'MY_SENTRY_DSN',
},
}),
],
});
Expand Down Expand Up @@ -62,7 +64,9 @@ Catch and send `uncaughtException` to the Sentry.
const logger = winston.createLogger({
transports: [
new SentryTransport({
dsn: 'MY_SENTRY_DSN',
sentry{
dsn: 'MY_SENTRY_DSN',
},
handleExceptions: true,
}),
],
Expand All @@ -73,12 +77,37 @@ const logger = winston.createLogger({
const logger = winston.createLogger({
exceptionHandlers: [
new SentryTransport({
dsn: 'MY_SENTRY_DSN',
sentry: {
dsn: 'MY_SENTRY_DSN',
}
}),
]
});
```

## Options

```javascript
new SentryTransport(opts)
```

* opts: TransportStreamOptions

```javascript
interface TransportStreamOptions {
format?: logform.Format;
level?: string;
silent?: boolean;
handleExceptions?: boolean;

log?(info: any, next: () => void): any;
logv?(info: any, next: () => void): any;
close?(): void;
}
```

* opts.sentry: Please see [Sentry client options](https://docs.sentry.io/error-reporting/configuration/?platform=javascript).

## Default Extra for Error Object

By default, if you provide an Error Object to logger, this package will set the following extra:
Expand Down
4 changes: 3 additions & 1 deletion example/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { SentryTransport } from '../src/index';
const logger = winston.createLogger({
transports: [
new SentryTransport({
dsn: process.env.SENTRY_DSN,
sentry: {
dsn: process.env.SENTRY_DSN,
},
level: 'error',
handleExceptions: true,
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-sentry-javascript-node",
"version": "0.1.3",
"version": "0.2.0",
"description": "Sentry transport for the winson logger that using official Sentry SDK for javascript instead of the old Raven.",
"main": "lib/index.js",
"scripts": {
Expand Down
17 changes: 8 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { LogEntry } from 'winston';
import Transport, { TransportStreamOptions } from 'winston-transport';
import * as Sentry from '@sentry/node';

export interface SentryOption {
dsn: string;
}

export interface Extra {
extra?: {
[key: string]: any;
Expand All @@ -22,14 +18,17 @@ export interface UserInfo {
user?: Sentry.User
}

export interface SentryOptions {
sentry: Sentry.NodeOptions;
}

export type Log = LogEntry & Extra & Tag & UserInfo;

export class SentryTransport extends Transport {
public constructor(opts: TransportStreamOptions & SentryOption) {
super(opts);
Sentry.init({
dsn: opts.dsn,
});
public constructor(opts: TransportStreamOptions & SentryOptions) {
const { sentry, ...rest } = opts;
super(rest);
Sentry.init(sentry);
}

public log(info: Log, next: () => void): void {
Expand Down

0 comments on commit 95c8c9b

Please sign in to comment.