@vouchfor/libs 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/.editorconfig ADDED
@@ -0,0 +1,13 @@
1
+ # editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ charset = utf-8
9
+ trim_trailing_whitespace = true
10
+ insert_final_newline = true
11
+
12
+ [*.md]
13
+ trim_trailing_whitespace = false
package/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # Share libs
2
+
3
+ included modules
4
+ - `dotenv`
5
+ - `@logtail/node`
6
+ - `@logtail/winston`
7
+ - `@sentry/node`
8
+ - `@sentry/tracing`
9
+ - `winston`
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ // will need to publish this module to npm registry with private account
15
+ npm i @vouchfor/libs
16
+
17
+ // or directly from private repo
18
+
19
+ npm i git+ssh://git@github.com:VouchAU/vouch-libs.git
20
+ ```
21
+
22
+ ## ENV variables
23
+
24
+ ```
25
+ ENV=dev|staging|prod
26
+ LOGTAIL_SOURCE_TOKE=xxxxxx
27
+ SENTRY_DSN=xxxxxxx
28
+ ```
29
+
30
+ ## Logger
31
+
32
+ `Logger` class is constructed using winston and [logtail](https://betterstack.com/logtail) as the transport layer.
33
+ To channel the logs to `Logtail`, it requires `LOGTAIL_SOURCE_TOKEN` in the `.env` file, or register the token to the config module:
34
+
35
+ ```js
36
+ import { Logger } from '@vouchfor/libs';
37
+
38
+ const logger = new Logger();
39
+
40
+ logger.info('Hi');
41
+ // --> [INFO] Hi
42
+ ```
43
+
44
+ get winstan logger instance directly
45
+ ```js
46
+ import { getLogger } from '@vouchfor/libs';
47
+
48
+ const logger = getLogger();
49
+
50
+ logger.info('Hi');
51
+ // --> [INFO] Hi
52
+ ```
53
+
54
+ `getLogger` support winston `transformer` and `format` method passing through the config options
55
+ ```js
56
+ const logger = getLogger({
57
+ transformer: (info) => ({...info, foo: process.env.BAR}),
58
+ format: (info) => `[LOGGER] ${JSON.stringiy(info)}`
59
+ });
60
+
61
+ logger.info('Hi');
62
+ // --> [LOGGER] {"message": "Hi", "foo": "bar", ... }
63
+
64
+ ```
65
+
66
+ Injecting metadata per process/request.
67
+ ```js
68
+ import { Logger } from '@vouchfor/libs';
69
+
70
+ const logger = new Logger({
71
+ metadata: {
72
+ requestId: "1234-123-123-1234",
73
+ }
74
+ });
75
+
76
+ logger.info('Hi');
77
+ // --> [INFO] Hi {"metadata":{"requestId":"1234-123-123-1234"}}
78
+ ```
79
+
80
+ Logtail `source_token` can be injected through the class initialisation.
81
+ ```js
82
+ import { Logger } from '@vouchfor/libs';
83
+
84
+ const logger = new Logger({
85
+ token: 'xxxxxxx',
86
+ metadata: {
87
+ requestId: "1234-123-123-1234",
88
+ }
89
+ });
90
+
91
+ logger.info('Hi');
92
+ // --> [INFO] Hi {"metadata":{"requestId":"1234-123-123-1234"}}
93
+ ```
94
+
95
+ ## Sentry
96
+
97
+ Sentry DSN is needed in `.env` file as in [.env](#env-variables).
98
+ ```js
99
+ import { Sentry } from '@vouchfor/libs';
100
+
101
+ try {
102
+ throw new Error('123');
103
+ } catch (err) {
104
+ Sentry.captureException(err);
105
+ }
106
+
107
+ ```
108
+
109
+ To use Sentry in lambda, it requires `@sentry/serverless`
110
+
111
+ ```js
112
+ import { Sentry } from '@vouchfor/libs';
113
+ import { AWSLambda } from '@sentry/serverless';
114
+
115
+ exports.handler = AWSLambda.wrapHandler(async (event, context) => {
116
+ throw new Error('oh, hello there!');
117
+ });
118
+
119
+ // or just use the `@sentry/serverless` module
120
+
121
+ import * as Sentry from '@sentry/serverless';
122
+
123
+ Sentry.init({
124
+ ...
125
+ })
126
+
127
+ exports.handler = Sentry.AWSLambda.wrapHandler(async (event, context) => {
128
+ throw new Error('oh, hello there!');
129
+ });
130
+
131
+ ```
132
+
133
+
134
+
@@ -0,0 +1,8 @@
1
+ import { LogEntry } from "winston";
2
+ import Transport from "winston-transport";
3
+ import { Logtail } from "@logtail/node";
4
+ export declare class LogtailTransport extends Transport {
5
+ private _logtail;
6
+ constructor(_logtail: Logtail, opts?: Transport.TransportStreamOptions);
7
+ log(info: LogEntry, cb: Function): void;
8
+ }
@@ -0,0 +1,3 @@
1
+ export { Logger } from './logger';
2
+ export type { LoggerConfig } from './logger';
3
+ export { initSentry, Sentry } from './sentry';