@leaflink/snitch 1.0.1 → 1.1.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 CHANGED
@@ -25,12 +25,25 @@ The level a log was called at is passed to transports to help determine how a me
25
25
 
26
26
  ## Usage
27
27
 
28
+ ### Singleton Logger
29
+
28
30
  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
31
 
30
32
  ```typescript
31
33
  import logger from '@leaflink/snitch';
34
+ import { ConsoleTransport } from '@leaflink/snitch/transports/console';
35
+
36
+ if (config.debug) {
37
+ logger.addTransport(new ConsoleTransport());
38
+ }
32
39
  ```
33
40
 
41
+ ### New Logger
42
+
43
+ | Name | Default | Description |
44
+ | --- | --- | --- |
45
+ | `transports` | `[]` | Define transports when creating new logger instead of adding with `.addTransport` |
46
+
34
47
  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
48
 
36
49
  ```typescript
@@ -45,9 +58,14 @@ const loggerInstance = new Logger({
45
58
  loggerInstance.log('Example log message');
46
59
  ```
47
60
 
48
-
49
61
  ### Console Transport
50
62
 
63
+ Parameters:
64
+
65
+ | Name | Default | Description |
66
+ | --- | --- | --- |
67
+ | `level` | `debug` | Minimum log level to handle. |
68
+
51
69
  `main.ts`
52
70
  ```typescript
53
71
  import logger from '@leaflink/snitch';
@@ -69,6 +87,13 @@ try {
69
87
 
70
88
  ### Sentry Transport
71
89
 
90
+ Parameters:
91
+
92
+ | Name | Default | Description |
93
+ | --- | --- | --- |
94
+ | `level` | `debug` | Minimum log level to handle. |
95
+ | `sentryInstance` | N/A, required | Initialized Sentry logger instance. |
96
+
72
97
  `main.ts`
73
98
  ```typescript
74
99
  import * as Sentry from '@sentry/vue';
@@ -98,6 +123,46 @@ try {
98
123
  }
99
124
  ```
100
125
 
126
+ ### Datadog Logs Transport
127
+
128
+ Parameters:
129
+
130
+ | Name | Default | Description |
131
+ | --- | --- | --- |
132
+ | `level` | `info` | Minimum log level to handle. |
133
+ | `datadogLogsInstance` | N/A, required | Initialized Datadog Logs instance. |
134
+
135
+ See [Datadog Logs documentation](https://docs.datadoghq.com/logs/log_collection/javascript/#custom-logs) for more on custom logging with Datadog.
136
+
137
+ `main.ts`
138
+ ```typescript
139
+ import { datadogLogs } from '@datadog/browser-logs';
140
+ import logger from '@leaflink/snitch';
141
+ import { DatadogTransport } from '@leaflink/snitch/transports/datadog';
142
+
143
+ // Important: init Datadog logs instance before creating transport
144
+ datadogLogs.init({
145
+ // ...
146
+ });
147
+ logger.addTransport(new DatadogTransport({
148
+ datadogLogsInstance: datadogLogs,
149
+ }));
150
+ ```
151
+
152
+ call in component file
153
+ ```typescript
154
+ import logger from '@leaflink/snitch';
155
+
156
+ logger.info('File loaded!');
157
+
158
+ try {
159
+ await someErroringMethod()
160
+ } catch (err) {
161
+ // If an Error is passed as the message, this is captured as an exception in Datadog
162
+ logger.error(err, { id: 123 });
163
+ }
164
+ ```
165
+
101
166
  ### With Log Level
102
167
 
103
168
  `main.ts`
@@ -0,0 +1,11 @@
1
+ import { LogLevel, Transport } from '../index.js';
2
+ import type { datadogLogs } from '@datadog/browser-logs';
3
+ export interface DatadogTransportOptions {
4
+ level?: LogLevel;
5
+ datadogLogsInstance: typeof datadogLogs;
6
+ }
7
+ export declare class DatadogTransport implements Transport {
8
+ level: LogLevel;
9
+ log: (message: string | object, meta: Record<string, unknown> | undefined, level: LogLevel) => void;
10
+ constructor(opts: DatadogTransportOptions);
11
+ }
@@ -0,0 +1,23 @@
1
+ export class DatadogTransport {
2
+ level;
3
+ log;
4
+ constructor(opts) {
5
+ this.level = opts.level || 'info';
6
+ this.log = (message, meta = {}, level) => {
7
+ // LogLevel maps directly to datadogLogsInstance logger methods
8
+ if (message instanceof Error) {
9
+ // Datadog logger has an optional third error param for exception handling
10
+ opts.datadogLogsInstance.logger[level](message.message, meta, message);
11
+ }
12
+ else if (typeof message === 'string') {
13
+ opts.datadogLogsInstance.logger[level](message, meta);
14
+ }
15
+ else {
16
+ // Datadog requires a string for the message param. Our downstream usage currently provides
17
+ // either a string or an Error, but for future-proofing this just stringify the unknown object as message.
18
+ opts.datadogLogsInstance.logger[level](JSON.stringify(message), meta);
19
+ }
20
+ };
21
+ }
22
+ }
23
+ //# sourceMappingURL=datadog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datadog.js","sourceRoot":"","sources":["../../src/transports/datadog.ts"],"names":[],"mappings":"AAQA,MAAM,OAAO,gBAAgB;IAC3B,KAAK,CAAW;IAChB,GAAG,CAAiG;IAEpG,YAAY,IAA6B;QACvC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI,MAAM,CAAC;QAClC,IAAI,CAAC,GAAG,GAAG,CAAC,OAAO,EAAE,IAAI,GAAG,EAAE,EAAE,KAAK,EAAE,EAAE;YACvC,+DAA+D;YAC/D,IAAI,OAAO,YAAY,KAAK,EAAE;gBAC5B,0EAA0E;gBAC1E,IAAI,CAAC,mBAAmB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;aACxE;iBAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;gBACtC,IAAI,CAAC,mBAAmB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;aACvD;iBAAM;gBACL,2FAA2F;gBAC3F,0GAA0G;gBAC1G,IAAI,CAAC,mBAAmB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC;aACvE;QACH,CAAC,CAAA;IACH,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leaflink/snitch",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Front end logging inspired by winston.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -49,6 +49,7 @@
49
49
  "devDependencies": {
50
50
  "@commitlint/cli": "^17.4.4",
51
51
  "@commitlint/config-conventional": "^17.4.4",
52
+ "@datadog/browser-logs": "^4.39.0",
52
53
  "@vitest/coverage-c8": "^0.29.2",
53
54
  "typescript": "^4.9.5",
54
55
  "vite": "^4.1.4",