usermaven / usermaven-js

Usermaven provides instant actionable analytics to grow your SaaS business.
MIT License
1 stars 2 forks source link

fix: logger issue #123

Closed seeratawan01 closed 2 weeks ago

seeratawan01 commented 2 weeks ago

PR Type

Enhancement, Bug fix


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
10 files
client.ts
Improve logging configuration and integration in UsermavenClient

packages/javascript-sdk/src/core/client.ts
  • Updated logger initialization to use configurable log level.
  • Passed logger instance to various components for consistent logging.
  • Enhanced logging in transport and persistence initialization.
  • +12/-10 
    local-storage.ts
    Integrate logger into LocalStoragePersistence                       

    packages/javascript-sdk/src/persistence/local-storage.ts
  • Added logger parameter to constructor.
  • Replaced direct logger calls with instance logger.
  • +8/-6     
    autocapture.ts
    Enhance logging and element handling in AutoCapture           

    packages/javascript-sdk/src/tracking/autocapture.ts
  • Added logger to AutoCapture constructor.
  • Improved element capture condition handling.
  • +9/-3     
    beacon.ts
    Integrate logger into BeaconTransport                                       

    packages/javascript-sdk/src/transport/beacon.ts
  • Added logger to BeaconTransport constructor.
  • Replaced direct logger calls with instance logger.
  • +2/-2     
    fetch.ts
    Integrate logger into FetchTransport                                         

    packages/javascript-sdk/src/transport/fetch.ts
  • Added logger to FetchTransport constructor.
  • Replaced direct logger calls with instance logger.
  • +4/-4     
    https.ts
    Integrate logger into HttpsTransport                                         

    packages/javascript-sdk/src/transport/https.ts
  • Added logger to HttpsTransport constructor.
  • Replaced direct logger calls with instance logger.
  • +2/-2     
    xhr.ts
    Integrate logger into XhrTransport                                             

    packages/javascript-sdk/src/transport/xhr.ts
  • Added logger to XhrTransport constructor.
  • Replaced direct logger calls with instance logger.
  • +10/-7   
    helpers.ts
    Refine log level parsing and default setting                         

    packages/javascript-sdk/src/utils/helpers.ts
  • Updated default log level to ERROR in parseLogLevel function.
  • Improved log level parsing logic.
  • +9/-2     
    logger.ts
    Allow configurable log level in logger                                     

    packages/javascript-sdk/src/utils/logger.ts - Added log level parameter to getLogger function.
    +2/-2     
    queue.ts
    Integrate logger into RetryQueue                                                 

    packages/javascript-sdk/src/utils/queue.ts
  • Added logger to RetryQueue constructor.
  • Replaced direct logger calls with instance logger.
  • +7/-6     
    Configuration changes
    2 files
    config.ts
    Update default logging level to ERROR                                       

    packages/javascript-sdk/src/core/config.ts - Changed default log level from WARN to ERROR.
    +1/-1     
    index.html
    Simplify script tag by removing log level attribute           

    packages/javascript-sdk/examples/index.html - Removed data-log-level attribute from script tag.
    +0/-1     
    Bug fix
    1 files
    common.ts
    Adjust logging level for window availability check             

    packages/javascript-sdk/src/utils/common.ts - Changed logger call from error to warn for window availability.
    +1/-1     

    ๐Ÿ’ก PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    github-actions[bot] commented 2 weeks ago

    PR Reviewer Guide ๐Ÿ”

    Here are some key observations to aid the review process:

    ๐Ÿ… Score: 85
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Recommended focus areas for review

    Error Handling
    The error handling in transport methods should be reviewed to ensure that they gracefully handle exceptions and provide useful feedback to the user or system. Local Storage Access
    The code should handle potential exceptions when accessing local storage more robustly, especially considering different browser security settings that might block local storage access.
    Code feedback:
    relevant filepackages/javascript-sdk/src/core/client.ts
    suggestion       Consider adding a fallback or default value for `config.namespace` to ensure it always has a valid value. [important]
    relevant linethis.namespace = config.namespace || 'usermaven';

    relevant filepackages/javascript-sdk/src/core/client.ts
    suggestion       Ensure that `RetryQueue` initialization parameters are validated or have sensible defaults to prevent potential issues with extreme values. [medium]
    relevant linethis.retryQueue = new RetryQueue(

    relevant filepackages/javascript-sdk/src/persistence/local-storage.ts
    suggestion       Add error handling for JSON parsing in the `load` method to catch and handle potential exceptions from malformed data. [important]
    relevant linethis.logger.error('Error loading from localStorage:', error);

    relevant filepackages/javascript-sdk/src/transport/fetch.ts
    suggestion       Consider implementing retry logic in `FetchTransport.send` method for failed HTTP requests, especially for recoverable HTTP statuses. [medium]
    relevant linethrow new Error(`HTTP error! status: ${response.status}`);

    relevant filepackages/javascript-sdk/src/utils/helpers.ts
    suggestion       Refactor `parseLogLevel` to throw an error or log a warning when an unrecognized log level is passed. This will prevent silent failures. [medium]
    relevant linereturn LogLevel.ERROR;

    github-actions[bot] commented 2 weeks ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Implement error handling for JSON parsing in the load method to handle potential data corruption gracefully ___ **Add error handling for JSON parsing in the load method to prevent application
    crashes due to corrupted localStorage data.** [packages/javascript-sdk/src/persistence/local-storage.ts [52-54]](https://github.com/usermaven/usermaven-js/pull/123/files#diff-846d61ee126d4dac6b174bdf629abecdae565db54b7f836eb6b39e639ebb5466R52-R54) ```diff const data = localStorage.getItem(this.prefix + 'data'); -if (data) { - this.storage = JSON.parse(data); +try { + if (data) { + this.storage = JSON.parse(data); + } +} catch (error) { + this.logger.error('Failed to parse localStorage data:', error); } ```
    Suggestion importance[1-10]: 9 Why: Adding error handling for JSON parsing is a significant enhancement that prevents application crashes due to corrupted localStorage data, thereby improving the application's resilience and user experience.
    9
    Enhance the parseLogLevel function to log a warning for unrecognized log levels ___ **Refactor the parseLogLevel function to handle unexpected values gracefully by
    logging a warning when an unrecognized log level is provided.** [packages/javascript-sdk/src/utils/helpers.ts [68-75]](https://github.com/usermaven/usermaven-js/pull/123/files#diff-51c6882c0bb5b1ea9ca3fda284cba02f7aa0a927d814f09b7b2f980473d87c90R68-R75) ```diff const level = (LogLevel[upperValue as keyof typeof LogLevel] as LogLevel) if (level || level === 0) { return level +} else { + this.logger.warn(`Unrecognized log level: ${value}`); + return LogLevel.ERROR; } -return LogLevel.ERROR; ```
    Suggestion importance[1-10]: 6 Why: The suggestion enhances the function by logging a warning for unrecognized log levels, which aids in debugging and ensures that unexpected values are handled more transparently.
    6
    Possible bug
    Initialize the logger before it is used in the RetryQueue constructor ___ **Ensure that the logger is initialized before using it in the RetryQueue constructor
    to avoid potential reference errors.** [packages/javascript-sdk/src/core/client.ts [35-36]](https://github.com/usermaven/usermaven-js/pull/123/files#diff-cf1b46df259d6f5f7aa4358f3de9b0e4ca7051836cc3f55729ad1c56a5f7ac40R35-R36) ```diff +this.logger = getLogger(this.config.logLevel); this.retryQueue = new RetryQueue( this.transport, this.config.maxSendAttempts || 3, this.config.minSendTimeout || 1000, 10, 200, // Reduced interval to .2 second this.logger ); ```
    Suggestion importance[1-10]: 8 Why: The suggestion addresses a potential bug by ensuring the logger is initialized before being used in the RetryQueue constructor, which is crucial for preventing reference errors and ensuring proper logging functionality.
    8
    Best practice
    Add validation for the config parameter in transport constructors to ensure it contains required properties ___ **Validate the config parameter in the BeaconTransport, FetchTransport, and
    XhrTransport constructors to ensure it contains necessary properties before use.** [packages/javascript-sdk/src/core/client.ts [184]](https://github.com/usermaven/usermaven-js/pull/123/files#diff-cf1b46df259d6f5f7aa4358f3de9b0e4ca7051836cc3f55729ad1c56a5f7ac40R184-R184) ```diff +if (!config.trackingHost) { + throw new Error('Configuration must include a trackingHost.'); +} return new BeaconTransport(config.trackingHost, config, this.logger); ```
    Suggestion importance[1-10]: 7 Why: This suggestion improves robustness by adding validation for the `config` parameter, ensuring that required properties like `trackingHost` are present before proceeding, which can prevent runtime errors.
    7