با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این کتابخانه گزارش همه کاره را برای تعاملات API Google Ads فراهم می کند. می توانید عکس بگیرید:
اطلاعات دقیق: درخواست های کامل به API ارسال شده و پاسخ های دریافت شده.
خلاصه های مختصر: مروری بر سطح بالا از تعاملات.
و شما می توانید این تنظیمات ورود به سیستم را به دو روش کنترل کنید:
پیکربندی کتابخانه مشتری: از گزینه های پیکربندی خاص کتابخانه استفاده کنید.
برنامه نویسی با پایتون: برای کنترل مستقیم بیشتر از چارچوب لاگ داخلی پایتون استفاده کنید.
هنگامی که یک نمونه GoogleAdsClient مقداردهی اولیه می شود، گزارش به طور خودکار پیکربندی می شود. برای مثال، زمانی که از متد load_from_storage استفاده می کنید، این مرحله مقداردهی اولیه رخ می دهد. در این مرحله، کتابخانه:
تنظیمات گزارش مشخص شده خود را از پیکربندی آن بخوانید.
هنگامی که این کتابخانه به عنوان یک بسته نصب شده استفاده می شود، باید گزارش آن را با تنظیمات گزارش برنامه خود ادغام کنید. به طور خاص، شما باید با متد addHandler یک کنترل کننده گزارش را به نمونه لاگر خود کتابخانه اضافه کنید. این کنترل کننده تعیین می کند که پیام های گزارش کتابخانه به کجا هدایت شوند (مثلاً به کنسول، یک فایل و غیره). برای انجام این کار، ابتدا نمونه لاگر کتابخانه را همانطور که در اینجا نشان داده شده است بازیابی کنید:
پس از بازیابی لاگر کتابخانه، می توانید به آن بگویید که کجا پیام های گزارش را خروجی کند.
ارسال گزارشها به کنسول: برای نمایش پیامهای گزارش روی کنسول خود، یک کنترلکننده اصلی اضافه میکنید. در اینجا نحوه هدایت لاگ ها به خروجی استاندارد ( stdout ) آمده است:
و در اینجا نحوه تنظیم یک کنترل کننده اصلی است که به لاگر می گوید که در stdout چاپ کند:
importsys# Assuming 'logger' was retrieved as per previous instructionslogger.addHandler(logging.StreamHandler(sys.stdout))
اگر ترجیح می دهید گزارش ها را به خطای استاندارد ( stderr ) بفرستید، که اغلب برای پیام های خطا و هشدارها استفاده می شود:
importsys# Assuming 'logger' was retrieved as per previous instructionslogger.addHandler(logging.StreamHandler(sys.stderr))
پیکربندی سایر تنظیمات گزارشگیری بهصورت برنامهای: هنگامی که شی لاگر را دارید، میتوانید با استفاده از ماژول ورود به سیستم داخلی پایتون، تنظیمات دیگر گزارشگیری را نیز به صورت برنامهنویسی تغییر دهید. به عنوان مثال، برای تغییر سطح گزارش به DEBUG (که پیام های دقیق تری را نشان می دهد):
logger.setLevel(logging.DEBUG)
سطوح ورود به سیستم
کلاینت گزارشها را در چند سطح مختلف تولید میکند و میتوانید پیکربندی خود را برای دیدن برخی یا همه موارد زیر تنظیم کنید:
سطح
درخواست موفقیت آمیز
درخواست ناموفق
DEBUG
گزارش دقیق با اشیاء درخواست و پاسخ کامل به عنوان JSON.
گزارش تفصیلی با درخواست کامل و اشیاء استثنا به عنوان JSON.
INFO
خلاصه ای مختصر با فیلدهای درخواست و پاسخ خاص.
گزارش تفصیلی با درخواست کامل و اشیاء استثنا به عنوان JSON.
WARNING
هیچ کدام
خلاصه ای مختصر با اطلاعات درخواستی خاص، وضعیت استثنا و پیام.
از آنجایی که چارچوب ورود به سیستم پایتون پیامهای گزارشی را که شدیدتر از سطح پیکربندی شده هستند نادیده میگیرد، تنظیم روی WARNING به این معنی است که شما فقط پیامهای مختصر مربوط به درخواستهای ناموفق را خواهید دید، اما تنظیم روی DEBUG به این معنی است که همه انواع گزارشهای ممکن را در جدول بالا مشاهده خواهید کرد.
ورود به فایل
هنگامی که اسکریپتهای نمونه مانند get_campaigns.py را از خط فرمان خود اجرا میکنید، هر پیام گزارشی که معمولاً در کنسول چاپ میشود را میتوان به یک فایل هدایت کرد (یا "لولهای" کرد). این یکی از ویژگی های پوسته سیستم عامل شما است، نه خود کتابخانه پایتون. در اینجا به این صورت است:
برای ذخیره خروجی استاندارد (بیشتر گزارشها) در یک فایل (بازنویسی آن):
کتابخانه مشتری پایتون از رهگیرهای gRPC برای دسترسی و ثبت جزئیات درخواست و پاسخ استفاده می کند. شما می توانید ثبت سفارشی خود را با ایجاد یک رهگیر gRPC با منطق سفارشی تنظیم کنید. برای جزئیات بیشتر و مثالی از یک رهگیر ثبت سفارشی، به راهنمای ورود به سیستم مراجعه کنید.
تاریخ آخرین بهروزرسانی 2025-09-03 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-09-03 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThe Google Ads API Python client library offers configurable logging for interactions, allowing detailed or summary logs of requests and responses.\u003c/p\u003e\n"],["\u003cp\u003eLogging is managed via the client library configuration and utilizes Python's built-in logging framework, outputting to \u003ccode\u003estderr\u003c/code\u003e by default.\u003c/p\u003e\n"],["\u003cp\u003eLog levels can be adjusted (\u003ccode\u003eDEBUG\u003c/code\u003e, \u003ccode\u003eINFO\u003c/code\u003e, \u003ccode\u003eWARNING\u003c/code\u003e) to control the verbosity and types of logged information, impacting visibility into successful and failed requests.\u003c/p\u003e\n"],["\u003cp\u003eLogs can be directed to a file for persistent storage, and custom logging interceptors can be implemented for tailored logging behavior beyond the library's built-in options.\u003c/p\u003e\n"]]],[],null,["# Enable Logging\n\nThe library provides versatile logging for Google Ads API interactions. You can\ncapture:\n\n- Detailed information: Full requests sent to the API and responses received.\n- Concise summaries: A high-level overview of the interactions.\n\nAnd you can control these logging settings in two ways:\n\n- Client Library Configuration: Use the library's specific [configuration](/google-ads/api/docs/client-libs/python/configuration) options.\n- Programmatically with Python: Use Python's built-in [logging](//docs.python.org/3.7/library/logging.html) framework for more direct control.\n\nLogging is configured automatically when a `GoogleAdsClient` instance is\ninitialized. This initialization step occurs, for example, when you use the\n`load_from_storage` method. At this point, the library will:\n\n- Read your specified logging settings from its [configuration](/google-ads/api/docs/client-libs/python/configuration).\n- Pass these settings to Python's built-in [`logging.config.dictConfig`](//docs.python.org/3.7/library/logging.config.html#logging.config.dictConfig) to activate them.\n\nWhen this library is used as an installed package, you need to integrate its\nlogging with your application's logging setup. Specifically, you must add a\nlogging handler to the library's own logger instance with the\n[`addHandler`](//docs.python.org/3/library/logging.html#logging.Logger.addHandler)\nmethod. This handler will dictate where the library's log messages are directed\n(e.g., to the console, a file, etc.). To do this, first retrieve the library's\nlogger instance as shown here: \n\n import logging\n\n logger = logging.getLogger('google.ads.googleads.client')\n\nAfter retrieving the library's logger, you can tell it where to output log\nmessages.\n\n- Sending Logs to the Console: To display log messages on your console, you\n add a basic handler. Here's how to direct logs to standard output (`stdout`):\n\n And here's how to set a basic handler that will tell the logger to print to\n `stdout`: \n\n import sys\n\n # Assuming 'logger' was retrieved as per previous instructions\n logger.addHandler(logging.StreamHandler(sys.stdout))\n\n If you prefer to send logs to standard error (`stderr`), which is often used\n for error messages and warnings: \n\n import sys\n\n # Assuming 'logger' was retrieved as per previous instructions\n logger.addHandler(logging.StreamHandler(sys.stderr))\n\n- Configuring Other Logging Settings Programmatically: Once you have the\n logger object, you can also programmatically change other logging settings\n using Python's built-in [logging](//docs.python.org/3.7/library/logging.html)\n module. For example, to change the logging level to `DEBUG` (which will show\n more detailed messages):\n\n logger.setLevel(logging.DEBUG)\n\nLog levels\n----------\n\nThe client generates logs at a few different levels and you can set your\nconfiguration to see some or all of the below:\n\n| Level | Successful Request | Failed Request |\n|-----------|--------------------------------------------------------------------|---------------------------------------------------------------------------------------|\n| `DEBUG` | A detailed log with complete request and response objects as JSON. | A detailed log with complete request and exception objects as JSON. |\n| `INFO` | A concise summary with specific request and response fields. | A detailed log with complete request and exception objects as JSON. |\n| `WARNING` | None | A concise summary with specific request information, the exception state and message. |\n\nSince the Python logging framework ignores log messages that are less severe\nthan the configured level, setting to `WARNING` means you will only see\nconcise messages related to failed requests, but setting to `DEBUG` means\nyou will see all possible types of logs in the above table.\n\nLogging to file\n---------------\n\nWhen you run example scripts like\n[`get_campaigns.py`](//github.com/googleads/google-ads-python/blob/main/examples/basic_operations/get_campaigns.py)\nfrom your command line, any log messages typically printed to the console can\nbe redirected (or \"piped\") to a file. This is a feature of your operating\nsystem's shell, not the Python library itself. Here's how:\n\nTo save standard output (most logs) to a file (overwriting it): \n\n python get_campaigns.py -c $CLIENT_ID \u003e campaign_logs.txt\n\nTo append standard output to a file: \n\n python get_campaigns.py -c $CLIENT_ID \u003e\u003e campaign_logs.txt\n\nTo save both standard output and standard error (for errors/warnings) to the\nsame file: \n\n python get_campaigns.py -c $CLIENT_ID \u003e all_logs.txt 2\u003e&1\n\n(Or, on some modern shells like Bash 4+): \n\n python get_campaigns.py -c $CLIENT_ID &\u003e all_logs.txt\n\nLogging interceptors\n--------------------\n\nThe Python client library uses gRPC\n[interceptors](//grpc.io/blog/grpc-web-interceptor) to access and log request\nand response details. You can set up your own custom logging by creating a gRPC\ninterceptor with custom logic. See the [Logging\nguide](/google-ads/api/docs/best-practices/logging#option_4_implement_a_custom_grpc_logging_interceptor)\nfor more details and an example of a custom logging interceptor."]]