From 1f6e6606536e9cea13445b84a8dea33886e5c6cc Mon Sep 17 00:00:00 2001 From: Bartosz Herba Date: Wed, 6 Nov 2024 12:51:05 +0100 Subject: [PATCH] feat: change log level to verbosity (#7306) --- .changeset/bright-ways-play.md | 7 ++ .../3.middleware/2.guides/10.logging.md | 47 ++++++----- .../4.sdk/2.getting-started/4.logger.md | 80 ++++++------------- .../structuredLog/gcpStructuredLog.spec.ts | 4 +- .../logger/src/ConsolaStructuredLogger.ts | 16 ++-- packages/logger/src/LoggerFactory.ts | 2 +- .../{LogLevel.ts => LogVerbosity.ts} | 2 +- .../logger/src/interfaces/LoggerOptions.ts | 4 +- .../logger/src/interfaces/StructuredLog.ts | 4 +- .../logger/src/interfaces/StructuredLogger.ts | 4 +- packages/logger/src/interfaces/index.ts | 2 +- .../src/structuredLog/GCPStructuredLog.ts | 10 +-- packages/middleware/src/types/config.ts | 12 +-- .../middlewareModule/utils/defaultLogger.ts | 2 +- 14 files changed, 81 insertions(+), 115 deletions(-) create mode 100644 .changeset/bright-ways-play.md rename packages/logger/src/interfaces/{LogLevel.ts => LogVerbosity.ts} (89%) diff --git a/.changeset/bright-ways-play.md b/.changeset/bright-ways-play.md new file mode 100644 index 0000000000..9e88faf78d --- /dev/null +++ b/.changeset/bright-ways-play.md @@ -0,0 +1,7 @@ +--- +"@vue-storefront/middleware": minor +"@vue-storefront/logger": minor +"@vue-storefront/sdk": minor +--- + +Change LogLevel into LogVerbosity diff --git a/docs/content/3.middleware/2.guides/10.logging.md b/docs/content/3.middleware/2.guides/10.logging.md index c5a09051a8..dc43a1a0a2 100644 --- a/docs/content/3.middleware/2.guides/10.logging.md +++ b/docs/content/3.middleware/2.guides/10.logging.md @@ -41,8 +41,10 @@ const someExtension = { name: "some-extension", extendApp(params) { const logger = getLogger(params); - logger.info("You can call a logger inside extendApp hook, called on bootstrap of the application"); - } + logger.info( + "You can call a logger inside extendApp hook, called on bootstrap of the application" + ); + }, }; ``` @@ -57,7 +59,9 @@ const someExtension = { name: "some-extension", hooks(req, res, alokai) { const logger = getLogger(alokai); - logger.info("You can call a logger every time a hooks factory gets invoked"); + logger.info( + "You can call a logger every time a hooks factory gets invoked" + ); return { // ... }; @@ -110,7 +114,9 @@ const someExtension = { name: "some-extension", hooks(req, res, alokai) { const hooksLogger = getLogger(alokai); - hooksLogger.info("You can call a logger every time a hooks factory gets invoked"); + 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: @@ -119,7 +125,7 @@ const someExtension = { const logger = getLogger(params); logger.info("Do that!"); return params.args; - } + }, }; }, }; @@ -145,8 +151,8 @@ const testingExtension = { return { // ... }; - } - } + }, + }, }; ``` @@ -202,25 +208,18 @@ export interface LoggerOptions { /** * The log level aligned with RFC5424. */ - level?: LogLevel; // (def. "info") + verbosity?: LogVerbosity; // (def. "info") /** * Whether to include the stack trace in the log message. */ includeStackTrace?: boolean; // (def. true) - - /** - * Own implementation of logger to be used internally. - * - * @remarks If provided then other options won't be used. - */ - handler?: LoggerInterface; } ``` -#### 1. level +#### 1. Verbosity -This option sets the log level based on the [RFC 5424](https://datatracker.ietf.org/doc/html/rfc5424) syslog standard. It defines the severity of log messages that should be recorded. Available log levels are: +This option sets the log verbosity level based on the [RFC 5424](https://datatracker.ietf.org/doc/html/rfc5424) syslog standard. It defines the severity of log messages that should be recorded. Available log levels are: - **emergency**: The highest severity level, indicating a system-wide emergency. - **alert**: Indicates an urgent condition that requires immediate attention. @@ -232,12 +231,12 @@ This option sets the log level based on the [RFC 5424](https://datatracker.ietf. - **debug**: The most detailed log level, useful for tracing and debugging the application. **Example:** -Setting the log level to `error` ensures only error messages and above (i.e., critical, alert, emergency) will be captured: +Setting the log verbosity level to `error` ensures only error messages and above (i.e., critical, alert, emergency) will be captured: ```json { "logger": { - "level": "error" + "verbosity": "error" } } ``` @@ -247,7 +246,7 @@ For a development environment, you might prefer a more verbose log level like `d ```json { "logger": { - "level": "debug" + "verbosity": "debug" } } ``` @@ -272,7 +271,7 @@ To configure the logger globally, add a `logger` object to the top-level configu { "logger": { "includeStackTrace": true, - "level": "debug" + "verbosity": "debug" }, // The rest of the middleware configuration "integrations": { @@ -294,7 +293,7 @@ For example, here is how you would configure the logger for an integration with "integrations": { "commerce": { "location": "@vsf-enterprise/sapcc-api/server", - + "configuration": { // Configuration of integration itself } @@ -302,7 +301,7 @@ For example, here is how you would configure the logger for an integration with // Configuration of the logger only for "commerce" integration "logger": { "includeStackTrace": true, - "level": "debug" + "verbosity": "debug" }, } } @@ -337,4 +336,4 @@ export interface LoggerInterface { Each method in this interface corresponds to a log level defined by [RFC 5424](https://datatracker.ietf.org/doc/html/rfc5424), and the logger implementation should handle the provided `logData` and optional `metadata`. -Once a custom `handler` is provided, **all other logger configuration options (such as `level` or `includeStackTrace`) will no longer be used**. This allows for complete control over how the logs are processed. +Once a custom `handler` is provided, **all other logger configuration options (such as `verbosity` or `includeStackTrace`) will no longer be used**. This allows for complete control over how the logs are processed. diff --git a/docs/content/4.sdk/2.getting-started/4.logger.md b/docs/content/4.sdk/2.getting-started/4.logger.md index 268aea915d..82a40ad83d 100644 --- a/docs/content/4.sdk/2.getting-started/4.logger.md +++ b/docs/content/4.sdk/2.getting-started/4.logger.md @@ -11,9 +11,10 @@ If the Logger is already included in the Alokai Storefront, you can skip this st The Logger is provided by the SDK and framework specific packages. In order to install the Logger, you need to update following packages to at least the following versions: -* `@vue-storefront/sdk` to `3.3.0` -* `@vue-storefront/nuxt` to `6.2.0` -* `@vue-storefront/next` to `4.3.0` + +- `@vue-storefront/sdk` to `3.3.0` +- `@vue-storefront/nuxt` to `6.2.0` +- `@vue-storefront/next` to `4.3.0` After updating the packages, you need to provide the Logger module to the SDK config. @@ -46,35 +47,36 @@ export default defineSdkConfig(({ buildModule, loggerModule, middlewareModule, g // ... })); ``` -:: +:: ## Configuration The default configuration is already provided, but you can customize it by providing the following options: -* `level` - the minimum level of the message to be logged. The default value is `info`. Possible values are: - * `emergency` - * `alert` - * `critical` - * `error` - * `warning` - * `notice` - * `info` - * `debug` -* `includeStackTrace` - a boolean value that determines if the stack trace should be included in the log message. The default value is `true`. -* `handler` - a custom handler that will be called when the message is logged. +- `verbosity` - the minimum level of the message to be logged. The default value is `info`. Possible values are: + - `emergency` + - `alert` + - `critical` + - `error` + - `warning` + - `notice` + - `info` + - `debug` +- `includeStackTrace` - a boolean value that determines if the stack trace should be included in the log message. The default value is `true`. 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, e.g.: + ```ts // ... logger: buildModule(loggerModule, { - level: 'debug', + verbosity: 'debug', includeStackTrace: false, }), ``` ### Custom handler + There might a certain use case where you want to customize the way the Logger logs messages e.g. you want to change a log provider to a different one. Then you can provide your own custom handler for the Logger. The handler must implement the `LoggerInterface` interface provided in the section [types](#type). @@ -93,27 +95,27 @@ You can use the Logger in the same way as you would use any other SDK modules: ::code-group ```tsx [Server Side] -import { getSdk } from '@/sdk'; +import { getSdk } from "@/sdk"; function ServerComponent() { const sdk = getSdk(); - sdk.logger.info('Example server side log'); + sdk.logger.info("Example server side log"); // ... } ``` ```tsx [Client Side] -import { useSdk } from '@/sdk/alokai-context'; +import { useSdk } from "@/sdk/alokai-context"; function ClientComponent() { const sdk = useSdk(); useEffect(() => { - sdk.logger.info('Example client side log'); + sdk.logger.info("Example client side log"); }, []); - + // ... } ``` @@ -128,41 +130,7 @@ function ClientComponent() { useSdk().logger.info('Example log');