Skip to content

Commit 09799cb

Browse files
committed
🌲logging
1 parent 1debfe2 commit 09799cb

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

src/snippet/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
def main(config: Config):
99
if config.log_level:
1010
logging.basicConfig(level=config.log_level)
11-
11+
logger = logging.getLogger(__name__)
12+
logger.debug('project directory is %r', config.project_root)
1213
examples, paths, failures = run(config)
1314

1415
if failures:
15-
logging.error('failures:\n%s', textwrap.indent('\n'.join(f'{name}: {exc}' for name, exc in failures), prefix=' '))
16+
logger.error('failures:\n%s', textwrap.indent('\n'.join(f'{name}: {exc}' for name, exc in failures), prefix=' '))
1617
raise Exception(f'There were %s failures!' % len(failures))

src/snippet/config.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import glob
21
import logging
2+
import glob
33
import os
44

55
import toml
66

7+
from snippet.logs import logger
8+
79

810
class Config:
911
# IO
@@ -20,7 +22,7 @@ class Config:
2022
# a mustache template for each file (triple braces important for code literals, no escaping)
2123
output_template = '```{{language_name}}\n{{comment_prefix}}example: {{{name}}}{{comment_suffix}}\n{{{code}}}\n```\n'
2224

23-
# Logging
25+
# logger
2426
log_level = logging.INFO
2527

2628
# Code block indicators
@@ -65,16 +67,16 @@ def get_config(config_paths=None, **options):
6567
config_paths.append(os.path.join(project_root, '**', '*.toml'))
6668

6769
for toml_file in find_configs(glob_patterns=config_paths):
68-
logging.debug('trying config from %s', toml_file)
70+
logger.debug('trying config from %s', toml_file)
6971
with open(toml_file) as f:
7072
try:
7173
config_file_contents = toml.load(f)
7274
except toml.TomlDecodeError as e:
73-
logging.debug('failed to load %s: %s', toml_file, e)
75+
logger.debug('failed to load %s: %s', toml_file, e)
7476
continue
7577
snippet_config = config_file_contents.get('snippet')
7678
if snippet_config:
77-
logging.info('loading config from %s', toml_file)
79+
logger.info('loading config from %s', toml_file)
7880
new_options.update(snippet_config)
7981

8082
# passed keyword args override other parameters

src/snippet/file_wrangler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import logging
21
import os
32
import glob
43

54
import pystache
65

76
from snippet.config import Config
7+
from snippet.logs import logger
88

99

1010
def write_example(config: Config, example_name, example_block):
@@ -24,10 +24,10 @@ def write_example(config: Config, example_name, example_block):
2424
)
2525

2626
if not os.path.exists(config.output_dir):
27-
logging.info('creating output directory %s', config.output_dir)
27+
logger.info('creating output directory %s', config.output_dir)
2828
os.makedirs(config.output_dir)
2929
output_file = os.path.join(config.output_dir, output_file_name)
30-
logging.info('writing %r to %s', example_name, output_file)
30+
logger.info('writing %r to %s', example_name, output_file)
3131
with open(output_file, 'a' if config.output_append else 'w') as fh:
3232
fh.write(output)
3333

src/snippet/logs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import logging
2+
3+
logger = logging.getLogger(__name__)

src/snippet/workflow.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
import textwrap
32
from functools import partial
43

@@ -7,13 +6,14 @@
76
from snippet.snippet import extract_snippets
87
from snippet.wrapper import wrap
98
from snippet import exceptions
9+
from snippet.logs import logger
1010

1111

1212
def run(config: Config):
1313
examples = {}
1414
failures = []
1515
paths = file_wrangler.find_files(config)
16-
logging.debug('files to parse:\n%s', textwrap.indent('\n'.join(paths), prefix=' '))
16+
logger.debug('files to parse:\n%s', textwrap.indent('\n'.join(paths), prefix=' '))
1717

1818
for path in paths:
1919
# load the file
@@ -39,8 +39,8 @@ def run(config: Config):
3939

4040
for (path, line_num, example_name), code_lines in examples.items():
4141
example_block = '\n'.join(code_lines)
42-
logging.info('example: %r', example_name)
43-
logging.debug('example code: %s', example_block)
42+
logger.info('example: %r', example_name)
43+
logger.debug('example code: %s', example_block)
4444

4545
wrap(config, failures, path, partial(
4646
file_wrangler.write_example, config, example_name, example_block

0 commit comments

Comments
 (0)