microsoftgraph / microsoftgraph/msgraph-sdk-php

Logging example would be useful

Open
#1,560 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation type:question
Dominant language
PHP
Stars
669
Forks
150
Avg merge
15h 21m
Merged PRs (30d)
3

Description

I can't find any practical examples of how to apply logging to graph requests. When things don't work as expected it's really handy to be able to see what you are actually sending and receiving from the API.

I've tried the following but it crashes trying to create the client:

        $this->tokenRequestContext = new AuthorizationCodeContext(
            $this->config['tenant_id'],
            $this->config['app_id'],
            $this->config['client_secret_value'],
            'NOT_AUTHENTICATED', // Auth code dummy value
            $this->urlGenerator->generate('ms365_login_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
        );

        $tokenCache = new UserAccessTokenCache($this->tokenRequestContext); // Custom cache
        // Create the default handler stack
        $handlerStack = GraphClientFactory::getDefaultHandlerStack();

        // Add the log handler
        $handlerStack->push(Middleware::log(
            $this->logger,
            new \GuzzleHttp\MessageFormatter('{req_headers} - {req_body} - {res_headers} - {res_body}')
        ));

        $adapter = new GraphRequestAdapter(
            GraphPhpLeagueAuthenticationProvider::createWithAccessTokenProvider(
                GraphPhpLeagueAccessTokenProvider::createWithCache(
                    $tokenCache,
                    $this->tokenRequestContext,
                    self::AZURE_SCOPES
                )
            ),
            GraphClientFactory::createWithMiddleware($handlerStack)
        );

        $this->graphServiceClient = GraphServiceClient::createWithRequestAdapter($adapter);

Any help with this would be very handy.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the GraphClientFactory handler stack, Middleware::log, and GraphRequestAdapter setup shown in the issue. Verify why the provided client-creation example crashes, then document a practical way to log Graph API request and response details. Done means a newcomer can follow the example successfully and inspect the exchanged data.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.