تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
توفّر المكتبة تسجيلًا متعدد الاستخدامات للتفاعلات مع Google Ads API. يمكنك تسجيل ما يلي:
معلومات تفصيلية: الطلبات الكاملة المُرسَلة إلى واجهة برمجة التطبيقات والردود التي تم تلقّيها
ملخّصات موجزة: نظرة عامة شاملة على التفاعلات
ويمكنك التحكّم في إعدادات التسجيل هذه بطريقتَين:
إعدادات مكتبة البرامج: استخدِم خيارات الإعدادات الخاصة بالمكتبة.
برمجيًا باستخدام Python: استخدِم إطار عمل تسجيل البيانات المضمّن في Python للتحكّم بشكل مباشر أكثر.
يتم ضبط إعدادات التسجيل تلقائيًا عند تهيئة مثيل 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))
ضبط إعدادات التسجيل الأخرى آليًا: بعد الحصول على عنصر المسجّل، يمكنك أيضًا تغيير إعدادات التسجيل الأخرى آليًا باستخدام وحدة التسجيل المضمّنة في Python. على سبيل المثال، لتغيير مستوى التسجيل إلى DEBUG (الذي سيعرض رسائل أكثر تفصيلاً):
logger.setLevel(logging.DEBUG)
مستويات السجلّ
ينشئ العميل سجلّات على مستويات مختلفة، ويمكنك ضبط الإعدادات لعرض بعض أو كل ما يلي:
المستوى
طلب ناجح
تعذّر تنفيذ الطلب
DEBUG
سجلّ تفصيلي يتضمّن عناصر الطلب والاستجابة الكاملة بتنسيق JSON
سجلّ تفصيلي يتضمّن طلبات كاملة وكائنات استثناء بتنسيق JSON
INFO
ملخّص موجز يتضمّن حقول الطلب والاستجابة المحدّدة
سجلّ تفصيلي يتضمّن طلبات كاملة وكائنات استثناء بتنسيق JSON
WARNING
بدون
ملخّص موجز يتضمّن معلومات الطلب المحدّد وحالة الاستثناء ورسالته
بما أنّ إطار تسجيل البيانات في Python يتجاهل رسائل السجلّ الأقل خطورة من المستوى الذي تم ضبطه، يعني الضبط على WARNING أنّك سترى فقط رسائل موجزة تتعلّق بالطلبات التي تعذّر تنفيذها، ولكن الضبط على DEBUG يعني أنّك سترى جميع أنواع السجلّات الممكنة في الجدول أعلاه.
تسجيل البيانات في ملف
عند تشغيل نصوص برمجية نموذجية مثل
get_campaigns.py
من سطر الأوامر، يمكن إعادة توجيه أي رسائل سجلّ يتم عرضها عادةً في وحدة التحكّم (أو "نقلها") إلى ملف. هذه ميزة في واجهة سطر الأوامر لنظام التشغيل، وليست في مكتبة Python نفسها. يُرجى اتّباع الخطوات التالية:
لحفظ الناتج العادي (معظم السجلات) في ملف (مع استبداله):
تستخدم مكتبة عميل Python معترِضات gRPC للوصول إلى تفاصيل الطلبات والردود وتسجيلها. يمكنك إعداد تسجيل مخصّص من خلال إنشاء أداة اعتراض gRPC تتضمّن منطقًا مخصّصًا. راجِع دليل تسجيل البيانات لمعرفة المزيد من التفاصيل ومثال على أداة اعتراض مخصّصة لتسجيل البيانات.
تاريخ التعديل الأخير: 2025-09-05 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-05 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\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."]]