diff --git a/docs/index.rst b/docs/index.rst index f330bf4f9c31..5dd66181f949 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -112,6 +112,7 @@ logging-usage Client + logging-logger .. toctree:: :maxdepth: 0 diff --git a/docs/logging-logger.rst b/docs/logging-logger.rst new file mode 100644 index 000000000000..8deb9b434534 --- /dev/null +++ b/docs/logging-logger.rst @@ -0,0 +1,8 @@ +Logger +====== + +.. automodule:: gcloud.logging.logger + :members: + :undoc-members: + :show-inheritance: + diff --git a/gcloud/logging/client.py b/gcloud/logging/client.py index beaf9ed2217e..4499fcdf9c4e 100644 --- a/gcloud/logging/client.py +++ b/gcloud/logging/client.py @@ -17,6 +17,7 @@ from gcloud.client import JSONClient from gcloud.logging.connection import Connection +from gcloud.logging.logger import Logger class Client(JSONClient): @@ -41,3 +42,14 @@ class Client(JSONClient): """ _connection_class = Connection + + def logger(self, name): + """Creates a logger bound to the current client. + + :type name: string + :param name: the name of the logger to be constructed. + + :rtype: :class:`gcloud.pubsub.logger.Logger` + :returns: Logger created with the current client. + """ + return Logger(name, client=self) diff --git a/gcloud/logging/logger.py b/gcloud/logging/logger.py new file mode 100644 index 000000000000..9d76fa6ac5e7 --- /dev/null +++ b/gcloud/logging/logger.py @@ -0,0 +1,43 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Define API Loggers.""" + + +class Logger(object): + """Loggers represent named targets for log entries. + + See: + https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs + + :type name: string + :param name: the name of the logger + + :type client: :class:`gcloud.logging.client.Client` + :param client: A client which holds credentials and project configuration + for the logger (which requires a project). + """ + def __init__(self, name, client): + self.name = name + self._client = client + + @property + def client(self): + """Clent bound to the logger.""" + return self._client + + @property + def project(self): + """Project bound to the logger.""" + return self._client.project diff --git a/gcloud/logging/test_client.py b/gcloud/logging/test_client.py index d5c17fcfa207..707b3a381209 100644 --- a/gcloud/logging/test_client.py +++ b/gcloud/logging/test_client.py @@ -17,6 +17,9 @@ class TestClient(unittest2.TestCase): + PROJECT = 'PROJECT' + LOGGER_NAME = 'LOGGER_NAME' + def _getTargetClass(self): from gcloud.logging.client import Client return Client @@ -25,10 +28,18 @@ def _makeOne(self, *args, **kw): return self._getTargetClass()(*args, **kw) def test_ctor(self): - PROJECT = 'PROJECT' creds = _Credentials() - client = self._makeOne(project=PROJECT, credentials=creds) - self.assertEqual(client.project, PROJECT) + client = self._makeOne(project=self.PROJECT, credentials=creds) + self.assertEqual(client.project, self.PROJECT) + + def test_logger(self): + creds = _Credentials() + + client_obj = self._makeOne(project=self.PROJECT, credentials=creds) + logger = client_obj.logger(self.LOGGER_NAME) + self.assertEqual(logger.name, self.LOGGER_NAME) + self.assertTrue(logger.client is client_obj) + self.assertEqual(logger.project, self.PROJECT) class _Credentials(object): diff --git a/gcloud/logging/test_logger.py b/gcloud/logging/test_logger.py new file mode 100644 index 000000000000..e3d8107d882c --- /dev/null +++ b/gcloud/logging/test_logger.py @@ -0,0 +1,50 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest2 + + +class TestLogger(unittest2.TestCase): + + PROJECT = 'test-project' + LOGGER_NAME = 'logger-name' + + def _getTargetClass(self): + from gcloud.logging.logger import Logger + return Logger + + def _makeOne(self, *args, **kw): + return self._getTargetClass()(*args, **kw) + + def test_ctor(self): + conn = _Connection() + client = _Client(self.PROJECT, conn) + logger = self._makeOne(self.LOGGER_NAME, client=client) + self.assertEqual(logger.name, self.LOGGER_NAME) + self.assertTrue(logger.client is client) + self.assertEqual(logger.project, self.PROJECT) + + +class _Connection(object): + + def __init__(self, *responses): + self._responses = responses + self._requested = [] + + +class _Client(object): + + def __init__(self, project, connection=None): + self.project = project + self.connection = connection