pwa-kit icon indicating copy to clipboard operation
pwa-kit copied to clipboard

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

Open adamraya opened this issue 9 months ago • 0 comments

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

  • [ ] Bug fix (non-breaking change that fixes an issue)
  • [x] New feature (non-breaking change that adds functionality)
  • [ ] Documentation update
  • [ ] Breaking change (could cause existing functionality to not work as expected)
  • [ ] Other changes (non-breaking changes that does not fit any of the above)

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

  • Override the console object with a custom logger using Winston in pwa-kit-react-sdk and pwa-kit-runtime.

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
  1. Repeat the same adjusting the TESTMRT_LOG_LEVEL value to "info".

Checklists

General

  • [ ] Changes are covered by test cases
  • [ ] CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

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

  • [ ] There are no changes to UI

or...

Localization

  • [ ] Changes include a UI text update in the Retail React App (which requires translation)

adamraya avatar May 15 '24 21:05 adamraya