Problem/Motivation

The client should be capable of using a custom logger. 🪵

Proposed resolution

* A 'logger' property should be added to the base ApiClient class.
** Logger will be an object optionally containing methods for each of the NPM logging levels: error, warn, info, http, verbose, debug, silly.
* A 'log' method will be added to the base ApiClient class.
** log will take a level and a message (both strings) as parameters and call the equivalent logger method (passing the message) if it exists.
* A defaultLogger utility will be added to the ApiClient package.
** It is a logger that uses console.log/debug/warn, etc. for the related logging levels.
* The ApiClient class will use the defaultLogger if a custom logger is not provided.
* A debug property will be added to the JsonApiClient class.
* If debug is true, the client will perform debug logging using the provided logger.

Remaining tasks

* Implement changes as outlined above.
* Document inline using tsdoc
* Add test coverage.

Determine which (if any) logging library to use as a default logger.
Some libraries include tslog and typescript-logging
- Current example just uses a zero dependency solution of console log methods. Wondering if maybe this is the right choice for the base class, and we could later decide to use a logging library for the JsonApiClient if we prefer it.

How verbose will the logs be? Should we allow multiple levels of verbosity?
- Current example uses a debug logging level when debug is enabled. The logger following npm logging levels adds some flexibility here.

Should we provide a way to pass a variable to the logger?

API changes

* Adding a log method to ApiClient

Data model changes

ApiClient:
* Adding a logger property
* Adding a log method.
JsonApiClient:
* Adding a debug property

Issue fork api_client-3377149

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

coby.sher created an issue. See original summary.

coby.sher’s picture

Issue summary: View changes

brianperry made their first commit to this issue’s fork.

brianperry’s picture

Pushed up a working branch - I found that it was difficult to expand on the requirements here without experimenting in code a little bit. Getting there...

brianperry’s picture

Issue summary: View changes
coby.sher’s picture

Status: Active » Needs work
shrutishende’s picture

Assigned: Unassigned » shrutishende
shrutishende’s picture

Status: Needs work » Active
shrutishende’s picture

Assigned: shrutishende » Unassigned
Status: Active » Needs review

pratik_kamble made their first commit to this issue’s fork.

pratik_kamble’s picture

Status: Needs review » Reviewed & tested by the community

When running locally, if the 'debug' option is set to true, the code adds debug messages using the default logger. To view these messages, you can adjust the default log level in your Chrome settings.

Alternatively, if you provide a custom logger, the code will use that custom logger instead."

brianperry’s picture

Status: Reviewed & tested by the community » Needs work

Great progress. I think there is just one thing I'd like to see adjusted - the LogLevels type seems to strict to me. https://git.drupalcode.org/project/api_client/-/merge_requests/6#note_21...

Setting to needs work as a result.

I also added some test coverage which should be considered when this is reviewed next.

pratik_kamble’s picture

Assigned: Unassigned » pratik_kamble
Status: Needs work » Active

  • brianperry committed cfd02a9f on canary
    Issue #3377149 by brianperry, shrutishende, coby.sher, pratik_kamble:...
brianperry’s picture

Assigned: pratik_kamble » Unassigned
Status: Active » Fixed

Merged and marking as fixed. Thanks all! Created https://www.drupal.org/project/api_client/issues/3393109 as a follow up issue.

  • brianperry committed cfd02a9f on main
    Issue #3377149 by brianperry, shrutishende, coby.sher, pratik_kamble:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.