Skip to content

Commit a4709c2

Browse files
author
dvonthenen
committed
Implement Logging Levels for Supportability
1 parent 80b878d commit a4709c2

File tree

16 files changed

+1134
-479
lines changed

16 files changed

+1134
-479
lines changed

deepgram/audio/microphone/microphone.py

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import threading
88
import pyaudio
99
from array import array
10-
from sys import byteorder
10+
import logging, verboselogs
1111

1212
from .errors import DeepgramMicrophoneError
1313

1414
FORMAT = pyaudio.paInt16
1515
CHANNELS = 1
1616
RATE = 16000
17-
CHUNK = 8000
17+
CHUNK = 8194
1818

1919

2020
class Microphone:
@@ -23,8 +23,18 @@ class Microphone:
2323
"""
2424

2525
def __init__(
26-
self, push_callback, format=FORMAT, rate=RATE, chunk=CHUNK, channels=CHANNELS
26+
self,
27+
push_callback,
28+
verbose=logging.WARNING,
29+
format=FORMAT,
30+
rate=RATE,
31+
chunk=CHUNK,
32+
channels=CHANNELS,
2733
):
34+
self.logger = logging.getLogger(__name__)
35+
self.logger.addHandler(logging.StreamHandler())
36+
self.logger.setLevel(verbose)
37+
2838
self.audio = pyaudio.PyAudio()
2939
self.chunk = chunk
3040
self.rate = rate
@@ -34,20 +44,36 @@ def __init__(
3444
self.stream = None
3545

3646
def is_active(self):
47+
self.logger.debug("Microphone.is_active ENTER")
3748
if self.stream is None:
49+
self.logger.error("stream is None")
50+
self.logger.debug("Microphone.is_active LEAVE")
3851
return False
39-
return self.stream.is_active()
52+
53+
val = self.stream.is_active()
54+
self.logger.info("is_active: %s", val)
55+
self.logger.debug("Microphone.is_active LEAVE")
56+
return
4057

4158
def start(self):
59+
self.logger.debug("Microphone.start ENTER")
60+
4261
if self.stream is not None:
62+
self.logger.error("stream is None")
63+
self.logger.debug("Microphone.start LEAVE")
4364
raise DeepgramMicrophoneError("Microphone already started")
4465

66+
self.logger.info("format: %s", self.format)
67+
self.logger.info("channels: %d", self.channels)
68+
self.logger.info("rate: %d", self.rate)
69+
self.logger.info("chunk: %d", self.chunk)
70+
4571
self.stream = self.audio.open(
4672
format=self.format,
4773
channels=self.channels,
4874
rate=self.rate,
4975
input=True,
50-
frames_per_buffer=CHUNK,
76+
frames_per_buffer=self.chunk,
5177
)
5278

5379
self.exit = False
@@ -57,7 +83,12 @@ def start(self):
5783
self.thread = threading.Thread(target=self.processing)
5884
self.thread.start()
5985

86+
self.logger.notice("start succeeded")
87+
self.logger.debug("Microphone.start LEAVE")
88+
6089
def processing(self):
90+
self.logger.debug("Microphone.processing ENTER")
91+
6192
try:
6293
while True:
6394
data = self.stream.read(self.chunk)
@@ -66,27 +97,45 @@ def processing(self):
6697
localExit = self.exit
6798
self.lock.release()
6899
if localExit:
100+
self.logger.info("exit is True")
69101
break
70102
if data is None:
103+
self.logger.info("data is None")
71104
continue
72105

73106
if inspect.iscoroutinefunction(self.push_callback):
107+
self.logger.verbose("async/await callback")
74108
asyncio.run(self.push_callback(data))
75109
else:
110+
self.logger.verbose("regular threaded callback")
76111
self.push_callback(data)
77112

113+
self.logger.notice("processing exiting...")
114+
self.logger.debug("Microphone.processing LEAVE")
115+
78116
except Exception as e:
79-
print(f"Error while sending: {str(e)}")
117+
self.logger.error("Error while sending: %s", str(e))
118+
self.logger.debug("Microphone.processing LEAVE")
80119
raise
81120

82121
def finish(self):
122+
self.logger.debug("Microphone.finish ENTER")
123+
83124
self.lock.acquire()
125+
self.logger.notice("signal exit")
84126
self.exit = True
85127
self.lock.release()
86128

87-
self.thread.join()
88-
self.thread = None
129+
if self.thread is not None:
130+
self.thread.join()
131+
self.thread = None
132+
self.logger.notice("processing/send thread joined")
89133

90-
self.stream.stop_stream()
91-
self.stream.close()
92-
self.stream = None
134+
if self.stream is not None:
135+
self.stream.stop_stream()
136+
self.stream.close()
137+
self.stream = None
138+
self.logger.notice("stream/recv thread joined")
139+
140+
self.logger.notice("finish succeeded")
141+
self.logger.debug("Microphone.finish LEAVE")

deepgram/client.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from typing import Optional
66
from importlib import import_module
7+
import logging, verboselogs
78

8-
from .clients.listen import ListenClient
9+
from .clients.listen import ListenClient, PreRecordedClient
910
from .clients.manage.client import ManageClient
1011
from .clients.onprem.client import OnPremClient
1112

@@ -30,10 +31,13 @@ class DeepgramClient:
3031
listen: Returns a ListenClient instance for interacting with Deepgram's transcription services.
3132
manage: Returns a ManageClient instance for managing Deepgram resources.
3233
onprem: Returns an OnPremClient instance for interacting with Deepgram's on-premises API.
33-
3434
"""
3535

3636
def __init__(self, api_key: str, config: Optional[DeepgramClientOptions] = None):
37+
verboselogs.install()
38+
self.logger = logging.getLogger(__name__)
39+
self.logger.addHandler(logging.StreamHandler())
40+
3741
if not api_key:
3842
raise DeepgramApiKeyError("Deepgram API key is required")
3943

@@ -44,6 +48,8 @@ def __init__(self, api_key: str, config: Optional[DeepgramClientOptions] = None)
4448
config.set_apikey(self.api_key)
4549
self.config = config
4650

51+
self.logger.setLevel(logging.SPAM)
52+
4753
@property
4854
def listen(self):
4955
return ListenClient(self.config)
@@ -59,6 +65,9 @@ def onprem(self):
5965
# INTERNAL CLASSES
6066
class Version:
6167
def __init__(self, config, parent: str):
68+
self.logger = logging.getLogger(__name__)
69+
self.logger.addHandler(logging.StreamHandler())
70+
self.logger.setLevel(config.verbose)
6271
self.config = config
6372
self.parent = parent
6473

@@ -75,8 +84,11 @@ def __init__(self, config, parent: str):
7584
# raise DeepgramModuleError("Invalid parent")
7685

7786
def v(self, version: str = ""):
78-
# print(f"version: {version}")
87+
self.logger.debug("Version.v ENTER")
88+
self.logger.info("version: %s", version)
7989
if len(version) == 0:
90+
self.logger.error("version is empty")
91+
self.logger.debug("Version.v LEAVE")
8092
raise DeepgramModuleError("Invalid module version")
8193

8294
className = ""
@@ -86,22 +98,30 @@ def v(self, version: str = ""):
8698
case "onprem":
8799
className = "OnPremClient"
88100
case _:
101+
self.logger.error("parent unknown: %s", self.parent)
102+
self.logger.debug("Version.v LEAVE")
89103
raise DeepgramModuleError("Invalid parent type")
90104

91105
# create class path
92106
path = f"deepgram.clients.{self.parent}.v{version}.client"
93-
# print(f"path: {path}")
94-
# print(f"className: {className}")
107+
self.logger.info("path: %s", path)
108+
self.logger.info("className: %s", className)
95109

96110
# import class
97111
mod = import_module(path)
98112
if mod is None:
113+
self.logger.error("module path is None")
114+
self.logger.debug("Version.v LEAVE")
99115
raise DeepgramModuleError("Unable to find package")
100116

101117
my_class = getattr(mod, className)
102118
if my_class is None:
119+
self.logger.error("my_class is None")
120+
self.logger.debug("Version.v LEAVE")
103121
raise DeepgramModuleError("Unable to find class")
104122

105123
# instantiate class
106124
myClass = my_class(self.config)
125+
self.logger.notice("Version.v succeeded")
126+
self.logger.debug("Version.v LEAVE")
107127
return myClass

deepgram/clients/listen.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: MIT
44

55
from importlib import import_module
6+
import logging, verboselogs
67

78
from ..options import DeepgramClientOptions
89

@@ -30,6 +31,9 @@ def legacylive(self):
3031
# INTERNAL CLASSES
3132
class Version:
3233
def __init__(self, config, parent: str):
34+
self.logger = logging.getLogger(__name__)
35+
self.logger.addHandler(logging.StreamHandler())
36+
self.logger.setLevel(config.verbose)
3337
self.config = config
3438
self.parent = parent
3539

@@ -46,8 +50,11 @@ def __init__(self, config, parent: str):
4650
# raise DeepgramModuleError("Invalid parent")
4751

4852
def v(self, version: str = ""):
49-
# print(f"version: {version}")
53+
self.logger.debug("Version.v ENTER")
54+
self.logger.info("version: %s", version)
5055
if len(version) == 0:
56+
self.logger.error("version is empty")
57+
self.logger.debug("Version.v LEAVE")
5158
raise DeepgramModuleError("Invalid module version")
5259

5360
className = ""
@@ -57,22 +64,30 @@ def v(self, version: str = ""):
5764
case "prerecorded":
5865
className = "PreRecordedClient"
5966
case _:
67+
self.logger.error("parent unknown: %s", self.parent)
68+
self.logger.debug("Version.v LEAVE")
6069
raise DeepgramModuleError("Invalid parent type")
6170

6271
# create class path
6372
path = f"deepgram.clients.{self.parent}.v{version}.client"
64-
# print(f"path: {path}")
65-
# print(f"className: {className}")
73+
self.logger.info("path: %s", path)
74+
self.logger.info("className: %s", className)
6675

6776
# import class
6877
mod = import_module(path)
6978
if mod is None:
79+
self.logger.error("module path is None")
80+
self.logger.debug("Version.v LEAVE")
7081
raise DeepgramModuleError("Unable to find package")
7182

7283
my_class = getattr(mod, className)
7384
if my_class is None:
85+
self.logger.error("my_class is None")
86+
self.logger.debug("Version.v LEAVE")
7487
raise DeepgramModuleError("Unable to find class")
7588

7689
# instantiate class
7790
myClass = my_class(self.config)
91+
self.logger.notice("Version.v succeeded")
92+
self.logger.debug("Version.v LEAVE")
7893
return myClass

0 commit comments

Comments
 (0)