Skip to content

Commit 197ce68

Browse files
authored
fix: stop library from overriding application logging config (#81)
* fix: stop library from overriding application logging config Remove dictConfig() call from Graph.__init__ that was replacing the host application's entire logging setup. Follow Python best practice for library logging: add NullHandler to top-level 'cog' logger and let the application control its own configuration. - Remove logging_config dict from config.py - Remove dictConfig call and import from torque.py - Add NullHandler in cog/__init__.py - Namespace all loggers under 'cog.' (e.g. cog.torque, cog.database) - Clean up dictConfig references in test files * using __name__ for logger where possible
1 parent 0b1a361 commit 197ce68

15 files changed

Lines changed: 11 additions & 57 deletions

cog/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
import logging
2+
logging.getLogger("cog").addHandler(logging.NullHandler())
3+
14
def cog():
25
return "Cog is alive."

cog/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class Cache:
55

66
def __init__(self, cache_id, shared_cache=None):
7-
self.logger = logging.getLogger('table')
7+
self.logger = logging.getLogger(__name__)
88
self.cache_id = cache_id
99
if shared_cache is not None:
1010
if cache_id not in shared_cache:

cog/config.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,3 @@ def index_id(index_name):
7171
def cog_store(db_name, table_name, instance_id):
7272
return "/".join(cog_context()[0:-2]+[db_name, table_name+STORE+instance_id])
7373

74-
75-
import logging
76-
77-
logging_config = dict(
78-
version = 1,
79-
formatters = {
80-
'f': {'format':
81-
'%(asctime)s %(name)-12s %(levelname)-8s [%(filename)s:%(lineno)s - %(funcName)10s()] %(message)s'}
82-
},
83-
handlers = {
84-
'h': {'class': 'logging.StreamHandler',
85-
'formatter': 'f'
86-
}
87-
},
88-
root = {
89-
'handlers': ['h'],
90-
'level': logging.WARN,
91-
}
92-
)

cog/core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Table:
2828

2929
def __init__(self, name, namespace, db_instance_id, config, column_mode=False, shared_cache=None,
3030
flush_interval=1):
31-
self.logger = logging.getLogger('table')
31+
self.logger = logging.getLogger('cog.table')
3232
self.config = config
3333
self.shared_cache = shared_cache
3434
self.flush_interval = flush_interval
@@ -179,7 +179,7 @@ def load_from_store(cls, position: int, store):
179179
class Index:
180180

181181
def __init__(self, table_meta, config, logger, index_id=0):
182-
self.logger = logging.getLogger('index')
182+
self.logger = logging.getLogger('cog.index')
183183
self.table = table_meta
184184
self.config = config
185185
self.name = self.config.cog_index(table_meta.namespace, table_meta.name, table_meta.db_instance_id, index_id)
@@ -436,7 +436,7 @@ def __init__(self, tablemeta, config, logger, caching_enabled=True, shared_cache
436436
flush_interval=1):
437437
self.caching_enabled = caching_enabled
438438
self.batch_mode = False # When True, defers flush() until end_batch()
439-
self.logger = logging.getLogger('store')
439+
self.logger = logging.getLogger('cog.store')
440440
self.tablemeta = tablemeta
441441
self.config = config
442442
self.flush_interval = flush_interval
@@ -689,7 +689,7 @@ class Indexer:
689689
def __init__(self, tablemeta, config, logger):
690690
self.tablemeta = tablemeta
691691
self.config = config
692-
self.logger = logging.getLogger('indexer')
692+
self.logger = logging.getLogger('cog.indexer')
693693
self.index_list = [] # future range index.
694694
self.index_id = 0
695695
self.load_indexes()

cog/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Cog:
7575
"""
7676

7777
def __init__(self, shared_cache=None, flush_interval=1):
78-
self.logger = logging.getLogger('database')
78+
self.logger = logging.getLogger(__name__)
7979
self.config = config
8080
self.flush_interval = flush_interval
8181
self.logger.info(f"Cog init (flush_interval={flush_interval})")

cog/torque.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from cog.core import cog_hash, Record
44
import json
55
import logging
6-
from logging.config import dictConfig
76
from . import config as cfg
87
from cog.view import graph_template, script_part1, script_part2, graph_lib_src
98
from cog.embedding_providers import EMBEDDING_PROVIDERS, _chunked
@@ -109,8 +108,7 @@ def __init__(self, graph_name, cog_home="cog_home", cog_path_prefix=None, enable
109108
else:
110109
self.cache = None
111110

112-
dictConfig(self.config.logging_config)
113-
self.logger = logging.getLogger("torque")
111+
self.logger = logging.getLogger(__name__)
114112

115113
self.logger.debug(f"Torque init on graph: {graph_name} (flush_interval={flush_interval})")
116114

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='cogdb',
5-
version='3.7.2',
5+
version='3.7.3',
66
description='Persistent Embedded Graph Database',
77
url='http://github.com/arun1729/cog',
88
author='Arun Mahendra',

test/bench.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import logging
55
import os
66
import shutil
7-
from logging.config import dictConfig
87
import string
98
import random
109
import timeit
@@ -38,7 +37,6 @@ def setUpClass(cls):
3837

3938
def test_indexer(self):
4039

41-
dictConfig(config.logging_config)
4240

4341
logger = logging.getLogger()
4442
config.INDEX_CAPACITY = 10003

test/test_batch_mode.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import timeit
88
import unittest
99
import logging
10-
from logging.config import dictConfig
1110

1211
from cog import config
1312
from cog.core import Table, Record
@@ -35,7 +34,6 @@ def setUpClass(cls):
3534
shutil.rmtree(base_path)
3635
os.makedirs(base_path)
3736
config.CUSTOM_COG_DB_PATH = "/tmp/" + DIR_NAME
38-
dictConfig(config.logging_config)
3937

4038
def test_batch_mode_saves_correctly(self):
4139
"""Test that batch mode saves records correctly"""
@@ -156,7 +154,6 @@ class TestCogBatchMode(unittest.TestCase):
156154
@classmethod
157155
def setUpClass(cls):
158156
config.CUSTOM_COG_DB_PATH = "/tmp/" + DIR_NAME
159-
dictConfig(config.logging_config)
160157

161158
def test_cog_batch_mode(self):
162159
"""Test batch mode at Cog level"""
@@ -186,7 +183,6 @@ class TestGraphPutBatch(unittest.TestCase):
186183
@classmethod
187184
def setUpClass(cls):
188185
config.CUSTOM_COG_DB_PATH = "/tmp/" + DIR_NAME
189-
dictConfig(config.logging_config)
190186

191187
def test_put_batch_correctness(self):
192188
"""Test that put_batch inserts all triples correctly"""
@@ -302,7 +298,6 @@ class TestBatchModeIntegration(unittest.TestCase):
302298
@classmethod
303299
def setUpClass(cls):
304300
config.CUSTOM_COG_DB_PATH = "/tmp/" + DIR_NAME
305-
dictConfig(config.logging_config)
306301

307302
def test_large_graph_insertion(self):
308303
"""Test inserting a large graph using batch mode"""

test/test_core.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from cog import config
33
import logging
44
import os
5-
from logging.config import dictConfig
65
import shutil
76

87
import unittest
@@ -58,7 +57,6 @@ def test_record_list(self):
5857
self.assertEqual(record.value_link, unmarshalled_record.value_link)
5958

6059
def test_put_get_string(self):
61-
dictConfig(config.logging_config)
6260
logger = logging.getLogger()
6361

6462
expected_data = Record("rocket", "gemini-titan")
@@ -82,7 +80,6 @@ def test_put_get_string(self):
8280
store.close()
8381

8482
def test_put_get_multiple_string(self):
85-
dictConfig(config.logging_config)
8683
logger = logging.getLogger()
8784

8885
expected_data_list= [Record("rocket", "gemini-titan"), Record("rocket2", "saturn V"), Record("rocket0", "V2")]
@@ -102,7 +99,6 @@ def test_put_get_multiple_string(self):
10299
store.close()
103100

104101
def test_put_get_record_update(self):
105-
dictConfig(config.logging_config)
106102
logger = logging.getLogger()
107103

108104
expected_data_list= [Record("rocket", "gemini-titan"), Record("rocket", "saturn V"), Record("rocket", "V2")]
@@ -126,7 +122,6 @@ def test_put_get_record_update(self):
126122

127123
def test_collision(self):
128124
orig_conf = config.INDEX_CAPACITY
129-
dictConfig(config.logging_config)
130125
logger = logging.getLogger()
131126

132127
expected_data_list= [Record("rocket", "gemini-titan"), Record("rocket2", "saturn V"), Record("rocket0", "V2")]
@@ -151,7 +146,6 @@ def test_collision(self):
151146

152147
def test_collision_large(self):
153148
orig_conf = config.INDEX_CAPACITY
154-
dictConfig(config.logging_config)
155149
logger = logging.getLogger()
156150

157151
table = Table("testdb", "test_table", "test_xcvzdfsadx", config, logger)
@@ -179,7 +173,6 @@ def test_collision_large(self):
179173
config.INDEX_CAPACITY = orig_conf
180174

181175
def test_put_get_list(self):
182-
dictConfig(config.logging_config)
183176
logger = logging.getLogger()
184177

185178
fruits = (["apple", "orange", "banana", "pears", "cherry", "mango"])
@@ -211,7 +204,6 @@ def test_put_get_list(self):
211204
store.close()
212205

213206
def test_delete_list(self):
214-
dictConfig(config.logging_config)
215207
logger = logging.getLogger()
216208

217209
fruits = (["apple", "orange", "banana", "pears", "cherry", "mango"])
@@ -246,7 +238,6 @@ def test_delete_list(self):
246238

247239
def test_delete(self):
248240

249-
dictConfig(config.logging_config)
250241
logger = logging.getLogger()
251242

252243
expected_data = Record("new super data","super new old stuff")
@@ -280,7 +271,6 @@ def test_delete(self):
280271

281272
def test_indexer(self):
282273

283-
dictConfig(config.logging_config)
284274
logger = logging.getLogger()
285275

286276
expected_data = Record("new super data","super new old stuff")

0 commit comments

Comments
 (0)