Skip to content

Commit

Permalink
Merge pull request #1564 from tseaver/logging-logger_class_and_factory
Browse files Browse the repository at this point in the history
Add 'Logger' class and factory.
  • Loading branch information
tseaver committed Mar 3, 2016
2 parents 6074fef + a09662c commit ae49cf7
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@

logging-usage
Client <logging-client>
logging-logger

.. toctree::
:maxdepth: 0
Expand Down
8 changes: 8 additions & 0 deletions docs/logging-logger.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Logger
======

.. automodule:: gcloud.logging.logger
:members:
:undoc-members:
:show-inheritance:

12 changes: 12 additions & 0 deletions gcloud/logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from gcloud.client import JSONClient
from gcloud.logging.connection import Connection
from gcloud.logging.logger import Logger


class Client(JSONClient):
Expand All @@ -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)
43 changes: 43 additions & 0 deletions gcloud/logging/logger.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 14 additions & 3 deletions gcloud/logging/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
50 changes: 50 additions & 0 deletions gcloud/logging/test_logger.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ae49cf7

Please sign in to comment.