googleads / google-ads-php

Google Ads API Client Library for PHP
https://developers.google.com/google-ads/api/docs/client-libs/php
Apache License 2.0
295 stars 262 forks source link

Logging not working #994

Closed mohammadsiabbouforwork closed 9 months ago

mohammadsiabbouforwork commented 9 months ago

Your question: I'm trying to log requests I send, but it's not logging anything, I'm using Laravel logger :

 $config = new Configuration([
       'GOOGLE_ADS' => [
             'developerToken' => $this->developerToken,
             'loginCustomerId' => $this->customerId
        ],
        'OAUTH2' => [
            'clientId' => $client->getClientId(),
            'clientSecret' => $client->getClientSecret(),
            'refreshToken' => $client->getRefreshToken()
         ]
]);

$clientBuilder = (new GoogleAdsClientBuilder())
       ->from($config)
       ->withOAuth2Credential($oAuth2Credential)
       ->usingGapicV2Source(true)
       ->withLogger(Log::getLogger())
       ->withLogLevel(LogLevel::DEBUG)
       ->build();
$this->client = $clientBuilder->getGoogleAdsServiceClient();

Here is an example of a call :

        $countriesQuery =
            /** @lang text */
            " SELECT
                geo_target_constant.country_code,
                geo_target_constant.id
            FROM geo_target_constant
            WHERE geo_target_constant.id IN ( " . implode(',', $countriesIds) . " ) ";
        $stream = $this->client->searchStream(SearchGoogleAdsStreamRequest::build($this->customerId, $countriesQuery));

i would really appreciate the help, thank you.

fiboknacky commented 9 months ago

Hello,

I'm not sure how Log::getLogger() is configured, but if you create a logger similar to this line, it should just work.

The spec of the logger should be something similar to what is created in this method.

mohammadsiabbouforwork commented 9 months ago

Hey , thanks for your reply,

Log::getLogger() just returns an instance of a class implementing the LoggerInterface as defined here.

I even tried to create a simple class to avoid any issues related to Laravel :


use Psr\Log\LoggerInterface;
use Stringable;

class CustomLogger implements LoggerInterface
{

    public function emergency(Stringable|string $message, array $context = []): void
    {
        $this->log('emergency', $message, $context);
    }

    public function alert(Stringable|string $message, array $context = []): void
    {
        $this->log('alert', $message, $context);
    }

    public function critical(Stringable|string $message, array $context = []): void
    {
        $this->log('critical', $message, $context);
    }

    public function error(Stringable|string $message, array $context = []): void
    {
        $this->log('error', $message, $context);
    }

    public function warning(Stringable|string $message, array $context = []): void
    {
        $this->log('warning', $message, $context);
    }

    public function notice(Stringable|string $message, array $context = []): void
    {
        $this->log('info', $message, $context);
    }

    public function info(Stringable|string $message, array $context = []): void
    {
        $this->log('info', $message, $context);
    }

    public function debug(Stringable|string $message, array $context = []): void
    {
        $this->log('debug', $message, $context);
    }

    public function log($level, Stringable|string $message, array $context = []): void
    {
        file_put_contents("storage/logs", $message . PHP_EOL, FILE_APPEND);
    }
}

and used it instead :

        $logger = new CustomLogger();
        $logger->info("TEST " . time());

        $clientBuilder = (new GoogleAdsClientBuilder())
            ->from($config)
            ->withOAuth2Credential($oAuth2Credential)
            ->usingGapicV2Source(true)
            ->withLogger($logger)
            ->withLogLevel(LogLevel::DEBUG)
            ->build();

The "TEST" line shows up in the logs, but the library's logs don't! Are you sure this has nothing to do with the usingGapicV2Source ?

Sorry for bothering you with this, thanks for your help.

fiboknacky commented 9 months ago

usingGapicV2Source is completely unrelated. You can just remove it if you wish. Instead, if you don't set a logger at all, does it return anything? And you're sure that your query returns a result (not empty result), correct?

mohammadsiabbouforwork commented 9 months ago

if I don't set the logger it still returns results normally, however it won't show the logs , even while specifying a custom logFilePath. And yes, I have a fully working API integration class that returns non-empty results.

fiboknacky commented 9 months ago

Could you check if your path has the write permission set properly?

if I don't set the logger it still returns results normally, however it won't show the logs , even while specifying a custom logFilePath.

Is it possible for you to test your code in command prompt? It should output something if you don't set withLogger.

mohammadsiabbouforwork commented 9 months ago

Yes, the path has the write permission, I double-checked it. I tried running a script directly, and it doesn't show anything nor log anything when using a custom logger. I have to mention I'm running it in a docker container in case that effects anything.

fiboknacky commented 9 months ago

BTW, do you use REST or gRPC? I ask because logging is supported by gRPC at this moment.

mohammadsiabbouforwork commented 9 months ago

Hi, I double-checked, and it turned it I was using REST API, the documentations said it will use gRPC by default but since I didn't have the extension setup in the container it falls back to REST. I configured gRPC and used it, now the logs are working ok, sorry for bothering you with this, thanks for your help.

fiboknacky commented 9 months ago

No worries. I'm really glad that you could find the cause. :)

devjerry0 commented 6 months ago

Someone should add this to the logging docs! https://developers.google.com/google-ads/api/docs/client-libs/php/logging

fiboknacky commented 6 months ago

The PHP client library conforms to PSR-3 for logging and provides a logger for gRPC calls.

The above sentence is already in the guide, but I'll make it more clear. Thanks for suggestion.

devjerry0 commented 6 months ago

yeah, something like it defaults to rest if the extension is not installed would have been super helpful.