Skip to content

Commit 83ea1fd

Browse files
authored
Merge pull request #557 from sunbeam-labs/codex/simplify-logging-setup-in-sunbeam
Use config-defined log file
2 parents aabaa83 + ebf9521 commit 83ea1fd

File tree

5 files changed

+17
-23
lines changed

5 files changed

+17
-23
lines changed

sunbeam/configs/default_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ all:
55
samplelist_fp: "samples.csv"
66
paired_end: true
77
version: "{SB_VERSION}"
8+
log_fp: "sunbeam.log"
89

910
# Quality control
1011
qc:

sunbeam/logging.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_sunbeam_logger() -> logging.Logger:
2525
return logger
2626

2727

28-
def get_pipeline_logger(log_file: Path = None) -> logging.Logger:
28+
def get_pipeline_logger(log_fp: Path = None) -> logging.Logger:
2929
"""Sets up logging for the main pipeline entry point."""
3030
logger = logging.getLogger("sunbeam.pipeline")
3131
logger.setLevel(logging.DEBUG)
@@ -38,11 +38,11 @@ def get_pipeline_logger(log_file: Path = None) -> logging.Logger:
3838
ch.setFormatter(ConditionalLevelFormatter())
3939

4040
# File handler
41-
if log_file is None:
41+
if log_fp is None:
4242
raise ValueError(
43-
"log_file is None but the logger hasn't been initialized with a file handler yet"
43+
"log_fp is None but the logger hasn't been initialized with a file handler yet"
4444
)
45-
fh = logging.FileHandler(log_file, mode="w")
45+
fh = logging.FileHandler(log_fp, mode="w")
4646
fh.setLevel(logging.DEBUG)
4747
fh.setFormatter(logging.Formatter("[%(asctime)s] %(levelname)s: %(message)s"))
4848

sunbeam/scripts/run.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import argparse
2-
import datetime
32
import os
43
import sys
54
from pathlib import Path
65
from snakemake.cli import main as snakemake_main
76
from sunbeam import __version__
87
from sunbeam.logging import get_pipeline_logger
8+
from sunbeam.project import SunbeamConfig
99

1010

1111
def main(argv: list[str] = sys.argv):
@@ -14,19 +14,17 @@ def main(argv: list[str] = sys.argv):
1414
args, remaining = parser.parse_known_args(argv)
1515

1616
profile = Path(args.profile).resolve()
17+
configfile = Path(profile) / "sunbeam_config.yml"
1718

18-
log_file = args.log_file
19-
if not log_file:
20-
log_file = (
21-
profile
22-
/ f"sunbeam_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.log"
23-
)
19+
sc = SunbeamConfig.from_file(configfile)
20+
Cfg = sc.resolved_paths()
21+
log_fp = Cfg["all"]["log_fp"]
2422

2523
# From here on everything is considered part of the "pipeline"
2624
# This means all logs are handled by the pipeline logger (or pipeline extension loggers)
2725
# You could argue it would make more sense to start this at the actual snakemake call
2826
# but this way we can log some relevant setup information that might be useful on post-mortem analysis
29-
logger = get_pipeline_logger(log_file)
27+
logger = get_pipeline_logger(log_fp)
3028

3129
snakefile = Path(__file__).parent.parent / "workflow" / "Snakefile"
3230
if not snakefile.exists():
@@ -56,8 +54,6 @@ def main(argv: list[str] = sys.argv):
5654

5755
os.environ["SUNBEAM_DOCKER_TAG"] = args.docker_tag
5856

59-
configfile = Path(profile) / "sunbeam_config.yml"
60-
6157
snakemake_args = [
6258
"--snakefile",
6359
str(snakefile),
@@ -128,10 +124,4 @@ def main_parser():
128124
default=__version__,
129125
help="The tag to use when pulling docker images for the core pipeline environments, defaults to sunbeam's current version, a good alternative is 'latest' for the latest stable release",
130126
)
131-
parser.add_argument(
132-
"--log_file",
133-
default=None,
134-
help="Path to a file where the pipeline log will be written. If not specified, logs will be written to `sunbeam_<current_datetime>.log` under the project directory.",
135-
)
136-
137127
return parser

sunbeam/workflow/Snakefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ from sunbeam.logging import get_pipeline_logger, get_extension_logger
1515
from sunbeam.project import SampleList, SunbeamConfig, output_subdir
1616
from sunbeam.project.post import compile_benchmarks
1717

18-
19-
logger = get_pipeline_logger()
20-
2118
MIN_MEM_MB = int(os.getenv("SUNBEAM_MIN_MEM_MB", 8000))
2219
MIN_RUNTIME = int(os.getenv("SUNBEAM_MIN_RUNTIME", 15))
2320

2421
sc = SunbeamConfig(config)
2522
Cfg = sc.resolved_paths()
23+
logger = get_pipeline_logger(Cfg["all"]["log_fp"])
2624
logger.debug(f"Sunbeam configuration: {Cfg}")
2725

2826
# Load extensions

tests/unit/test_sunbeam_config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_config_from_template(tmp_path):
3939
sc = SunbeamConfig.from_template(template_fp, root_fp, ext_dir)
4040
assert sc.config["all"]["root"] == str(root_fp.resolve())
4141
assert sc.config["all"]["version"] == __version__
42+
assert sc.config["all"]["log_fp"] == "sunbeam.log"
4243

4344

4445
def test_config_with_extension(test_extension):
@@ -57,13 +58,17 @@ def test_config_from_file(tmp_path):
5758
all:
5859
root: /path/to/root
5960
version: 1.0.0
61+
log_fp: sunbeam.log
6062
"""
6163
config_fp = tmp_path / "config.yml"
6264
with open(config_fp, "w") as f:
6365
f.write(config_str)
6466

6567
sc = SunbeamConfig.from_file(config_fp)
6668
assert sc.config["all"]["root"] == "/path/to/root"
69+
assert sc.config["all"]["log_fp"] == "sunbeam.log"
70+
res = sc.resolved_paths()
71+
assert res["all"]["log_fp"] == Path("/path/to/root") / "sunbeam.log"
6772

6873

6974
def test_config_to_file(tmp_path):

0 commit comments

Comments
 (0)