@tramvai/module-log 2.49.5 → 2.51.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.
Files changed (2) hide show
  1. package/README.md +78 -6
  2. package/package.json +7 -7
package/README.md CHANGED
@@ -14,12 +14,6 @@ see [@tinkoff/logger](../libs/logger#display-logs).
14
14
 
15
15
  > By default, on server all of the logs of level warn and above are enabled. On the client in dev-mode all the logs of level error and above are enabled while in prod-mode all of the logs on client are disabled.
16
16
 
17
- ### Send logs to the API
18
-
19
- It is implied that logs from the server are collected by the external tool that has access to the server console output and because of this logging to the external API from the server is not needed.
20
-
21
- In browser logs to the API are send with [RemoteReporter](../libs/logger.md#remotereporter). By default, all of the logs with levels `error` and `fatal` are send. The url for the API is specified by environment variable `FRONT_LOG_API`. For the customization see docs for the [RemoteReporter](../libs/logger.md#remotereporter).
22
-
23
17
  ### See logs from the server in browser
24
18
 
25
19
  This functionality is available only in dev-mode and can make development a little easier.
@@ -112,6 +106,84 @@ import { LOGGER_TOKEN } from '@tramvai/module-common';
112
106
  export class MyModule {}
113
107
  ```
114
108
 
109
+ ### Send logs to the API
110
+
111
+ :::info
112
+
113
+ It is implied that logs from the server are collected by the external tool that has access to the server console output and because of this logging to the external API from the server is not needed.
114
+
115
+ :::
116
+
117
+ For browser logs, you can send them to the API with [RemoteReporter](../libs/logger.md#remotereporter).
118
+
119
+ For example, if we want to send logs with levels `error` and `fatal` to url declared in environment variable `FRONT_LOG_API`:
120
+
121
+ ```ts
122
+ import { createToken, provide, Scope, APP_INFO_TOKEN } from '@tramvai/core';
123
+ import { ENV_USED_TOKEN, ENV_MANAGER_TOKEN, LOGGER_INIT_HOOK } from '@tramvai/tokens-common';
124
+ import { RemoteReporter } from '@tinkoff/logger';
125
+ import { isUrl } from '@tinkoff/env-validators';
126
+
127
+ const REMOTE_REPORTER = createToken<RemoteReporter>('remoteReporter');
128
+
129
+ const providers = [
130
+ // provide new env variable with logs collector endpoint
131
+ provide({
132
+ provide: ENV_USED_TOKEN,
133
+ useValue: [
134
+ // use isUrl for validation
135
+ { key: 'FRONT_LOG_API', dehydrate: true, validator: isUrl },
136
+ ],
137
+ }),
138
+ // provide new remote reporter
139
+ provide({
140
+ provide: REMOTE_REPORTER,
141
+ // we need only one instance of reporter
142
+ scope: Scope.SINGLETON,
143
+ useFactory: ({ appInfo, envManager, wuid }) => {
144
+ const { appName } = appInfo;
145
+ const logApi = envManager.get('FRONT_LOG_API');
146
+
147
+ return new RemoteReporter({
148
+ // number of parallel request
149
+ requestCount: 1,
150
+ // log levels which will be send to api
151
+ emitLevels: { error: true, fatal: true },
152
+ makeRequest(logObj) {
153
+ return sendLog({
154
+ logApi,
155
+ // additional information for every reported logs
156
+ payload: {
157
+ ...logObj,
158
+ appName,
159
+ userAgent: window.navigator.userAgent,
160
+ href: window.location.href,
161
+ },
162
+ });
163
+ },
164
+ });
165
+ },
166
+ deps: {
167
+ appInfo: APP_INFO_TOKEN,
168
+ envManager: ENV_MANAGER_TOKEN,
169
+ },
170
+ }),
171
+ // add reporter to logger
172
+ provide({
173
+ provide: LOGGER_INIT_HOOK,
174
+ multi: true,
175
+ useFactory({ remoteReporter }) {
176
+ return (loggerInstance) => {
177
+ loggerInstance.addBeforeReporter(remoteReporter);
178
+ };
179
+ },
180
+ deps: {
181
+ remoteReporter: REMOTE_REPORTER,
182
+ },
183
+ }),
184
+ ];
185
+ ```
186
+
115
187
  ### How to properly format logs
116
188
 
117
189
  See [@tinkoff/logger](../libs/logger.md#how-to-log-properly)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-log",
3
- "version": "2.49.5",
3
+ "version": "2.51.0",
4
4
  "description": "",
5
5
  "browser": "lib/browser.js",
6
6
  "main": "lib/server.js",
@@ -23,12 +23,12 @@
23
23
  },
24
24
  "peerDependencies": {
25
25
  "@tinkoff/utils": "^2.1.2",
26
- "@tramvai/core": "2.49.5",
27
- "@tramvai/module-environment": "2.49.5",
28
- "@tramvai/papi": "2.49.5",
29
- "@tramvai/state": "2.49.5",
30
- "@tramvai/tokens-common": "2.49.5",
31
- "@tramvai/tokens-server": "2.49.5",
26
+ "@tramvai/core": "2.51.0",
27
+ "@tramvai/module-environment": "2.51.0",
28
+ "@tramvai/papi": "2.51.0",
29
+ "@tramvai/state": "2.51.0",
30
+ "@tramvai/tokens-common": "2.51.0",
31
+ "@tramvai/tokens-server": "2.51.0",
32
32
  "std-env": "^2.2.1",
33
33
  "tslib": "^2.4.0"
34
34
  },