Skip to content

Commit b5798fd

Browse files
committed
Add 'Logger' class and factory.
1 parent 44f9558 commit b5798fd

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112

113113
logging-usage
114114
Client <logging-client>
115+
logging-logger
115116

116117
.. toctree::
117118
:maxdepth: 0

docs/logging-logger.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Logger
2+
======
3+
4+
.. automodule:: gcloud.logging.logger
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+

gcloud/logging/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from gcloud.client import JSONClient
1919
from gcloud.logging.connection import Connection
20+
from gcloud.logging.logger import Logger
2021

2122

2223
class Client(JSONClient):
@@ -41,3 +42,14 @@ class Client(JSONClient):
4142
"""
4243

4344
_connection_class = Connection
45+
46+
def logger(self, name):
47+
"""Creates a logger bound to the current client.
48+
49+
:type name: string
50+
:param name: the name of the logger to be constructed.
51+
52+
:rtype: :class:`gcloud.pubsub.logger.Logger`
53+
:returns: Logger created with the current client.
54+
"""
55+
return Logger(name, client=self)

gcloud/logging/logger.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Define API Loggers."""
16+
17+
18+
class Logger(object):
19+
"""Loggers represent named targets for log entries.
20+
21+
See:
22+
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs
23+
24+
:type name: string
25+
:param name: the name of the logger
26+
27+
:type client: :class:`gcloud.logging.client.Client`
28+
:param client: A client which holds credentials and project configuration
29+
for the logger (which requires a project).
30+
"""
31+
def __init__(self, name, client):
32+
self.name = name
33+
self._client = client
34+
35+
@property
36+
def client(self):
37+
"""Clent bound to the logger."""
38+
return self._client
39+
40+
@property
41+
def project(self):
42+
"""Project bound to the logger."""
43+
return self._client.project

gcloud/logging/test_client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
class TestClient(unittest2.TestCase):
1919

20+
PROJECT = 'PROJECT'
21+
LOGGER_NAME = 'LOGGER_NAME'
22+
2023
def _getTargetClass(self):
2124
from gcloud.logging.client import Client
2225
return Client
@@ -25,10 +28,18 @@ def _makeOne(self, *args, **kw):
2528
return self._getTargetClass()(*args, **kw)
2629

2730
def test_ctor(self):
28-
PROJECT = 'PROJECT'
2931
creds = _Credentials()
30-
client = self._makeOne(project=PROJECT, credentials=creds)
31-
self.assertEqual(client.project, PROJECT)
32+
client = self._makeOne(project=self.PROJECT, credentials=creds)
33+
self.assertEqual(client.project, self.PROJECT)
34+
35+
def test_logger(self):
36+
creds = _Credentials()
37+
38+
client_obj = self._makeOne(project=self.PROJECT, credentials=creds)
39+
logger = client_obj.logger(self.LOGGER_NAME)
40+
self.assertEqual(logger.name, self.LOGGER_NAME)
41+
self.assertTrue(logger.client is client_obj)
42+
self.assertEqual(logger.project, self.PROJECT)
3243

3344

3445
class _Credentials(object):

gcloud/logging/test_logger.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest2
16+
17+
18+
class TestLogger(unittest2.TestCase):
19+
20+
PROJECT = 'test-project'
21+
LOGGER_NAME = 'logger-name'
22+
23+
def _getTargetClass(self):
24+
from gcloud.logging.logger import Logger
25+
return Logger
26+
27+
def _makeOne(self, *args, **kw):
28+
return self._getTargetClass()(*args, **kw)
29+
30+
def test_ctor(self):
31+
conn = _Connection()
32+
client = _Client(self.PROJECT, conn)
33+
logger = self._makeOne(self.LOGGER_NAME, client=client)
34+
self.assertEqual(logger.name, self.LOGGER_NAME)
35+
self.assertTrue(logger.client is client)
36+
self.assertEqual(logger.project, self.PROJECT)
37+
38+
39+
class _Connection(object):
40+
41+
def __init__(self, *responses):
42+
self._responses = responses
43+
self._requested = []
44+
45+
46+
class _Client(object):
47+
48+
def __init__(self, project, connection=None):
49+
self.project = project
50+
self.connection = connection

0 commit comments

Comments
 (0)