microsoftgraph / msgraph-sdk-php

Microsoft Graph Library for PHP.
Other
583 stars 144 forks source link

Logging example would be useful #1560

Open ianef opened 4 months ago

ianef commented 4 months ago

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.