Skip to content

Commit 37ef2cd

Browse files
committed
test to fix logging
1 parent 6f6212d commit 37ef2cd

4 files changed

Lines changed: 34 additions & 38 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ This document outlines the coding standards and practices to follow when working
3333
## Logging
3434

3535
- **Eliot logging**: Use eliot as the default logging library using the `with start_action(...) as action` pattern
36-
- **Use log folder**: use to_nice_file(output_file=json_path, rendered_file=log_path) from pycomfort library (for which you also check exact syntax as you may have bad intuition on it) to add file destination for logging
36+
- **Use log folder**: use to_nice_file(output_file=json_path, rendered_file=log_path) and to_nice_stdout(output_file=json_path) from pycomfort library (for which you also check exact syntax as you may have bad intuition on it) to add file destination for logging
3737

3838
## CLI Development
3939

src/cell2sentence4longevity/preprocess.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import gc
66
import shutil
77
import re
8+
import sys
89

910
import typer
10-
from eliot import start_action
11-
from pycomfort.logging import to_nice_file
11+
from eliot import start_action, to_file
12+
from pycomfort.logging import to_nice_file, to_nice_stdout
1213
from dotenv import load_dotenv
1314

1415
from cell2sentence4longevity.preprocessing import (
@@ -118,6 +119,11 @@ def download(
118119
"-l",
119120
help="Path to eliot log file"
120121
),
122+
log_stdout: bool = typer.Option(
123+
True,
124+
"--log-stdout/--no-log-stdout",
125+
help="Mirror Eliot logs to stdout"
126+
),
121127
) -> None:
122128
"""Download dataset from a URL.
123129
@@ -128,6 +134,8 @@ def download(
128134
log_file.parent.mkdir(parents=True, exist_ok=True)
129135
json_path = log_file.with_suffix('.json')
130136
to_nice_file(output_file=json_path, rendered_file=log_file)
137+
if log_stdout:
138+
to_nice_stdout(output_file=json_path)
131139

132140
with start_action(action_type="cli_download") as action:
133141
if force:
@@ -196,6 +204,11 @@ def upload(
196204
"-l",
197205
help="Path to eliot log file"
198206
),
207+
log_stdout: bool = typer.Option(
208+
True,
209+
"--log-stdout/--no-log-stdout",
210+
help="Mirror Eliot logs to stdout"
211+
),
199212
) -> None:
200213
"""Upload to HuggingFace.
201214
@@ -205,6 +218,8 @@ def upload(
205218
log_file.parent.mkdir(parents=True, exist_ok=True)
206219
json_path = log_file.with_suffix('.json')
207220
to_nice_file(output_file=json_path, rendered_file=log_file)
221+
if log_stdout:
222+
to_file(sys.stdout)
208223

209224
with start_action(action_type="cli_upload") as action:
210225
typer.echo("Uploading to HuggingFace...")
@@ -296,7 +311,7 @@ def _process_single_file(
296311
join_collection=join_collection
297312
)
298313

299-
print(f" ✓ Conversion completed")
314+
typer.echo(" ✓ Conversion completed")
300315
action.log(message_type="conversion_completed", dataset_name=dataset_name)
301316

302317
# Force garbage collection to free memory
@@ -312,7 +327,7 @@ def _process_single_file(
312327
if repo_id and token:
313328
# Upload to same repository as subfolder (dataset_name creates subfolder in repo)
314329
action.log(message_type="upload_started", dataset_name=dataset_name, repo_id=repo_id, upload_dir=str(upload_dir))
315-
print(f" Uploading {dataset_name} to HuggingFace...")
330+
typer.echo(f" Uploading {dataset_name} to HuggingFace...")
316331
files_uploaded = upload_to_huggingface(
317332
data_splits_dir=upload_dir,
318333
token=token,
@@ -321,9 +336,9 @@ def _process_single_file(
321336
)
322337
dataset_url = f"https://huggingface.co/datasets/{repo_id}"
323338
if files_uploaded:
324-
print(f" ✓ Upload completed: {dataset_url}")
339+
typer.echo(f" ✓ Upload completed: {dataset_url}")
325340
else:
326-
print(f" ⚠ Upload returned False")
341+
typer.echo(" ⚠ Upload returned False")
327342
action.log(message_type="upload_completed", dataset_name=dataset_name, repo_id=repo_id, dataset_url=dataset_url, files_uploaded=files_uploaded)
328343

329344
# Final garbage collection
@@ -420,6 +435,11 @@ def run(
420435
"--log-dir",
421436
help="Directory for log files (separate log per file)"
422437
),
438+
log_stdout: bool = typer.Option(
439+
True,
440+
"--log-stdout/--no-log-stdout",
441+
help="Mirror Eliot logs to stdout"
442+
),
423443
batch_mode: bool = typer.Option(
424444
False,
425445
"--batch-mode",
@@ -472,7 +492,10 @@ def run(
472492
global_log = log_dir / "pipeline.log"
473493
json_path = global_log.with_suffix('.json')
474494
to_nice_file(output_file=json_path, rendered_file=global_log)
475-
print(f"Logging to: {global_log}")
495+
to_nice_stdout(output_file=json_path)
496+
typer.echo(f"Logging to: {global_log}")
497+
if log_stdout:
498+
to_file(sys.stdout)
476499

477500
with start_action(action_type="cli_run", batch_mode=batch_mode) as action:
478501
# Validate output directory - prevent writing to data/test (reserved for code tests)
@@ -541,7 +564,7 @@ def run(
541564
# Check if output already exists and skip if flag is enabled
542565
if skip_existing and check_output_exists(output_dir, dataset_name, skip_train_test_split):
543566
action.log(message_type="dataset_skipped", dataset_name=dataset_name, reason="output_already_exists", output_path=str(dataset_output_path))
544-
print(f" Skipping (output already exists)")
567+
typer.echo(" Skipping (output already exists)")
545568
skipped_datasets.append((dataset_name, dataset_output_path))
546569
continue
547570

src/cell2sentence4longevity/preprocessing/upload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def upload_to_huggingface(
276276
repo_paths=repo_paths[:10] # Log first 10 paths as sample
277277
)
278278

279-
print(f" Preparing to upload {total_files} files to {repo_id}...")
279+
typer.echo(f" Preparing to upload {total_files} files to {repo_id}...")
280280

281281
# Create a single commit with all operations (will overwrite existing files)
282282
with tqdm(total=1, desc=f'Uploading {dataset_name}') as pbar:
@@ -288,7 +288,7 @@ def upload_to_huggingface(
288288
)
289289
pbar.update(1)
290290

291-
print(f" Commit URL: {commit_info.commit_url}")
291+
typer.echo(f" Commit URL: {commit_info.commit_url}")
292292

293293
action.log(
294294
message_type="upload_complete",

tests/test_integration.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -394,33 +394,6 @@ def test_full_pipeline_with_real_data(self, temp_dirs: dict[str, Path]) -> None:
394394
test_action.log(message_type="test_complete", status="passed")
395395

396396

397-
class TestLogging:
398-
"""Test logging functionality."""
399-
400-
def test_log_file_creation(self) -> None:
401-
"""Test that log files are created correctly."""
402-
with tempfile.TemporaryDirectory() as temp_dir:
403-
temp_path = Path(temp_dir)
404-
json_log = temp_path / 'test.json'
405-
rendered_log = temp_path / 'test.log'
406-
407-
to_nice_file(output_file=json_log, rendered_file=rendered_log)
408-
409-
with start_action(action_type="test_log_creation") as action:
410-
action.log(message_type="test_message", status="testing")
411-
412-
assert json_log.exists(), "JSON log should be created"
413-
assert rendered_log.exists(), "Rendered log should be created"
414-
415-
# Verify JSON log content
416-
with open(json_log, 'r') as f:
417-
lines = f.readlines()
418-
assert len(lines) > 0, "JSON log should have content"
419-
420-
first_entry = json.loads(lines[0])
421-
assert 'action_type' in first_entry or 'message_type' in first_entry, \
422-
"Log should have structured entries"
423-
424397

425398
class TestAgeExtraction:
426399
"""Test age extraction functionality."""

0 commit comments

Comments
 (0)