diff --git a/src/reference/snakemake-style-guide.rst b/src/reference/snakemake-style-guide.rst index dc8f234f..4db75316 100644 --- a/src/reference/snakemake-style-guide.rst +++ b/src/reference/snakemake-style-guide.rst @@ -193,3 +193,37 @@ Example: --output-sequences {output.sequences:q} \ --output-metadata {output.metadata:q} """ + +Log standard out and error output to log files and the terminal +=============================================================== + +Use `the Snakemake log directive `_ for each rule that writes output to either standard out or error and direct output to the corresponding log file. +Use the ``tee`` command to ensure that output gets written to both the log file and the terminal, so users can track their workflow progress interactively and use the log file later for debugging. + +Example: + +.. code-block:: python + + rule filter: + input: + metadata="results/metadata.tsv", + output: + metadata="results/filtered_metadata.tsv", + log: + "logs/filter.txt" + shell: + """ + augur filter \ + --metadata {input.metadata} \ + --output-metadata {output.metadata} 2>&1 | tee {log} + """ + +Before using ``tee``, ensure that your workflow uses `bash's pipefail option `_, so successful ``tee`` execution does not mask errors from earlier commands in the pipe. +Snakemake uses bash's strict mode by default, so the pipefail option should be enabled by default. +However, some workflows may override the defaults `locally at specific rules `_ or globally as with `a custom shell prefix `_. + +Run workflows with ``--show-failed-logs`` +========================================= + +Run workflows with the ``--show-failed-logs`` which will print the logs for failed jobs to the terminal when the workflow exits. +This pattern helps users identify error messages without first finding the corresponding log file.