Skip to content

Commit e1e714c

Browse files
Helen Koiketheacodes
authored andcommitted
Print to stderr instead of stdout when exiting the program (#5569)
The function print sends the output to stdout disturbing the output of the application. Redirect its output to stderr instead with sys.stderr
1 parent f67a57e commit e1e714c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

logging/google/cloud/logging/handlers/transports/background_thread.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import atexit
2323
import logging
24+
import sys
2425
import threading
2526
import time
2627

@@ -195,7 +196,9 @@ def stop(self, grace_period=None):
195196
self._queue.put_nowait(_WORKER_TERMINATOR)
196197

197198
if grace_period is not None:
198-
print('Waiting up to %d seconds.' % (grace_period,))
199+
print(
200+
'Waiting up to %d seconds.' % (grace_period,),
201+
file=sys.stderr)
199202

200203
self._thread.join(timeout=grace_period)
201204

@@ -216,12 +219,15 @@ def _main_thread_terminated(self):
216219
if not self._queue.empty():
217220
print(
218221
'Program shutting down, attempting to send %d queued log '
219-
'entries to Stackdriver Logging...' % (self._queue.qsize(),))
222+
'entries to Stackdriver Logging...' % (self._queue.qsize(),),
223+
file=sys.stderr)
220224

221225
if self.stop(self._grace_period):
222-
print('Sent all pending logs.')
226+
print('Sent all pending logs.', file=sys.stderr)
223227
else:
224-
print('Failed to send %d pending logs.' % (self._queue.qsize(),))
228+
print(
229+
'Failed to send %d pending logs.' % (self._queue.qsize(),),
230+
file=sys.stderr)
225231

226232
def enqueue(self, record, message, resource=None, labels=None):
227233
"""Queues a log entry to be written by the background thread.

0 commit comments

Comments
 (0)