Skip to content

Commit c585ac3

Browse files
authored
fix: consolidate logging setup (#541)
* fix: consolidate logging setup When switching autosynth to use synthtool's git module, we transitively load synthtool's logger setup which adds a log handler globally. This causes logs to be emitted twice (with 2 different styles). This consolidates the logging setup into a single helper and now both autosynth's and synthtool's logger instance use the same configuration (with different logger names). * fix: add the level name when the log isn't colorize
1 parent 5b48b07 commit c585ac3

16 files changed

Lines changed: 133 additions & 150 deletions

autosynth/log.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import logging
15+
from synthtool.log import configure_logger
1616

1717

18-
def _configure_logger():
19-
"""Create and configure the default logger for autosynth.
20-
21-
The logger will prefix the log message with the current time and the
22-
log severity.
23-
"""
24-
logger = logging.getLogger("autosynth")
25-
logger.setLevel(logging.DEBUG)
26-
27-
handler = logging.StreamHandler()
28-
handler.setLevel(logging.DEBUG)
29-
formatter = logging.Formatter(
30-
"%(asctime)s [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
31-
)
32-
handler.setFormatter(formatter)
33-
logger.addHandler(handler)
34-
return logger
35-
36-
37-
logger = _configure_logger()
18+
logger = configure_logger("autosynth")

synthtool/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import sys
1818

1919
from synthtool.transforms import move, replace
20-
from synthtool import log
20+
from synthtool.log import logger
2121

2222
copy = move
2323

@@ -27,6 +27,6 @@
2727
# directly
2828
_main_module = sys.modules["__main__"]
2929
if hasattr(_main_module, "__file__") and "synthtool" not in _main_module.__file__:
30-
log.critical(
30+
logger.critical(
3131
"You are running the synthesis script directly, this will be disabled in a future release of Synthtool. Please use python3 -m synthtool instead."
3232
)

synthtool/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import click
2121
import pkg_resources
22-
import synthtool.log
22+
from synthtool.log import logger
2323
import synthtool.metadata
2424
from synthtool import preconfig
2525

@@ -82,7 +82,7 @@ def main(synthfile: str, metadata: str, extra_args: Sequence[str]):
8282
synth_file = os.path.abspath(synthfile)
8383

8484
if os.path.lexists(synth_file):
85-
synthtool.log.debug(f"Executing {synth_file}.")
85+
logger.debug(f"Executing {synth_file}.")
8686
# https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
8787
spec = importlib.util.spec_from_file_location("synth", synth_file)
8888
synth_module = importlib.util.module_from_spec(spec)
@@ -94,7 +94,7 @@ def main(synthfile: str, metadata: str, extra_args: Sequence[str]):
9494
spec.loader.exec_module(synth_module) # type: ignore
9595

9696
else:
97-
synthtool.log.exception(f"{synth_file} not found.")
97+
logger.exception(f"{synth_file} not found.")
9898
sys.exit(1)
9999

100100

synthtool/gcp/artman.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
import platform
1919
import tempfile
2020

21-
from synthtool import log
22-
from synthtool import metadata
23-
from synthtool import shell
21+
from synthtool import metadata, shell
22+
from synthtool.log import logger
2423

2524
ARTMAN_VERSION = os.environ.get("SYNTHTOOL_ARTMAN_VERSION", "latest")
2625

@@ -143,7 +142,7 @@ def run(
143142
return output_dir
144143

145144
def _ensure_dependencies_installed(self):
146-
log.debug("Ensuring dependencies.")
145+
logger.debug("Ensuring dependencies.")
147146

148147
dependencies = ["docker", "git"]
149148
failed_dependencies = []
@@ -158,7 +157,7 @@ def _ensure_dependencies_installed(self):
158157
)
159158

160159
def _install_artman(self):
161-
log.debug("Pulling artman image.")
160+
logger.debug("Pulling artman image.")
162161
shell.run(
163162
["docker", "pull", f"googleapis/artman:{ARTMAN_VERSION}"], hide_output=False
164163
)

synthtool/gcp/common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
from pathlib import Path
2020
from typing import Dict, List, Optional
2121

22+
from synthtool import _tracked_paths
2223
from synthtool.languages import node
24+
from synthtool.log import logger
2325
from synthtool.sources import git, templates
24-
from synthtool import _tracked_paths
25-
from synthtool import log
2626

2727

2828
TEMPLATES_URL: str = git.make_repo_clone_url("googleapis/synthtool")
@@ -33,7 +33,7 @@
3333
class CommonTemplates:
3434
def __init__(self):
3535
if LOCAL_TEMPLATES:
36-
log.debug(f"Using local templates at {LOCAL_TEMPLATES}")
36+
logger.debug(f"Using local templates at {LOCAL_TEMPLATES}")
3737
self._template_root = LOCAL_TEMPLATES
3838
else:
3939
templates_git = git.clone(TEMPLATES_URL)
@@ -68,7 +68,7 @@ def py_library(self, **kwargs) -> Path:
6868
kwargs["system_test_local_dependencies"] = kwargs[
6969
"system_test_dependencies"
7070
]
71-
log.warning(
71+
logger.warning(
7272
"Template argument 'system_test_dependencies' is deprecated."
7373
"Use 'system_test_local_dependencies' or 'system_test_external_dependencies'"
7474
"instead."

synthtool/gcp/discogapic_generator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from pathlib import Path
1616

1717
from synthtool import _tracked_paths
18-
from synthtool import log
1918
from synthtool.gcp import artman
19+
from synthtool.log import logger
2020
from synthtool.sources import git
2121

2222
DISCOVERY_ARTIFACT_MANAGER_URL: str = git.make_repo_clone_url(
@@ -90,7 +90,7 @@ def _generate_code(
9090
f"Unable to find configuration yaml file: {config_path}."
9191
)
9292

93-
log.debug(f"Running generator for {config_path}.")
93+
logger.debug(f"Running generator for {config_path}.")
9494
output_root = artman.Artman().run(
9595
f"googleapis/artman:{artman.ARTMAN_VERSION}",
9696
self.discovery_artifact_manager,
@@ -109,11 +109,11 @@ def _generate_code(
109109
f"Unable to find generated output of artman: {genfiles}."
110110
)
111111

112-
log.success(f"Generated code into {genfiles}.")
112+
logger.success(f"Generated code into {genfiles}.")
113113

114114
_tracked_paths.add(genfiles)
115115
return genfiles
116116

117117
def _clone_discovery_artifact_manager(self):
118-
log.debug("Cloning discovery-artifact-manager.")
118+
logger.debug("Cloning discovery-artifact-manager.")
119119
self.discovery_artifact_manager = git.clone(DISCOVERY_ARTIFACT_MANAGER_URL)

synthtool/gcp/gapic_bazel.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
import shutil
1818
import tempfile
1919

20-
from synthtool import _tracked_paths
21-
from synthtool import log
22-
from synthtool import metadata
23-
from synthtool import shell
20+
from synthtool import _tracked_paths, metadata, shell
21+
from synthtool.log import logger
2422
from synthtool.sources import git
2523

2624
GOOGLEAPIS_URL: str = git.make_repo_clone_url("googleapis/googleapis")
@@ -173,7 +171,7 @@ def _generate_code(
173171

174172
bazel_run_args = ["bazel", "build", bazel_target]
175173

176-
log.debug(f"Generating code for: {bazel_target}.")
174+
logger.debug(f"Generating code for: {bazel_target}.")
177175
shell.run(bazel_run_args)
178176

179177
# We've got tar file!
@@ -211,9 +209,9 @@ def _generate_code(
211209
os.makedirs(proto_output_path, exist_ok=True)
212210

213211
for i in proto_files:
214-
log.debug(f"Copy: {i} to {proto_output_path / i.name}")
212+
logger.debug(f"Copy: {i} to {proto_output_path / i.name}")
215213
shutil.copyfile(i, proto_output_path / i.name)
216-
log.success(f"Placed proto files into {proto_output_path}.")
214+
logger.success(f"Placed proto files into {proto_output_path}.")
217215

218216
os.chdir(cwd)
219217

@@ -225,7 +223,7 @@ def _generate_code(
225223
)
226224

227225
# Huzzah, it worked.
228-
log.success(f"Generated code into {output_dir}.")
226+
logger.success(f"Generated code into {output_dir}.")
229227

230228
# Record this in the synthtool metadata.
231229
metadata.add_client_destination(
@@ -245,10 +243,10 @@ def _clone_googleapis(self):
245243

246244
if LOCAL_GOOGLEAPIS:
247245
self._googleapis = Path(LOCAL_GOOGLEAPIS).expanduser()
248-
log.debug(f"Using local googleapis at {self._googleapis}")
246+
logger.debug(f"Using local googleapis at {self._googleapis}")
249247

250248
else:
251-
log.debug("Cloning googleapis.")
249+
logger.debug("Cloning googleapis.")
252250
self._googleapis = git.clone(GOOGLEAPIS_URL)
253251

254252
return self._googleapis
@@ -259,12 +257,12 @@ def _clone_googleapis_private(self):
259257

260258
if LOCAL_GOOGLEAPIS:
261259
self._googleapis_private = Path(LOCAL_GOOGLEAPIS).expanduser()
262-
log.debug(
260+
logger.debug(
263261
f"Using local googleapis at {self._googleapis_private} for googleapis-private"
264262
)
265263

266264
else:
267-
log.debug("Cloning googleapis-private.")
265+
logger.debug("Cloning googleapis-private.")
268266
self._googleapis_private = git.clone(GOOGLEAPIS_PRIVATE_URL)
269267

270268
return self._googleapis_private
@@ -277,17 +275,17 @@ def _clone_discovery_artifact_manager(self):
277275
self._discovery_artifact_manager = Path(
278276
LOCAL_DISCOVERY_ARTIFACT_MANAGER
279277
).expanduser()
280-
log.debug(
278+
logger.debug(
281279
f"Using local discovery_artifact_manager at {self._discovery_artifact_manager} for googleapis-private"
282280
)
283281
else:
284-
log.debug("Cloning discovery-artifact-manager.")
282+
logger.debug("Cloning discovery-artifact-manager.")
285283
self._discovery_artifact_manager = git.clone(DISCOVERY_ARTIFACT_MANAGER_URL)
286284

287285
return self._discovery_artifact_manager
288286

289287
def _ensure_dependencies_installed(self):
290-
log.debug("Ensuring dependencies.")
288+
logger.debug("Ensuring dependencies.")
291289

292290
dependencies = ["bazel", "zip", "unzip", "tar"]
293291
failed_dependencies = []

synthtool/gcp/gapic_generator.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
from pathlib import Path
2222
from typing import Optional
2323

24-
from synthtool import _tracked_paths
25-
from synthtool import log
26-
from synthtool import metadata
27-
from synthtool import shell
24+
from synthtool import _tracked_paths, metadata, shell
2825
from synthtool.gcp import artman
26+
from synthtool.log import logger
2927
from synthtool.sources import git
3028

3129
GOOGLEAPIS_URL: str = git.make_repo_clone_url("googleapis/googleapis")
@@ -103,7 +101,7 @@ def _generate_code(
103101

104102
generator_dir = LOCAL_GENERATOR
105103
if generator_dir is not None:
106-
log.debug(f"Using local generator at {generator_dir}")
104+
logger.debug(f"Using local generator at {generator_dir}")
107105

108106
# Run the code generator.
109107
# $ artman --config path/to/artman_api.yaml generate python_gapic
@@ -121,7 +119,7 @@ def _generate_code(
121119
f"Unable to find configuration yaml file: {(googleapis / config_path)}."
122120
)
123121

124-
log.debug(f"Running generator for {config_path}.")
122+
logger.debug(f"Running generator for {config_path}.")
125123

126124
if include_samples:
127125
if generator_args is None:
@@ -149,7 +147,7 @@ def _generate_code(
149147
f"Unable to find generated output of artman: {genfiles}."
150148
)
151149

152-
log.success(f"Generated code into {genfiles}.")
150+
logger.success(f"Generated code into {genfiles}.")
153151

154152
# Get the *.protos files and put them in a protos dir in the output
155153
if include_protos:
@@ -165,9 +163,9 @@ def _generate_code(
165163
os.makedirs(proto_output_path, exist_ok=True)
166164

167165
for i in proto_files:
168-
log.debug(f"Copy: {i} to {proto_output_path / i.name}")
166+
logger.debug(f"Copy: {i} to {proto_output_path / i.name}")
169167
shutil.copyfile(i, proto_output_path / i.name)
170-
log.success(f"Placed proto files into {proto_output_path}.")
168+
logger.success(f"Placed proto files into {proto_output_path}.")
171169

172170
if include_samples:
173171
samples_root_dir = None
@@ -209,10 +207,10 @@ def _clone_googleapis(self):
209207

210208
if LOCAL_GOOGLEAPIS:
211209
self._googleapis = Path(LOCAL_GOOGLEAPIS).expanduser()
212-
log.debug(f"Using local googleapis at {self._googleapis}")
210+
logger.debug(f"Using local googleapis at {self._googleapis}")
213211

214212
else:
215-
log.debug("Cloning googleapis.")
213+
logger.debug("Cloning googleapis.")
216214
self._googleapis = git.clone(GOOGLEAPIS_URL)
217215

218216
return self._googleapis
@@ -223,12 +221,12 @@ def _clone_googleapis_private(self):
223221

224222
if LOCAL_GOOGLEAPIS:
225223
self._googleapis_private = Path(LOCAL_GOOGLEAPIS).expanduser()
226-
log.debug(
224+
logger.debug(
227225
f"Using local googleapis at {self._googleapis_private} for googleapis-private"
228226
)
229227

230228
else:
231-
log.debug("Cloning googleapis-private.")
229+
logger.debug("Cloning googleapis-private.")
232230
self._googleapis_private = git.clone(GOOGLEAPIS_PRIVATE_URL)
233231

234232
return self._googleapis_private
@@ -300,7 +298,7 @@ def _include_samples(
300298
test_files = googleapis_samples_dir.glob("**/*.test.yaml")
301299
os.makedirs(samples_test_dir, exist_ok=True)
302300
for i in test_files:
303-
log.debug(f"Copy: {i} to {samples_test_dir / i.name}")
301+
logger.debug(f"Copy: {i} to {samples_test_dir / i.name}")
304302
shutil.copyfile(i, samples_test_dir / i.name)
305303

306304
# Download sample resources from sample_resources.yaml storage URIs.
@@ -321,7 +319,7 @@ def _include_samples(
321319
response = requests.get(uri, allow_redirects=True)
322320
download_path = samples_resources_dir / os.path.basename(uri)
323321
os.makedirs(samples_resources_dir, exist_ok=True)
324-
log.debug(f"Download {uri} to {download_path}")
322+
logger.debug(f"Download {uri} to {download_path}")
325323
with open(download_path, "wb") as output: # type: ignore
326324
output.write(response.content)
327325

@@ -339,7 +337,7 @@ def _include_samples(
339337
"ruby": "bundle exec ruby",
340338
}
341339
if language not in LANGUAGE_EXECUTABLES:
342-
log.info("skipping manifest gen")
340+
logger.info("skipping manifest gen")
343341
return None
344342

345343
manifest_arguments = [
@@ -355,7 +353,7 @@ def _include_samples(
355353
if os.path.isfile(code_sample):
356354
manifest_arguments.append(sample_path)
357355
try:
358-
log.debug(f"Writing samples manifest {manifest_arguments}")
356+
logger.debug(f"Writing samples manifest {manifest_arguments}")
359357
shell.run(manifest_arguments, cwd=samples_root_dir)
360358
except (subprocess.CalledProcessError, FileNotFoundError):
361-
log.warning("gen-manifest failed (sample-tester may not be installed)")
359+
logger.warning("gen-manifest failed (sample-tester may not be installed)")

0 commit comments

Comments
 (0)