@tramvai/module-log 2.94.7 → 2.94.9

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 +4 -190
  2. package/package.json +7 -7
package/README.md CHANGED
@@ -1,195 +1,9 @@
1
- # Log
1
+ # @tramvai/module-log
2
2
 
3
- Module adds `LOGGER_TOKEN` token implementation using library [@tinkoff/logger](references/libs/logger.md).
3
+ Module adds `LOGGER_TOKEN` token implementation using library [@tinkoff/logger](references/libs/logger.md)
4
+
5
+ Link to complete module documentation - https://tramvai.dev/docs/features/logging/
4
6
 
5
7
  ## Installation
6
8
 
7
9
  Module is automatically installs and adds with module `@tramvai/module-common`
8
-
9
- ## Explanation
10
-
11
- ### Display logs
12
-
13
- see [@tinkoff/logger](../libs/logger#display-logs).
14
-
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
-
17
- ### See logs from the server in browser
18
-
19
- This functionality is available only in dev-mode and can make development a little easier.
20
-
21
- In browser console when loading page of the app the special log group with name `Tramvai SSR Logs` will be showed. If you open this group you will see logs from the server that was logged to this particular request. Herewith will be displayed only logs that are enabled for the [displaying on the server](#display-logs). If you want to see all of the logs in browser with settings for [displaying in browser](#display-logs) you can specify env `DEBUG_FULL_SSR` when running app.
22
-
23
- ### See logs for the requests
24
-
25
- > Works only with [@tinkoff/request](https://tinkoff.github.io/tinkoff-request/)
26
-
27
- [http-client](references/modules/http-client.md) is already passes logger and its settings to the [log plugin](https://tinkoff.github.io/tinkoff-request/docs/plugins/log.html).
28
-
29
- Plugin automatically generates names for loggers using template `request.${name}` that might be used to setting up [displaying of logs](#display-logs):
30
-
31
- ```tsx
32
- const logger = di.get(LOGGER_TOKEN);
33
- const makeRequest = request([...otherPlugins, logger({ name: 'my-api-name', logger })]);
34
- ```
35
-
36
- As name of the logger equals to `my-api-name` to show logs:
37
-
38
- - on server extend env `LOG_ENABLE: 'request.my-api-name'`
39
- - on client call `logger.enable('request.my-api-name')`
40
-
41
- ### Change logger settings on server
42
-
43
- By default, settings for the logger on server are specified by envs `LOG_ENABLE` and `LOG_LEVEL`.
44
-
45
- You can change this settings in runtime using papi-route `{app}/private/papi/logger`
46
-
47
- Displaying of the logs is changed by query with the name `enable`, e.g.:
48
-
49
- ```
50
- https://localhost:3000/{app}/private/papi/logger?enable=request.tinkoff
51
- ```
52
-
53
- Level of the logs is change by query with the name `level`, e.g.:
54
-
55
- ```
56
- https://localhost:3000/{app}/private/papi/logger?level=warn
57
- ```
58
-
59
- To reset settings to default, based on env, use `mode=default`:
60
-
61
- ```
62
- https://localhost:3000/{app}/private/papi/logger?mode=default
63
- ```
64
-
65
- ### Env
66
-
67
- - `LOG_LEVEL` = trace | debug | info | warn | error | fatal - show logs with specified level and higher. E.g.:
68
- - if `LOG_LEVEL=info` then logs with levels `info`, `warn`, `error`, `fatal` will be showed
69
- - `LOG_ENABLE` = `${name}` | `${level}:${name}` - show logs with specified name of the logger or name + level. Several entries are passed with comma as delimiter. E.g.:
70
- - if `LOG_ENABLE=server` then show logs with the name `server`
71
- - if `LOG_ENABLE=trace:server*` then show logs with the name `server` and level `trace`
72
- - if `LOG_ENABLE=info:server,client,trace:shared` then show all of the specified logs using rules from above
73
-
74
- ### Debug
75
-
76
- Module uses logger with the id `ssr-logger`
77
-
78
- ## How to
79
-
80
- ### Example of base usage
81
-
82
- ```tsx
83
- import { Module, commandLineListToken, provide } from '@tramvai/core';
84
- import { LOGGER_TOKEN } from '@tramvai/module-common';
85
-
86
- @Module({
87
- providers: [
88
- provide({
89
- provide: commandLineListToken.customerStart,
90
- useFactory: ({ logger }) => {
91
- logger.debug('customer start'); // logging in the global namespace
92
-
93
- const myLogger = logger({
94
- name: 'test',
95
- });
96
-
97
- myLogger.warn('warning'); // logging in the namespace test
98
- myLogger.error('error!');
99
- },
100
- deps: {
101
- logger: LOGGER_TOKEN,
102
- },
103
- }),
104
- ],
105
- })
106
- export class MyModule {}
107
- ```
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
-
187
- ### How to properly format logs
188
-
189
- See [@tinkoff/logger](../libs/logger.md#how-to-log-properly)
190
-
191
- ## Exported tokens
192
-
193
- ### LOGGER_TOKEN
194
-
195
- Instance of the logger. Replaces base implementation for the `LOGGER_TOKEN` from the [@tramvai/module-common](references/modules/common.md)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-log",
3
- "version": "2.94.7",
3
+ "version": "2.94.9",
4
4
  "description": "",
5
5
  "browser": "lib/browser.js",
6
6
  "main": "lib/server.js",
@@ -22,12 +22,12 @@
22
22
  },
23
23
  "peerDependencies": {
24
24
  "@tinkoff/utils": "^2.1.2",
25
- "@tramvai/core": "2.94.7",
26
- "@tramvai/module-environment": "2.94.7",
27
- "@tramvai/papi": "2.94.7",
28
- "@tramvai/state": "2.94.7",
29
- "@tramvai/tokens-common": "2.94.7",
30
- "@tramvai/tokens-server": "2.94.7",
25
+ "@tramvai/core": "2.94.9",
26
+ "@tramvai/module-environment": "2.94.9",
27
+ "@tramvai/papi": "2.94.9",
28
+ "@tramvai/state": "2.94.9",
29
+ "@tramvai/tokens-common": "2.94.9",
30
+ "@tramvai/tokens-server": "2.94.9",
31
31
  "std-env": "^2.2.1",
32
32
  "tslib": "^2.4.0"
33
33
  },