Skip to content

Commit b767318

Browse files
committed
docs(tfal-56): next/nuxt logger implementation
1 parent 0109f6b commit b767318

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Logger
2+
3+
The Logger provides a logging utility for Alokai Storefront projects. It allows you to log messages at various levels
4+
and ability to provide additional context.
5+
6+
It is created to provide a unified way of logging messages across the project along with providing additional data out of the box.
7+
8+
## Installation
9+
10+
If the Logger is already included in the Alokai Storefront, you can skip this step.
11+
12+
The Logger is provided by the SDK and framework specific packages.
13+
14+
In order to install the Logger, you need to update following packages to at least the following versions:
15+
* `@vue-storefront/sdk` to `3.3.0`
16+
* `@vue-storefront/nuxt` to `6.2.0`
17+
* `@vue-storefront/next` to `4.3.0`
18+
19+
After updating the packages, you need to provide the Logger module to the SDK config.
20+
21+
::tabs{:titles='["Next.js", "Nuxt"]' class="mt-8"}
22+
23+
#tab-1
24+
25+
```ts
26+
// apps/storefront-unified-nextjs/sdk/config.ts
27+
28+
// ...
29+
export function getSdkConfig() {
30+
return defineSdkConfig(({ buildModule, config, getRequestHeaders, middlewareModule }) => ({ // [!code --]
31+
return defineSdkConfig(({ buildModule, config, getRequestHeaders, loggerModule, middlewareModule }) => ({ // [!code ++]
32+
logger: buildModule(loggerModule), // [!code ++]
33+
// ...
34+
}));
35+
}
36+
```
37+
38+
#tab-2
39+
40+
```ts
41+
// apps/storefront-unified-nuxt/sdk/config.ts
42+
43+
// ...
44+
export default defineSdkConfig(({ buildModule, middlewareModule, getRequestHeaders, config }) => ({ // [!code --]
45+
export default defineSdkConfig(({ buildModule, loggerModule, middlewareModule, getRequestHeaders, config }) => ({ // [!code ++]
46+
logger: buildModule(loggerModule), // [!code ++]
47+
// ...
48+
}));
49+
```
50+
::
51+
52+
53+
## Configuration
54+
55+
Available options:
56+
* `level` - the minimum level of the message to be logged. The default value is `info`. Possible values are:
57+
* `emergency`
58+
* `alert`
59+
* `critical`
60+
* `error`
61+
* `warning`
62+
* `notice`
63+
* `info`
64+
* `debug`
65+
* `includeStackTrace` - a boolean value that determines if the stack trace should be included in the log message. The default value is `true`.
66+
* `handler` - a custom handler that will be called when the message is logged.
67+
68+
To provide the configuration for the Logger, you need to update the SDK config with the Logger module configuration by providing an object with options to the `buildModule` function, f.e:
69+
```ts
70+
// ...
71+
logger: buildModule(loggerModule, {
72+
level: 'debug',
73+
includeStackTrace: false,
74+
handler: myCustomHandler,
75+
}),
76+
```
77+
78+
### Custom handler
79+
There might a certain use case where you want to customize the way the Logger logs messages f.e. you want to change a
80+
log provider to a different one.
81+
Then you can provide your own custom handler for the Logger. The handler must implement the `LoggerInterface` interface provided in the section [types](#type).
82+
83+
::warning
84+
Keep in mind that if you provide a custom handler, it will override the built-in logging functions and the other options
85+
passed to the Logger configuration will be ignored.
86+
::
87+
88+
## Usage
89+
90+
You can use the Logger in the same way as you would use any other SDK modules:
91+
92+
::tabs{:titles='["Next.js", "Nuxt"]' class="mt-8"}
93+
94+
#tab-1
95+
96+
::code-group
97+
98+
```tsx [Server Side]
99+
import { getSdk } from '@/sdk';
100+
101+
function ServerComponent() {
102+
const sdk = getSdk();
103+
104+
sdk.logger.info('Example server side log');
105+
106+
// ...
107+
}
108+
```
109+
110+
```tsx [Client Side]
111+
import { useSdk } from '@/sdk/alokai-context';
112+
113+
function ClientComponent() {
114+
const sdk = useSdk();
115+
116+
useEffect(() => {
117+
sdk.logger.info('Example client side log');
118+
}, []);
119+
120+
// ...
121+
}
122+
```
123+
124+
::
125+
126+
#tab-2
127+
128+
```vue
129+
// component
130+
<script setup>
131+
useSdk().logger.info('Example log');
132+
<script>
133+
```
134+
::
135+
136+
Now instead of using `console.log` you can use the Logger to log messages at different levels.
137+
138+
## Type
139+
140+
```ts
141+
type LogData = unknown;
142+
type Metadata = Record<string, unknown>;
143+
type LogLevel =
144+
| "emergency"
145+
| "alert"
146+
| "critical"
147+
| "error"
148+
| "warning"
149+
| "notice"
150+
| "info"
151+
| "debug";
152+
153+
interface LoggerInterface {
154+
emergency(logData: LogData, metadata?: Metadata): void;
155+
alert(logData: LogData, metadata?: Metadata): void;
156+
critical(logData: LogData, metadata?: Metadata): void;
157+
error(logData: LogData, metadata?: Metadata): void;
158+
warning(logData: LogData, metadata?: Metadata): void;
159+
notice(logData: LogData, metadata?: Metadata): void;
160+
info(logData: LogData, metadata?: Metadata): void;
161+
debug(logData: LogData, metadata?: Metadata): void;
162+
}
163+
164+
type LoggerModuleConfig = Partial<{
165+
level: LogLevel;
166+
includeStackTrace: boolean;
167+
handler: LoggerInterface;
168+
[key: string]: any;
169+
}>;
170+
171+
```

0 commit comments

Comments
 (0)