Skip to content

Commit 1f4e0d3

Browse files
committed
Make "use_logging" parameter global
1 parent 80d4e00 commit 1f4e0d3

File tree

6 files changed

+27
-26
lines changed

6 files changed

+27
-26
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ If you'd like to keep logs, execute `configure_testgres(node_cleanup_full=False)
8282

8383
> Note: context managers (aka `with`) call `stop()` and `cleanup()` automatically.
8484
85-
testgres supports [python logging](https://docs.python.org/3.6/library/logging.html),
85+
`testgres` supports [python logging](https://docs.python.org/3.6/library/logging.html),
8686
which means that you can aggregate logs from several nodes into one file:
8787

8888
```python
@@ -91,13 +91,20 @@ import logging
9191
# write everything to /tmp/testgres.log
9292
logging.basicConfig(filename='/tmp/testgres.log')
9393

94-
# create two different nodes with logging
95-
node1 = testgres.get_new_node(use_logging=True).init().start()
96-
node2 = testgres.get_new_node(use_logging=True).init().start()
94+
# enable logging, and create two different nodes
95+
testgres.configure_testgres(enable_python_logging=True)
96+
node1 = testgres.get_new_node().init().start()
97+
node2 = testgres.get_new_node().init().start()
9798

9899
# execute a few queries
99100
node1.execute('select 1')
100101
node2.execute('select 2')
102+
103+
# disable logging
104+
testgres.configure_testgres(enable_python_logging=False)
105+
106+
Look at `tests/test_simple.py` file for a complete example of the logging
107+
configuration.
101108
```
102109

103110

testgres/api.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@
3636
from .node import PostgresNode
3737

3838

39-
def get_new_node(name=None, base_dir=None, use_logging=False):
39+
def get_new_node(name=None, base_dir=None):
4040
"""
4141
Create a new node (select port automatically).
4242
4343
Args:
4444
name: node's application name.
4545
base_dir: path to node's data directory.
46-
use_logging: enable python logging.
4746
4847
Returns:
4948
An instance of PostgresNode.
5049
"""
5150

52-
return PostgresNode(name=name, base_dir=base_dir, use_logging=use_logging)
51+
return PostgresNode(name=name, base_dir=base_dir)

testgres/backup.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,13 @@ def _prepare_dir(self, destroy):
115115
# Return path to new node
116116
return dest_base_dir
117117

118-
def spawn_primary(self, name=None, destroy=True, use_logging=False):
118+
def spawn_primary(self, name=None, destroy=True):
119119
"""
120120
Create a primary node from a backup.
121121
122122
Args:
123123
name: primary's application name.
124124
destroy: should we convert this backup into a node?
125-
use_logging: enable python logging.
126125
127126
Returns:
128127
New instance of PostgresNode.
@@ -134,8 +133,7 @@ def spawn_primary(self, name=None, destroy=True, use_logging=False):
134133
# Build a new PostgresNode
135134
from .node import PostgresNode
136135
node = PostgresNode(name=name,
137-
base_dir=base_dir,
138-
use_logging=use_logging)
136+
base_dir=base_dir)
139137

140138
# New nodes should always remove dir tree
141139
node._should_rm_dirs = True
@@ -145,23 +143,21 @@ def spawn_primary(self, name=None, destroy=True, use_logging=False):
145143

146144
return node
147145

148-
def spawn_replica(self, name=None, destroy=True, use_logging=False):
146+
def spawn_replica(self, name=None, destroy=True):
149147
"""
150148
Create a replica of the original node from a backup.
151149
152150
Args:
153151
name: replica's application name.
154152
destroy: should we convert this backup into a node?
155-
use_logging: enable python logging.
156153
157154
Returns:
158155
New instance of PostgresNode.
159156
"""
160157

161158
# Build a new PostgresNode
162159
node = self.spawn_primary(name=name,
163-
destroy=destroy,
164-
use_logging=use_logging)
160+
destroy=destroy)
165161

166162
# Assign it a master and a recovery file (private magic)
167163
node._assign_master(self.original_node)

testgres/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class TestgresConfig:
1111
1212
cache_pg_config: shall we cache pg_config results?
1313
14+
use_python_logging: use python logging configuration for all nodes.
1415
error_log_lines: N of log lines to be included into exception (0=inf).
1516
1617
node_cleanup_full: shall we remove EVERYTHING (including logs)?
@@ -23,6 +24,7 @@ class TestgresConfig:
2324

2425
cache_pg_config = True
2526

27+
use_python_logging = False
2628
error_log_lines = 20
2729

2830
node_cleanup_full = True

testgres/node.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,14 @@ def __bool__(self):
7070

7171

7272
class PostgresNode(object):
73-
def __init__(self, name=None, port=None, base_dir=None, use_logging=False):
73+
def __init__(self, name=None, port=None, base_dir=None):
7474
"""
7575
Create a new node manually.
7676
7777
Args:
7878
name: node's application name.
7979
port: port to accept connections.
8080
base_dir: path to node's data directory.
81-
use_logging: enable python logging.
8281
"""
8382

8483
# basic
@@ -94,7 +93,6 @@ def __init__(self, name=None, port=None, base_dir=None, use_logging=False):
9493

9594
# private
9695
self._should_free_port = port is None
97-
self._use_logging = use_logging
9896
self._logger = None
9997
self._master = None
10098

@@ -205,7 +203,7 @@ def _prepare_dirs(self):
205203
os.makedirs(self.logs_dir)
206204

207205
def _maybe_start_logger(self):
208-
if self._use_logging:
206+
if TestgresConfig.use_python_logging:
209207
# spawn new logger if it doesn't exist or is stopped
210208
if not self._logger or not self._logger.is_alive():
211209
self._logger = TestgresLogger(self.name, self.pg_log_name)
@@ -869,24 +867,20 @@ def backup(self, username=None, xlog_method=DEFAULT_XLOG_METHOD):
869867
def replicate(self,
870868
name=None,
871869
username=None,
872-
xlog_method=DEFAULT_XLOG_METHOD,
873-
use_logging=False):
870+
xlog_method=DEFAULT_XLOG_METHOD):
874871
"""
875872
Create a binary replica of this node.
876873
877874
Args:
878875
name: replica's application name.
879876
username: database user name.
880877
xlog_method: a method for collecting the logs ('fetch' | 'stream').
881-
use_logging: enable python logging.
882878
"""
883879

884880
backup = self.backup(username=username, xlog_method=xlog_method)
885881

886882
# transform backup into a replica
887-
return backup.spawn_replica(name=name,
888-
destroy=True,
889-
use_logging=use_logging)
883+
return backup.spawn_replica(name=name, destroy=True)
890884

891885
def catchup(self, dbname=None, username=None):
892886
"""

tests/test_simple.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,10 @@ def test_logging(self):
458458
}
459459

460460
logging.config.dictConfig(log_conf)
461+
configure_testgres(use_python_logging=True)
461462

462463
node_name = 'master'
463-
with get_new_node(name=node_name, use_logging=True) as master:
464+
with get_new_node(name=node_name) as master:
464465
master.init().start()
465466

466467
# execute a dummy query a few times
@@ -482,6 +483,8 @@ def test_logging(self):
482483
master.restart()
483484
self.assertTrue(master._logger.is_alive())
484485

486+
configure_testgres(use_python_logging=False)
487+
485488
@unittest.skipUnless(
486489
util_is_executable("pgbench"), "pgbench may be missing")
487490
def test_pgbench(self):

0 commit comments

Comments
 (0)