@leaflink/snitch 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 +149 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/index.spec.d.ts +1 -0
- package/dist/index.spec.js +70 -0
- package/dist/index.spec.js.map +1 -0
- package/dist/transports/console.d.ts +9 -0
- package/dist/transports/console.js +22 -0
- package/dist/transports/console.js.map +1 -0
- package/dist/transports/sentry.d.ts +10 -0
- package/dist/transports/sentry.js +26 -0
- package/dist/transports/sentry.js.map +1 -0
- package/package.json +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# @leaflink/snitch
|
|
2
|
+
|
|
3
|
+
Simple and extensible browser logging, inspired by [Winston](https://github.com/winstonjs/winston).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`$ npm i @leaflink/snitch`
|
|
8
|
+
|
|
9
|
+
## Transports
|
|
10
|
+
|
|
11
|
+
Transports are a way of routing log messages to multiple destinations, with the ability to pre-process or format the message. `@leaflink/snitch` includes two transports by default (`console` and Sentry), but they can be extended using the `Transport` type exported by the logger.
|
|
12
|
+
|
|
13
|
+
## Log Levels
|
|
14
|
+
|
|
15
|
+
Transports can be created with a log level to only handle messages with a certain severity. Log levels supported by `@leaflink/snitch`, in decreasing order by severity, are:
|
|
16
|
+
|
|
17
|
+
- `error`
|
|
18
|
+
- `warn`
|
|
19
|
+
- `info`
|
|
20
|
+
- `debug`
|
|
21
|
+
|
|
22
|
+
These levels cascade *up*, so a transport created with level `debug` will handle logs with that level as well as `info`, `warn`, and `error`, but a transport created with level `error` will not handle logs with the levels `warn`, `info`, or `debug`.
|
|
23
|
+
|
|
24
|
+
The level a log was called at is passed to transports to help determine how a message should be handled (for example, logging with `console.<level>` in the console transport).
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
The default export exposed by `@leaflink/snitch` is a singleton logger instance with no initial transports, which can be shared between modules easily. It does not include any transports by default to allow flexibility in per-environment transports.
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import logger from '@leaflink/snitch';
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
In other situations, you may want to have a logger instance (or multiple instances) created and managed inside your application. To support this, `@leaflink/snitch` also exports a `Logger` class that can be used to create logger instances, optionally with predefined transports.
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { Logger } from '@leaflink/snitch';
|
|
38
|
+
import { ConsoleTransport } from '@leaflink/snitch/transports/console';
|
|
39
|
+
|
|
40
|
+
const loggerInstance = new Logger({
|
|
41
|
+
transports: [new ConsoleTransport()],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// `logger.log` is an alias for `logger.info` for convenience
|
|
45
|
+
loggerInstance.log('Example log message');
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### Console Transport
|
|
50
|
+
|
|
51
|
+
`main.ts`
|
|
52
|
+
```typescript
|
|
53
|
+
import logger from '@leaflink/snitch';
|
|
54
|
+
import { ConsoleTransport } from '@leaflink/snitch/transports/console';
|
|
55
|
+
|
|
56
|
+
logger.addTransport(new ConsoleTransport());
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
call in component file
|
|
60
|
+
```typescript
|
|
61
|
+
import logger from '@leaflink/snitch';
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
await someErroringMethod()
|
|
65
|
+
} catch (err) {
|
|
66
|
+
logger.error(err)
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Sentry Transport
|
|
71
|
+
|
|
72
|
+
`main.ts`
|
|
73
|
+
```typescript
|
|
74
|
+
import * as Sentry from '@sentry/vue';
|
|
75
|
+
import logger from '@leaflink/snitch';
|
|
76
|
+
import { SentryTransport } from '@leaflink/snitch/transports/sentry';
|
|
77
|
+
|
|
78
|
+
// Important: init Sentry instance before creating transport
|
|
79
|
+
Sentry.init({
|
|
80
|
+
// ...
|
|
81
|
+
});
|
|
82
|
+
logger.addTransport(new SentryTransport({
|
|
83
|
+
sentryInstance: Sentry,
|
|
84
|
+
}));
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
call in component file
|
|
88
|
+
```typescript
|
|
89
|
+
import logger from '@leaflink/snitch';
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
await someErroringMethod()
|
|
93
|
+
} catch (err) {
|
|
94
|
+
// optional error context object
|
|
95
|
+
const errorContext: Record<string, any> = getErrorContext();
|
|
96
|
+
// `err` is Error | string
|
|
97
|
+
logger.error(err, errorContext);
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### With Log Level
|
|
102
|
+
|
|
103
|
+
`main.ts`
|
|
104
|
+
```typescript
|
|
105
|
+
import logger from '@leaflink/snitch';
|
|
106
|
+
import { ConsoleTransport } from '@leaflink/snitch/transports/console';
|
|
107
|
+
|
|
108
|
+
logger.addTransport(new ConsoleTransport({
|
|
109
|
+
level: 'info'
|
|
110
|
+
}));
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Custom Transports
|
|
114
|
+
|
|
115
|
+
`custom-transport.ts`
|
|
116
|
+
```typescript
|
|
117
|
+
import { LogLevel, Transport } from '@leaflink/snitch';
|
|
118
|
+
// imagine this has a `report(options<{message, context}>)` method
|
|
119
|
+
import CustomDestinationInstance from '@example/destination';
|
|
120
|
+
|
|
121
|
+
interface CustomTransportOptions {
|
|
122
|
+
level?: LogLevel;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export class CustomTransport implements Transport {
|
|
126
|
+
level: LogLevel;
|
|
127
|
+
log: (message: string | object, meta: Record<string, unknown> | undefined, level: LogLevel) => void;
|
|
128
|
+
|
|
129
|
+
constructor(opts?: ConsoleTransportOptions) {
|
|
130
|
+
this.level = opts?.level || 'debug';
|
|
131
|
+
this.log = (message, meta) => {
|
|
132
|
+
CustomDestinationInstance.report({
|
|
133
|
+
message,
|
|
134
|
+
context: 'meta',
|
|
135
|
+
})
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`main.ts`
|
|
142
|
+
```typescript
|
|
143
|
+
import logger from '@leaflink/snitch';
|
|
144
|
+
import { CustomTransport } from './custom-transport';
|
|
145
|
+
|
|
146
|
+
logger.add(new CustomTransport());
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Note: You can add a custom transport in your project, but consider opening a PR in this repo instead!
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type LogLevel = 'error' | 'warn' | 'info' | 'debug';
|
|
2
|
+
type LogMessage = object | string;
|
|
3
|
+
type LogFunction = (message: LogMessage, meta: Record<string, unknown> | undefined, level: LogLevel) => void;
|
|
4
|
+
export interface Transport {
|
|
5
|
+
level: LogLevel;
|
|
6
|
+
log: LogFunction;
|
|
7
|
+
}
|
|
8
|
+
export declare class Logger {
|
|
9
|
+
transports: Transport[];
|
|
10
|
+
constructor(opts?: any);
|
|
11
|
+
private _log;
|
|
12
|
+
debug(message: LogMessage, meta?: Record<string, unknown>): void;
|
|
13
|
+
info(message: LogMessage, meta?: Record<string, unknown>): void;
|
|
14
|
+
log(message: LogMessage, meta?: Record<string, unknown>): void;
|
|
15
|
+
warn(message: LogMessage, meta?: Record<string, unknown>): void;
|
|
16
|
+
error(message: LogMessage, meta?: Record<string, unknown>): void;
|
|
17
|
+
addTransport(transport: Transport): void;
|
|
18
|
+
clearTransports(): void;
|
|
19
|
+
}
|
|
20
|
+
declare const logger: Logger;
|
|
21
|
+
export default logger;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const levels = {
|
|
2
|
+
error: 0,
|
|
3
|
+
warn: 1,
|
|
4
|
+
info: 2,
|
|
5
|
+
debug: 3,
|
|
6
|
+
};
|
|
7
|
+
export class Logger {
|
|
8
|
+
transports;
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
this.transports = opts?.transports || [];
|
|
11
|
+
}
|
|
12
|
+
_log(level, message, meta) {
|
|
13
|
+
for (const transport of this.transports) {
|
|
14
|
+
if (levels[transport.level] >= levels[level]) {
|
|
15
|
+
transport.log(message, meta, level);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
debug(message, meta) {
|
|
20
|
+
this._log('debug', message, meta);
|
|
21
|
+
}
|
|
22
|
+
info(message, meta) {
|
|
23
|
+
this._log('info', message, meta);
|
|
24
|
+
}
|
|
25
|
+
log(message, meta) {
|
|
26
|
+
this.info(message, meta);
|
|
27
|
+
}
|
|
28
|
+
warn(message, meta) {
|
|
29
|
+
this._log('warn', message, meta);
|
|
30
|
+
}
|
|
31
|
+
error(message, meta) {
|
|
32
|
+
this._log('error', message, meta);
|
|
33
|
+
}
|
|
34
|
+
addTransport(transport) {
|
|
35
|
+
this.transports.push(transport);
|
|
36
|
+
}
|
|
37
|
+
clearTransports() {
|
|
38
|
+
this.transports = [];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const logger = new Logger();
|
|
42
|
+
export default logger;
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,GAAG;IACb,KAAK,EAAE,CAAC;IACR,IAAI,EAAE,CAAC;IACP,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;CACT,CAAC;AAUF,MAAM,OAAO,MAAM;IACjB,UAAU,CAAc;IAExB,YAAY,IAAK;QACf,IAAI,CAAC,UAAU,GAAG,IAAI,EAAE,UAAU,IAAI,EAAE,CAAC;IAC3C,CAAC;IAEO,IAAI,CAAC,KAAe,EAAE,OAAmB,EAAE,IAA8B;QAC/E,KAAK,MAAM,SAAS,IAAI,IAAI,CAAC,UAAU,EAAE;YACvC,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE;gBAC5C,SAAS,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;aACrC;SACF;IACH,CAAC;IAGD,KAAK,CAAC,OAAmB,EAAE,IAA8B;QACvD,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,IAAI,CAAC,OAAmB,EAAE,IAA8B;QACtD,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;IAED,GAAG,CAAC,OAAmB,EAAE,IAA8B;QACrD,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC3B,CAAC;IAED,IAAI,CAAC,OAAmB,EAAE,IAA8B;QACtD,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;IAED,KAAK,CAAC,OAAmB,EAAE,IAA8B;QACvD,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,YAAY,CAAC,SAAoB;QAC/B,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAClC,CAAC;IAED,eAAe;QACb,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;IACvB,CAAC;CACF;AAED,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;AAC5B,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import logger, { Logger } from './index.js';
|
|
2
|
+
afterEach(() => logger.clearTransports());
|
|
3
|
+
it.each(['debug', 'info', 'warn', 'error'])('can register a logger to a log level', (level) => {
|
|
4
|
+
expect(logger.transports.length).toBe(0);
|
|
5
|
+
logger.addTransport({
|
|
6
|
+
level: level,
|
|
7
|
+
log: () => { },
|
|
8
|
+
});
|
|
9
|
+
expect(logger.transports.length).toBe(1);
|
|
10
|
+
expect(logger.transports[0].level).toBe(level);
|
|
11
|
+
});
|
|
12
|
+
it('calls a transport when the log level is at or below the transport\'s level', () => {
|
|
13
|
+
const errorSpy = vi.fn();
|
|
14
|
+
const warnSpy = vi.fn();
|
|
15
|
+
const infoSpy = vi.fn();
|
|
16
|
+
const debugSpy = vi.fn();
|
|
17
|
+
logger.addTransport({
|
|
18
|
+
level: 'error',
|
|
19
|
+
log: errorSpy,
|
|
20
|
+
});
|
|
21
|
+
logger.addTransport({
|
|
22
|
+
level: 'warn',
|
|
23
|
+
log: warnSpy,
|
|
24
|
+
});
|
|
25
|
+
logger.addTransport({
|
|
26
|
+
level: 'info',
|
|
27
|
+
log: infoSpy,
|
|
28
|
+
});
|
|
29
|
+
logger.addTransport({
|
|
30
|
+
level: 'debug',
|
|
31
|
+
log: debugSpy,
|
|
32
|
+
});
|
|
33
|
+
// eslint-disable-next-line testing-library/no-debugging-utils
|
|
34
|
+
logger.debug('debug message', {});
|
|
35
|
+
expect(errorSpy).not.toHaveBeenCalled();
|
|
36
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
37
|
+
expect(infoSpy).not.toHaveBeenCalled();
|
|
38
|
+
expect(debugSpy).toHaveBeenCalledWith('debug message', {}, 'debug');
|
|
39
|
+
logger.info('info message', {});
|
|
40
|
+
expect(errorSpy).not.toHaveBeenCalled();
|
|
41
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
42
|
+
expect(infoSpy).toHaveBeenCalledWith('info message', {}, 'info');
|
|
43
|
+
expect(debugSpy).toHaveBeenCalledWith('info message', {}, 'info');
|
|
44
|
+
logger.warn('warn message', {});
|
|
45
|
+
expect(errorSpy).not.toHaveBeenCalled();
|
|
46
|
+
expect(warnSpy).toHaveBeenCalledWith('warn message', {}, 'warn');
|
|
47
|
+
expect(infoSpy).toHaveBeenCalledWith('warn message', {}, 'warn');
|
|
48
|
+
expect(debugSpy).toHaveBeenCalledWith('warn message', {}, 'warn');
|
|
49
|
+
logger.error('error message', {});
|
|
50
|
+
expect(errorSpy).toHaveBeenCalledWith('error message', {}, 'error');
|
|
51
|
+
expect(warnSpy).toHaveBeenCalledWith('error message', {}, 'error');
|
|
52
|
+
expect(infoSpy).toHaveBeenCalledWith('error message', {}, 'error');
|
|
53
|
+
expect(debugSpy).toHaveBeenCalledWith('error message', {}, 'error');
|
|
54
|
+
});
|
|
55
|
+
it('can create a logger with transports set in the constructor', () => {
|
|
56
|
+
const customLogger = new Logger({
|
|
57
|
+
transports: [
|
|
58
|
+
{
|
|
59
|
+
level: 'info',
|
|
60
|
+
log: vi.fn()
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
level: 'warn',
|
|
64
|
+
log: vi.fn()
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
expect(customLogger.transports.length).toBe(2);
|
|
69
|
+
});
|
|
70
|
+
//# sourceMappingURL=index.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.spec.js","sourceRoot":"","sources":["../src/index.spec.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,EAAE,EAAE,MAAM,EAAY,MAAM,YAAY,CAAC;AAEtD,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC,CAAC;AAE1C,EAAE,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,sCAAsC,EAAE,CAAC,KAAK,EAAE,EAAE;IAC5F,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEzC,MAAM,CAAC,YAAY,CAAC;QAClB,KAAK,EAAE,KA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,CAAC,CAAC,oBAAoB,CAAC,eAAe,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;AACtE,CAAC,CAAC,CAAC;AAEH,EAAE,CAAC,4DAA4D,EAAE,GAAG,EAAE;IACpE,MAAM,YAAY,GAAG,IAAI,MAAM,CAAC;QAC9B,UAAU,EAAE;YACV;gBACE,KAAK,EAAE,MAAM;gBACb,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE;aACb;YACD;gBACE,KAAK,EAAE,MAAM;gBACb,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE;aACb;SACF;KACF,CAAC,CAAC;IAEH,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACjD,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { LogLevel, Transport } from '../index.js';
|
|
2
|
+
export interface ConsoleTransportOptions {
|
|
3
|
+
level?: LogLevel;
|
|
4
|
+
}
|
|
5
|
+
export declare class ConsoleTransport implements Transport {
|
|
6
|
+
level: LogLevel;
|
|
7
|
+
log: (message: string | object, meta: Record<string, unknown> | undefined, level: LogLevel) => void;
|
|
8
|
+
constructor(opts?: ConsoleTransportOptions);
|
|
9
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export class ConsoleTransport {
|
|
2
|
+
level;
|
|
3
|
+
log;
|
|
4
|
+
constructor(opts) {
|
|
5
|
+
this.level = opts?.level || 'debug';
|
|
6
|
+
this.log = (message, meta, level) => {
|
|
7
|
+
switch (level) {
|
|
8
|
+
case 'error':
|
|
9
|
+
console.error(message, meta);
|
|
10
|
+
break;
|
|
11
|
+
case 'warn':
|
|
12
|
+
console.warn(message, meta);
|
|
13
|
+
break;
|
|
14
|
+
case 'debug':
|
|
15
|
+
console.debug(message, meta);
|
|
16
|
+
default:
|
|
17
|
+
console.log(message, meta);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=console.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console.js","sourceRoot":"","sources":["../../src/transports/console.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,gBAAgB;IAC3B,KAAK,CAAW;IAChB,GAAG,CAAiG;IAEpG,YAAY,IAA8B;QACxC,IAAI,CAAC,KAAK,GAAG,IAAI,EAAE,KAAK,IAAI,OAAO,CAAC;QACpC,IAAI,CAAC,GAAG,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;YAClC,QAAQ,KAAK,EAAE;gBACb,KAAK,OAAO;oBACV,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;oBAC7B,MAAM;gBACR,KAAK,MAAM;oBACT,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;oBAC5B,MAAM;gBACR,KAAK,OAAO;oBACV,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;gBAC/B;oBACE,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;aAC9B;QACH,CAAC,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LogLevel, Transport } from '../index.js';
|
|
2
|
+
export interface SentryTransportOptions {
|
|
3
|
+
level?: LogLevel;
|
|
4
|
+
sentryInstance: any;
|
|
5
|
+
}
|
|
6
|
+
export declare class SentryTransport implements Transport {
|
|
7
|
+
level: LogLevel;
|
|
8
|
+
log: (message: string | object, meta: Record<string, unknown> | undefined, level: LogLevel) => void;
|
|
9
|
+
constructor(opts: SentryTransportOptions);
|
|
10
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const SentryErrorLevelMap = {
|
|
2
|
+
debug: 'debug',
|
|
3
|
+
info: 'info',
|
|
4
|
+
warn: 'warning',
|
|
5
|
+
error: 'error',
|
|
6
|
+
};
|
|
7
|
+
export class SentryTransport {
|
|
8
|
+
level;
|
|
9
|
+
log;
|
|
10
|
+
constructor(opts) {
|
|
11
|
+
this.level = opts.level || 'error';
|
|
12
|
+
this.log = (message, meta, level) => {
|
|
13
|
+
const sentryMeta = {
|
|
14
|
+
level: SentryErrorLevelMap[level],
|
|
15
|
+
...meta,
|
|
16
|
+
};
|
|
17
|
+
if (typeof message === 'string') {
|
|
18
|
+
opts.sentryInstance.captureMessage(message, sentryMeta);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
opts.sentryInstance.captureException(message, sentryMeta);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=sentry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sentry.js","sourceRoot":"","sources":["../../src/transports/sentry.ts"],"names":[],"mappings":"AAEA,MAAM,mBAAmB,GAAG;IAC1B,KAAK,EAAE,OAAO;IACd,IAAI,EAAE,MAAM;IACZ,IAAI,EAAE,SAAS;IACf,KAAK,EAAE,OAAO;CACf,CAAC;AAOF,MAAM,OAAO,eAAe;IAC1B,KAAK,CAAW;IAChB,GAAG,CAAiG;IAEpG,YAAY,IAA4B;QACtC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI,OAAO,CAAC;QACnC,IAAI,CAAC,GAAG,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;YAClC,MAAM,UAAU,GAAG;gBACjB,KAAK,EAAE,mBAAmB,CAAC,KAAK,CAAC;gBACjC,GAAG,IAAI;aACR,CAAC;YACF,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;gBAC/B,IAAI,CAAC,cAAc,CAAC,cAAc,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;aACzD;iBAAM;gBACL,IAAI,CAAC,cAAc,CAAC,gBAAgB,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;aAC3D;QACH,CAAC,CAAC;IACJ,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leaflink/snitch",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Front end logging inspired by winston.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=16",
|
|
15
|
+
"npm": ">=8"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"lint:commits": "commitlint",
|
|
26
|
+
"test": "vitest --reporter verbose",
|
|
27
|
+
"test:ci": "vitest run --silent --coverage"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/LeafLink/snitch.git"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/LeafLink/snitch/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/LeafLink/snitch#readme",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@commitlint/cli": "^17.4.4",
|
|
40
|
+
"@commitlint/config-conventional": "^17.4.4",
|
|
41
|
+
"@vitest/coverage-c8": "^0.29.2",
|
|
42
|
+
"typescript": "^4.9.5",
|
|
43
|
+
"vite": "^4.1.4",
|
|
44
|
+
"vitest": "^0.29.2"
|
|
45
|
+
},
|
|
46
|
+
"commitlint": {
|
|
47
|
+
"extends": [
|
|
48
|
+
"@commitlint/config-conventional"
|
|
49
|
+
],
|
|
50
|
+
"rules": {
|
|
51
|
+
"scope-case": [
|
|
52
|
+
0
|
|
53
|
+
],
|
|
54
|
+
"body-max-line-length": [
|
|
55
|
+
0
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|