{"action":"created","comment":{"url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/comments/35240386","id":35240386,"diff_hunk":"@@ -0,0 +1,69 @@\n+# Copyright 2015 Google Inc. All Rights Reserved.\n+#\n+# Licensed under the Apache License, Version 2.0 (the \"License\");\n+# you may not use this file except in compliance with the License.\n+# You may obtain a copy of the License at\n+#\n+#     http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing, software\n+# distributed under the License is distributed on an \"AS IS\" BASIS,\n+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+# See the License for the specific language governing permissions and\n+# limitations under the License.\n+#\n+import json\n+import logging\n+import os\n+\n+LOG_PATH_TEMPLATE = '/var/log/app_engine/app.{}.json'\n+\n+class CloudLoggingHandler(logging.FileHandler):\n+  \"\"\"\n+  A handler that emits logs to Cloud Logging, wrapped in JSON and with\n+  appropriate metadata. The process of converting the user's formatted logs into\n+  a JSON payload for Cloud Logging consumption is implemented as part of the\n+  handler itself, and not as a formatting step, so as not to interfere with\n+  user-defined logging formats.\n+\n+  The handler will look for HTTP header information in the environment (which\n+  will be present in the GAE 1.0-compatible runtime) and, if it exists, will\n+  parse the X-Cloud-Trace-Context header to add a Trace ID to each log record.\n+\n+  Logging calls can also alternatively 'trace_id' in as a field in the 'extra'\n+  dict, which will be used preferentially to fill out the Trace ID metadata.\n+  \"\"\"\n+\n+  def __init__(self):\n+    # Large log entries will get mangled if multiple workers write to the same\n+    # file simultaneously, so we'll use the worker's PID to pick a log filename.\n+    filename = LOG_PATH_TEMPLATE.format(os.getpid())\n+    super(CloudLoggingHandler, self).__init__(filename, delay=True)\n+\n+  def format(self, record):\n+    \"\"\"\n+    Format the specified record with the default logging.Handler behavior, but\n+    then wrap it in JSON and add metadata for consumption by GCL.\n+    \"\"\"\n+    # First, let's just get the log string as it would normally be formatted.\n+    message = super(CloudLoggingHandler, self).format(record)\n+\n+    # Now assemble a dictionary with the log string as the message.\n+    payload = {'message': message,\n+               'timestamp': {'seconds': int(record.created), \n+                             'nanos': int((record.created - int(record.created)) * 1000000000)}, # pylint: disable=line-too-long\n+               'thread': record.thread,\n+               'severity': record.levelname,}\n+\n+    # Now make a best effort to add the trace id.\n+    # First, try to get the trace id from the 'extras' of the record.\n+    try:\n+      payload['traceId'] = record.trace_id","path":"multicore_runtime/cloud_logging.py","position":61,"original_position":61,"commit_id":"657a92948f3ea2be2fba324870e2e0e6478a8471","original_commit_id":"657a92948f3ea2be2fba324870e2e0e6478a8471","user":{"login":"andrewsg","id":686551,"avatar_url":"https://avatars.githubusercontent.com/u/686551?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewsg","html_url":"https://github.com/andrewsg","followers_url":"https://api.github.com/users/andrewsg/followers","following_url":"https://api.github.com/users/andrewsg/following{/other_user}","gists_url":"https://api.github.com/users/andrewsg/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewsg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewsg/subscriptions","organizations_url":"https://api.github.com/users/andrewsg/orgs","repos_url":"https://api.github.com/users/andrewsg/repos","events_url":"https://api.github.com/users/andrewsg/events{/privacy}","received_events_url":"https://api.github.com/users/andrewsg/received_events","type":"User","site_admin":false},"body":"hasattr violates EAFP style -- try/except has no race condition (not that that's actually a problem as this will execute in a single thread, but at least the style is consistent).\r\n\r\nI'll try an alternative with getattr() + a default and no try/except.","created_at":"2015-07-22T17:36:10Z","updated_at":"2015-07-22T17:36:10Z","html_url":"https://github.com/GoogleCloudPlatform/appengine-python-vm-runtime/pull/18#discussion_r35240386","pull_request_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/18","_links":{"self":{"href":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/comments/35240386"},"html":{"href":"https://github.com/GoogleCloudPlatform/appengine-python-vm-runtime/pull/18#discussion_r35240386"},"pull_request":{"href":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/18"}}},"pull_request":{"url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/18","id":40516060,"html_url":"https://github.com/GoogleCloudPlatform/appengine-python-vm-runtime/pull/18","diff_url":"https://github.com/GoogleCloudPlatform/appengine-python-vm-runtime/pull/18.diff","patch_url":"https://github.com/GoogleCloudPlatform/appengine-python-vm-runtime/pull/18.patch","issue_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/issues/18","number":18,"state":"open","locked":false,"title":"Send structured logs to Cloud Logging","user":{"login":"andrewsg","id":686551,"avatar_url":"https://avatars.githubusercontent.com/u/686551?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewsg","html_url":"https://github.com/andrewsg","followers_url":"https://api.github.com/users/andrewsg/followers","following_url":"https://api.github.com/users/andrewsg/following{/other_user}","gists_url":"https://api.github.com/users/andrewsg/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewsg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewsg/subscriptions","organizations_url":"https://api.github.com/users/andrewsg/orgs","repos_url":"https://api.github.com/users/andrewsg/repos","events_url":"https://api.github.com/users/andrewsg/events{/privacy}","received_events_url":"https://api.github.com/users/andrewsg/received_events","type":"User","site_admin":false},"body":"R: @mingzhaodotname CC: @isdal ","created_at":"2015-07-21T23:24:43Z","updated_at":"2015-07-22T17:36:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4c1151e0eb29d6c9662e311936f6259d390391a4","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/18/commits","review_comments_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/18/comments","review_comment_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/comments{/number}","comments_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/issues/18/comments","statuses_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/statuses/657a92948f3ea2be2fba324870e2e0e6478a8471","head":{"label":"andrewsg:logs","ref":"logs","sha":"657a92948f3ea2be2fba324870e2e0e6478a8471","user":{"login":"andrewsg","id":686551,"avatar_url":"https://avatars.githubusercontent.com/u/686551?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewsg","html_url":"https://github.com/andrewsg","followers_url":"https://api.github.com/users/andrewsg/followers","following_url":"https://api.github.com/users/andrewsg/following{/other_user}","gists_url":"https://api.github.com/users/andrewsg/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewsg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewsg/subscriptions","organizations_url":"https://api.github.com/users/andrewsg/orgs","repos_url":"https://api.github.com/users/andrewsg/repos","events_url":"https://api.github.com/users/andrewsg/events{/privacy}","received_events_url":"https://api.github.com/users/andrewsg/received_events","type":"User","site_admin":false},"repo":{"id":37627647,"name":"appengine-python-vm-runtime","full_name":"andrewsg/appengine-python-vm-runtime","owner":{"login":"andrewsg","id":686551,"avatar_url":"https://avatars.githubusercontent.com/u/686551?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewsg","html_url":"https://github.com/andrewsg","followers_url":"https://api.github.com/users/andrewsg/followers","following_url":"https://api.github.com/users/andrewsg/following{/other_user}","gists_url":"https://api.github.com/users/andrewsg/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewsg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewsg/subscriptions","organizations_url":"https://api.github.com/users/andrewsg/orgs","repos_url":"https://api.github.com/users/andrewsg/repos","events_url":"https://api.github.com/users/andrewsg/events{/privacy}","received_events_url":"https://api.github.com/users/andrewsg/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/andrewsg/appengine-python-vm-runtime","description":"Google Managed VMs Python Runtime","fork":true,"url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime","forks_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/forks","keys_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/keys{/key_id}","collaborators_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/teams","hooks_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/hooks","issue_events_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/issues/events{/number}","events_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/events","assignees_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/assignees{/user}","branches_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/branches{/branch}","tags_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/tags","blobs_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/git/refs{/sha}","trees_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/git/trees{/sha}","statuses_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/statuses/{sha}","languages_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/languages","stargazers_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/stargazers","contributors_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/contributors","subscribers_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/subscribers","subscription_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/subscription","commits_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/commits{/sha}","git_commits_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/git/commits{/sha}","comments_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/comments{/number}","issue_comment_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/issues/comments{/number}","contents_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/contents/{+path}","compare_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/compare/{base}...{head}","merges_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/merges","archive_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/downloads","issues_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/issues{/number}","pulls_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/pulls{/number}","milestones_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/milestones{/number}","notifications_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/labels{/name}","releases_url":"https://api.github.com/repos/andrewsg/appengine-python-vm-runtime/releases{/id}","created_at":"2015-06-18T00:02:48Z","updated_at":"2015-06-18T00:03:17Z","pushed_at":"2015-07-21T23:24:09Z","git_url":"git://github.com/andrewsg/appengine-python-vm-runtime.git","ssh_url":"git@github.com:andrewsg/appengine-python-vm-runtime.git","clone_url":"https://github.com/andrewsg/appengine-python-vm-runtime.git","svn_url":"https://github.com/andrewsg/appengine-python-vm-runtime","homepage":"","size":25741,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"GoogleCloudPlatform:master","ref":"master","sha":"9f43bba75dd9c37801e7081b2a6fc95e6e303dce","user":{"login":"GoogleCloudPlatform","id":2810941,"avatar_url":"https://avatars.githubusercontent.com/u/2810941?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleCloudPlatform","html_url":"https://github.com/GoogleCloudPlatform","followers_url":"https://api.github.com/users/GoogleCloudPlatform/followers","following_url":"https://api.github.com/users/GoogleCloudPlatform/following{/other_user}","gists_url":"https://api.github.com/users/GoogleCloudPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleCloudPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleCloudPlatform/subscriptions","organizations_url":"https://api.github.com/users/GoogleCloudPlatform/orgs","repos_url":"https://api.github.com/users/GoogleCloudPlatform/repos","events_url":"https://api.github.com/users/GoogleCloudPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleCloudPlatform/received_events","type":"Organization","site_admin":false},"repo":{"id":29995176,"name":"appengine-python-vm-runtime","full_name":"GoogleCloudPlatform/appengine-python-vm-runtime","owner":{"login":"GoogleCloudPlatform","id":2810941,"avatar_url":"https://avatars.githubusercontent.com/u/2810941?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleCloudPlatform","html_url":"https://github.com/GoogleCloudPlatform","followers_url":"https://api.github.com/users/GoogleCloudPlatform/followers","following_url":"https://api.github.com/users/GoogleCloudPlatform/following{/other_user}","gists_url":"https://api.github.com/users/GoogleCloudPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleCloudPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleCloudPlatform/subscriptions","organizations_url":"https://api.github.com/users/GoogleCloudPlatform/orgs","repos_url":"https://api.github.com/users/GoogleCloudPlatform/repos","events_url":"https://api.github.com/users/GoogleCloudPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleCloudPlatform/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/GoogleCloudPlatform/appengine-python-vm-runtime","description":"Google Managed VMs Python Runtime","fork":false,"url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime","forks_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/forks","keys_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/keys{/key_id}","collaborators_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/teams","hooks_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/hooks","issue_events_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/issues/events{/number}","events_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/events","assignees_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/assignees{/user}","branches_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/branches{/branch}","tags_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/tags","blobs_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/git/refs{/sha}","trees_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/git/trees{/sha}","statuses_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/statuses/{sha}","languages_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/languages","stargazers_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/stargazers","contributors_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/contributors","subscribers_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/subscribers","subscription_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/subscription","commits_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/commits{/sha}","git_commits_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/git/commits{/sha}","comments_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/comments{/number}","issue_comment_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/issues/comments{/number}","contents_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/contents/{+path}","compare_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/compare/{base}...{head}","merges_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/merges","archive_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/downloads","issues_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/issues{/number}","pulls_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls{/number}","milestones_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/milestones{/number}","notifications_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/labels{/name}","releases_url":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/releases{/id}","created_at":"2015-01-29T00:17:43Z","updated_at":"2015-07-22T03:54:16Z","pushed_at":"2015-07-21T23:24:43Z","git_url":"git://github.com/GoogleCloudPlatform/appengine-python-vm-runtime.git","ssh_url":"git@github.com:GoogleCloudPlatform/appengine-python-vm-runtime.git","clone_url":"https://github.com/GoogleCloudPlatform/appengine-python-vm-runtime.git","svn_url":"https://github.com/GoogleCloudPlatform/appengine-python-vm-runtime","homepage":"","size":25968,"stargazers_count":6,"watchers_count":6,"language":"Python","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":6,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/18"},"html":{"href":"https://github.com/GoogleCloudPlatform/appengine-python-vm-runtime/pull/18"},"issue":{"href":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/issues/18"},"comments":{"href":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/issues/18/comments"},"review_comments":{"href":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/18/comments"},"review_comment":{"href":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/pulls/18/commits"},"statuses":{"href":"https://api.github.com/repos/GoogleCloudPlatform/appengine-python-vm-runtime/statuses/657a92948f3ea2be2fba324870e2e0e6478a8471"}}}}