SalesforceCommerceCloud / pwa-kit

React-based JavaScript frontend framework to create a progressive web app (PWA) storefront for Salesforce B2C Commerce.
https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/pwa-kit-overview.html
BSD 3-Clause "New" or "Revised" License
283 stars 130 forks source link

@W-15634851@ [Spike] Filter PWA Kit server-side logs via a defined log level #1790

Closed adamraya closed 4 months ago

adamraya commented 4 months ago

Description

The solution ensures all logs are managed according to the specified log level by adding a new logger that overrides the console object and substitutes it with a custom logger using Winston.

The log level can be adjusted via the TESTMRT_LOG_LEVEL environment variable.

The 3pp winston is already approved.

Types of Changes

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

How to Test-Drive This PR

A bundle has been published with some test console statements in the _app component on the test-env-2.

if (isServer) {
      console.debug('PWAKITLOG >>> _app: console.debug')
      console.info('PWAKITLOG >>> _app: console.info')
      console.log('PWAKITLOG >>> _app: console.log')
      console.warn('PWAKITLOG >>> _app: console.warn')
      console.error('PWAKITLOG >>> _app: console.error')
}
  1. Run the tail-logs command against the test-env-2 environment.
npx @salesforce/pwa-kit-dev@latest tail-logs --project scaffold-pwa --environment test-env-2
  1. Adjust the TESTMRT_LOG_LEVEL value to "warn" using our cloud api key.
curl --location --request PATCH 'https://cloud.mobify.com/api/projects/scaffold-pwa/target/test-env-2/env-var/' \
--header 'Authorization: Bearer your-cloud-api-key' \
--header 'Content-Type: application/json' \
--data '{
    "TESTMRT_LOG_LEVEL": {"value": "warn"}
}'
  1. Load the bundle home page /: https://scaffold-pwa-test-env-2.mobify-storefront.com/

  2. Verify that only logs with the log level "warn" and above are visible.

    • 1️⃣ Incoming HTTP request using Morgan [pwa-kit-runtime]
    • 2️⃣ Express app Request ID and the CloudFront ID [pwa-kit-runtime]
    • 3️⃣ PWA Kit and other packages console warn and error statements
    • 4️⃣ User’s console statements declared in the template-retail-react-app
  3. Repeat the same adjusting the TESTMRT_LOG_LEVEL value to "info".

Checklists

General

Accessibility Compliance

You must check off all items in one of the follow two lists:

or...

Localization

adamraya commented 4 months ago

Closing Spike PR