Skip to content

Commit e270bd8

Browse files
committed
Switched to module-level logging.
- getChild() not supported in 2.6 . - Removed massive amount of commented logging/logic.
1 parent 4555d9f commit e270bd8

File tree

4 files changed

+106
-302
lines changed

4 files changed

+106
-302
lines changed

gdrivefs/cache/cache_agent.py

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
from gdrivefs.cache.cache_registry import CacheRegistry, CacheFault
1111
from gdrivefs.report import Report
1212

13+
_logger = logging.getLogger(__name__)
14+
15+
1316
class CacheAgent(object):
1417
"""A particular namespace within the cache."""
1518

16-
__log = None
17-
1819
registry = None
1920
resource_name = None
2021
max_age = None
@@ -27,9 +28,7 @@ class CacheAgent(object):
2728

2829
def __init__(self, resource_name, max_age, fault_handler=None,
2930
cleanup_pretrigger=None):
30-
self.__log = logging.getLogger().getChild('CacheAgent')
31-
32-
self.__log.debug("CacheAgent(%s,%s,%s,%s)" % (resource_name, max_age,
31+
_logger.debug("CacheAgent(%s,%s,%s,%s)" % (resource_name, max_age,
3332
type(fault_handler),
3433
cleanup_pretrigger))
3534

@@ -55,20 +54,10 @@ def __del__(self):
5554
def __post_status(self):
5655
"""Send the current status to our reporting tool."""
5756

58-
try:
59-
num_values = self.registry.count(self.resource_name)
60-
except:
61-
self.__log.exception("Could not get count of values for resource "
62-
"with name [%s]." % (self.resource_name))
63-
raise
57+
num_values = self.registry.count(self.resource_name)
6458

65-
try:
66-
self.report.set_values(self.report_source_name, 'count',
67-
num_values)
68-
except:
69-
self.__log.exception("Cache could not post status for resource "
70-
"with name [%s]." % (self.resource_name))
71-
raise
59+
self.report.set_values(self.report_source_name, 'count',
60+
num_values)
7261

7362
status_post_interval_s = Conf.get('cache_status_post_frequency_s')
7463
status_timer = Timer(status_post_interval_s, self.__post_status)
@@ -80,15 +69,10 @@ def __cleanup_check(self):
8069
removed.
8170
"""
8271

83-
self.__log.debug("Doing clean-up for cache resource with name [%s]." %
72+
_logger.debug("Doing clean-up for cache resource with name [%s]." %
8473
(self.resource_name))
8574

86-
try:
87-
cache_dict = self.registry.list_raw(self.resource_name)
88-
except:
89-
self.__log.exception("Could not do clean-up check with resource-"
90-
"name [%s]." % (self.resource_name))
91-
raise
75+
cache_dict = self.registry.list_raw(self.resource_name)
9276

9377
total_keys = [ (key, value_tuple[1]) for key, value_tuple \
9478
in cache_dict.iteritems() ]
@@ -98,41 +82,35 @@ def __cleanup_check(self):
9882
if (datetime.now() - value_tuple[1]).seconds > \
9983
self.max_age ]
10084

101-
self.__log.info("Found (%d) entries to clean-up from entry-cache." %
102-
(len(cleanup_keys)))
85+
_logger.info("Found (%d) entries to clean-up from entry-cache." %
86+
(len(cleanup_keys)))
10387

10488
if cleanup_keys:
10589
for key in cleanup_keys:
106-
self.__log.debug("Cache entry [%s] under resource-name [%s] "
107-
"will be cleaned-up." % (key,
108-
self.resource_name))
90+
_logger.debug("Cache entry [%s] under resource-name [%s] "
91+
"will be cleaned-up." %
92+
(key, self.resource_name))
10993

11094
if self.exists(key, no_fault_check=True) == False:
111-
self.__log.debug("Entry with ID [%s] has already been "
112-
"cleaned-up." % (key))
95+
_logger.debug("Entry with ID [%s] has already been "
96+
"cleaned-up." % (key))
11397
else:
114-
try:
115-
self.remove(key)
116-
except:
117-
self.__log.exception("Cache entry [%s] under resource-"
118-
"name [%s] could not be cleaned-"
119-
"up." % (key, self.resource_name))
120-
raise
98+
self.remove(key)
12199

122-
self.__log.debug("Scheduled clean-up complete.")
100+
_logger.debug("Scheduled clean-up complete.")
123101

124102
cleanup_interval_s = Conf.get('cache_cleanup_check_frequency_s')
125103
cleanup_timer = Timer(cleanup_interval_s, self.__cleanup_check)
126104

127105
Timers.get_instance().register_timer('cleanup', cleanup_timer)
128106

129107
def set(self, key, value):
130-
self.__log.debug("CacheAgent.set(%s,%s)" % (key, value))
108+
_logger.debug("CacheAgent.set(%s,%s)" % (key, value))
131109

132110
return self.registry.set(self.resource_name, key, value)
133111

134112
def remove(self, key):
135-
self.__log.debug("CacheAgent.remove(%s)" % (key))
113+
_logger.debug("CacheAgent.remove(%s)" % (key))
136114

137115
return self.registry.remove(self.resource_name,
138116
key,
@@ -143,34 +121,28 @@ def get(self, key, handle_fault = None):
143121
if handle_fault == None:
144122
handle_fault = True
145123

146-
self.__log.debug("CacheAgent.get(%s)" % (key))
124+
_logger.debug("CacheAgent.get(%s)" % (key))
147125

148126
try:
149127
result = self.registry.get(self.resource_name,
150128
key,
151129
max_age=self.max_age,
152130
cleanup_pretrigger=self.cleanup_pretrigger)
153131
except CacheFault:
154-
self.__log.debug("There was a cache-miss while requesting item "
155-
"with ID (key).")
132+
_logger.debug("There was a cache-miss while requesting item with "
133+
"ID (key).")
156134

157135
if self.fault_handler == None or not handle_fault:
158136
raise
159137

160-
try:
161-
result = self.fault_handler(self.resource_name, key)
162-
except:
163-
self.__log.exception("There was an exception in the fault-"
164-
"handler, handling for key [%s].", key)
165-
raise
166-
167-
if result == None:
138+
result = self.fault_handler(self.resource_name, key)
139+
if result is None:
168140
raise
169141

170142
return result
171143

172144
def exists(self, key, no_fault_check=False):
173-
self.__log.debug("CacheAgent.exists(%s)" % (key))
145+
_logger.debug("CacheAgent.exists(%s)" % (key))
174146

175147
return self.registry.exists(self.resource_name, key,
176148
max_age=self.max_age,

gdrivefs/cache/cache_registry.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from threading import RLock
44
from datetime import datetime
55

6+
_logger = logging.getLogger(__name__)
7+
68

79
class CacheFault(Exception):
810
pass
@@ -12,10 +14,8 @@ class CacheRegistry(object):
1214
"""The main cache container."""
1315

1416
__rlock = RLock()
15-
__log = None
1617

1718
def __init__(self):
18-
self.__log = logging.getLogger().getChild('CacheReg')
1919
self.__cache = { }
2020

2121
@staticmethod
@@ -34,8 +34,8 @@ def get_instance(resource_name):
3434

3535
def set(self, resource_name, key, value):
3636

37-
self.__log.debug("CacheRegistry.set(%s,%s,%s)" % (resource_name, key,
38-
value))
37+
_logger.debug("CacheRegistry.set(%s,%s,%s)" %
38+
(resource_name, key, value))
3939

4040
with CacheRegistry.__rlock:
4141
try:
@@ -49,8 +49,8 @@ def set(self, resource_name, key, value):
4949

5050
def remove(self, resource_name, key, cleanup_pretrigger=None):
5151

52-
self.__log.debug("CacheRegistry.remove(%s,%s,%s)" %
53-
(resource_name, key, type(cleanup_pretrigger)))
52+
_logger.debug("CacheRegistry.remove(%s,%s,%s)" %
53+
(resource_name, key, type(cleanup_pretrigger)))
5454

5555
with CacheRegistry.__rlock:
5656
try:
@@ -69,8 +69,8 @@ def get(self, resource_name, key, max_age, cleanup_pretrigger=None):
6969
if cleanup_pretrigger == None
7070
else '<given>')
7171

72-
self.__log.debug("CacheRegistry.get(%s,%s,%s,%s)" %
73-
(resource_name, key, max_age, trigger_given_phrase))
72+
_logger.debug("CacheRegistry.get(%s,%s,%s,%s)" %
73+
(resource_name, key, max_age, trigger_given_phrase))
7474

7575
with CacheRegistry.__rlock:
7676
try:
@@ -88,30 +88,24 @@ def get(self, resource_name, key, max_age, cleanup_pretrigger=None):
8888

8989
def list_raw(self, resource_name):
9090

91-
self.__log.debug("CacheRegistry.list(%s)" % (resource_name))
91+
_logger.debug("CacheRegistry.list(%s)" % (resource_name))
9292

9393
with CacheRegistry.__rlock:
94-
try:
95-
return self.__cache[resource_name]
96-
except:
97-
self.__log.exception("Could not list raw-entries under cache "
98-
"labelled with resource-name [%s]." %
99-
(resource_name))
100-
raise
94+
return self.__cache[resource_name]
10195

10296
def exists(self, resource_name, key, max_age, cleanup_pretrigger=None,
10397
no_fault_check=False):
10498

105-
self.__log.debug("CacheRegistry.exists(%s,%s,%s,%s)" % (resource_name,
106-
key, max_age, cleanup_pretrigger))
99+
_logger.debug("CacheRegistry.exists(%s,%s,%s,%s)" %
100+
(resource_name, key, max_age, cleanup_pretrigger))
107101

108102
with CacheRegistry.__rlock:
109103
try:
110104
(value, timestamp) = self.__cache[resource_name][key]
111105
except:
112106
return False
113107

114-
if max_age != None and not no_fault_check and \
108+
if max_age is not None and not no_fault_check and \
115109
(datetime.now() - timestamp).seconds > max_age:
116110
self.__cleanup_entry(resource_name, key, False,
117111
cleanup_pretrigger=cleanup_pretrigger)
@@ -126,23 +120,13 @@ def count(self, resource_name):
126120
def __cleanup_entry(self, resource_name, key, force,
127121
cleanup_pretrigger=None):
128122

129-
self.__log.debug("Doing clean-up for resource_name [%s] and key "
130-
"[%s]." % (resource_name, key))
123+
_logger.debug("Doing clean-up for resource_name [%s] and key "
124+
"[%s]." % (resource_name, key))
131125

132-
if cleanup_pretrigger != None:
133-
self.__log.debug("Running pre-cleanup trigger for resource_name "
134-
"[%s] and key [%s]." % (resource_name, key))
135-
136-
try:
137-
cleanup_pretrigger(resource_name, key, force)
138-
except:
139-
self.__log.exception("Cleanup-trigger failed.")
140-
raise
126+
if cleanup_pretrigger is not None:
127+
_logger.debug("Running pre-cleanup trigger for resource_name "
128+
"[%s] and key [%s]." % (resource_name, key))
141129

142-
try:
143-
del self.__cache[resource_name][key]
144-
except:
145-
self.__log.exception("Could not clean-up entry with resource_name "
146-
"[%s] and key [%s]." % (resource_name, key))
147-
raise
130+
cleanup_pretrigger(resource_name, key, force)
148131

132+
del self.__cache[resource_name][key]

gdrivefs/cache/cacheclient_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
from gdrivefs.cache.cache_agent import CacheAgent
44

5+
_logger = logging.getLogger(__name__)
6+
7+
58
class CacheClientBase(object):
69
"""Meant to be inherited by a class. Is used to configure a particular
710
namespace within the cache.
811
"""
912

10-
__log = None
11-
1213
@property
1314
def cache(self):
1415
try:
@@ -23,11 +24,10 @@ def cache(self):
2324
return self._cache
2425

2526
def __init__(self):
26-
self.__log = logging.getLogger().getChild('CacheClientBase')
2727
child_type = self.__class__.__bases__[0].__name__
2828
max_age = self.get_max_cache_age_seconds()
2929

30-
self.__log.debug("CacheClientBase(%s,%s)" % (child_type, max_age))
30+
_logger.debug("CacheClientBase(%s,%s)" % (child_type, max_age))
3131

3232
self.child_type = child_type
3333
self.max_age = max_age

0 commit comments

Comments
 (0)