Skip to content

Commit

Permalink
refactor: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Fifciu committed Oct 29, 2024
1 parent 2a6cfd4 commit 2a8054f
Show file tree
Hide file tree
Showing 13 changed files with 620 additions and 588 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Working with logger
# Logging

The middleware application provides a logger instance that automatically attaches metadata related to the scope of each call. By adding this contextual data, the logger makes it significantly easier to trace the origin of logs or errors, simplifying the debugging and monitoring process across the application.

## Using logger

The middleware application provides access to the logger in various parts of the system, such as in extensions and integrations. This flexibility allows you to log important events, errors, and other data throughout the lifecycle of your application. Whether you’re working in an extension or an integration, you can easily utilize the logger to capture and track necessary information.
The middleware application provides access to the logger in various parts of the system, such as in extensions and integrations. This flexibility allows you to log important events, errors, and other data throughout the lifecycle of your application.

```json[Example log]
{
Expand All @@ -22,13 +22,17 @@ The middleware application provides access to the logger in various parts of the
}
```

In this section, we will explore how to access and use the logger in different parts of the application, ensuring you have the tools to log data efficiently and consistently.
In this section, we will explore how to access and use the logger in different parts of the application.

These examples will demonstrate the proper ways to obtain and interact with the logger, ensuring that you are able to implement consistent and effective logging practices across your middleware application. As you will see, `getLogger` function solves the problem in most cases.
The below examples will demonstrate how to use the application logger in a way that ensures that correct metadata is conveyed in the logs. To achieve that we will use `getLogger` function that will extract the logger instance from the passed.

:::tip
The logger is integrated in the middleware to provide additional metadata about scope of call with every log out of the box. In case of error, it also provides error boundary.
:::

### Example log

### extendApp
### `extendApp`

```ts
import { getLogger } from "@vue-storefront/middleware";
Expand All @@ -37,12 +41,12 @@ const someExtension = {
name: "some-extension",
extendApp(params) {
const logger = getLogger(params);
logger.info("Logged succesfully!");
logger.info("You can call a logger inside extendApp hook, called on bootstrap of the application");
}
};
```

### hooks
### Hooks

Logger is available in hooks factory:

Expand All @@ -53,7 +57,7 @@ const someExtension = {
name: "some-extension",
hooks(req, res, alokai) {
const logger = getLogger(alokai);
logger.info("hooks");
logger.info("You can call a logger every time a hooks factory gets invoked");
return {
// ...
};
Expand All @@ -72,22 +76,22 @@ const someExtension = {
return {
beforeCall(params) {
const logger = getLogger(params);
logger.info("smth");
logger.info("You can log every time a beforeCall hook gets invoked");
return params.args;
},
beforeCreate(params) {
const logger = getLogger(params);
logger.info("smth");
logger.info("You can log every time a beforeCreate hook gets invoked");
return params;
},
afterCall(params) {
const logger = getLogger(params);
logger.info("smth");
logger.info("You can log every time a afterCall hook gets invoked");
return params.response;
},
afterCreate(params) {
const logger = getLogger(params);
logger.info("smth");
logger.info("You can log every time a afterCreate hook gets invoked");
return params;
},
};
Expand All @@ -106,14 +110,14 @@ const someExtension = {
name: "some-extension",
hooks(req, res, alokai) {
const hooksLogger = getLogger(alokai);
hooksLogger.info("hooks");
hooksLogger.info("You can call a logger every time a hooks factory gets invoked");
return {
beforeCall(params) {
// Never access via closure a logger belonging to the hooks factory:
hooksLogger.info("smth");
hooksLogger.info("Don't do that!");
// Instead use own logger of every hook function:
const logger = getLogger(params);
logger.info("smth");
logger.info("Do that!");
return params.args;
}
};
Expand All @@ -137,7 +141,7 @@ const testingExtension = {
extendApiMethods: {
addedCustomEndpoint(context) {
const logger = getLogger(context);
logger.info("some log");
logger.info("You can log inside newly created handler!");
return {
// ...
};
Expand All @@ -160,7 +164,7 @@ const onCreate = async (
alokai: AlokaiContainer
) => {
const logger = getLogger(alokai);
logger.info("smth");
logger.info("You can log inside onCreate when creating a new integration");

return {
// ...
Expand All @@ -175,7 +179,7 @@ The hook got a new parameter from which you can derive the logger with help of `
```ts
const init = (settings: any, alokai: AlokaiContainer) => {
const logger = getLogger(alokai);
logger.info("smth");
logger.info("You can log inside init function when creating an integration");

return {
config: settings,
Expand Down
Loading

0 comments on commit 2a8054f

Please sign in to comment.