Skip to content

fix: setup_logfile in setup logger to ensure logfile handler is added#3486

Merged
johanneskoester merged 9 commits intosnakemake:mainfrom
cademirch:fix-logfile-issue
Mar 26, 2025
Merged

fix: setup_logfile in setup logger to ensure logfile handler is added#3486
johanneskoester merged 9 commits intosnakemake:mainfrom
cademirch:fix-logfile-issue

Conversation

@cademirch
Copy link
Copy Markdown
Contributor

@cademirch cademirch commented Mar 26, 2025

Fixes oversight that resulted in empty log files. Will add a test soon.

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • The documentation (docs/) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake).

Summary by CodeRabbit

  • New Features

    • Introduced a new test for verifying the existence and content of log files generated during execution.
    • Added a new workflow definition with rules for generating output files.
  • Refactor

    • Enhanced the logging system to ensure log files are created during setup and improved the cleanup process by detaching handlers.
  • Bug Fixes

    • Improved error handling during temporary directory cleanup, ensuring resources are properly released on Windows systems.

@cademirch cademirch requested a review from fgvieira March 26, 2025 14:11
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 26, 2025

📝 Walkthrough

Walkthrough

The changes modify the logging behavior by removing the log file setup call from the workflow execution method in the API module and integrating the log file setup into the logger manager. The logger manager now conditionally creates a log file during setup and improves the cleanup process by detaching handlers before closing the logger. Additionally, a new test for log file verification has been added, and error handling for temporary directory cleanup in tests has been enhanced to address potential issues on Windows platforms.

Changes

File(s) Change Summary
src/snakemake/api.py Removed the call to logger_manager.setup_logfile() in the execute_workflow method of the DAGApi class.
src/snakemake/logging.py Added a call to setup_logfile() in the LoggerManager.setup method when no stream handlers are present; updated cleanup_logfile() to detach the handler from the logger before closing it; updated get_logfile() to specify a return type of List[str].
tests/tests.py Added a new test function test_logfile() for log file verification; updated test_github_issue_14() to improve error handling during cleanup on Windows.
tests/test_logfile/Snakefile Introduced a new Snakefile defining two rules: all and a, for generating output files.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant LoggerManager
    participant DAGApi

    User->>DAGApi: execute_workflow()
    DAGApi-->>LoggerManager: setup_logfile()
    LoggerManager-->>DAGApi: Log file setup complete
    DAGApi-->>User: Workflow execution complete
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7a9bd6f and 7efe060.

📒 Files selected for processing (1)
  • src/snakemake/logging.py (4 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.py`: Do not try to improve formatting. Do not suggest type annotations for functions that are defined inside of functions or methods. Do not suggest type annotation of the `s...

**/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

  • src/snakemake/logging.py
⏰ Context from checks skipped due to timeout of 90000ms (34)
  • GitHub Check: tests (10, windows-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py311)
  • GitHub Check: tests (9, windows-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py311)
  • GitHub Check: tests (8, windows-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py311)
  • GitHub Check: tests (7, windows-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py311)
  • GitHub Check: tests (6, windows-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py311)
  • GitHub Check: tests (5, macos-latest, py312)
  • GitHub Check: tests (5, windows-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py311)
  • GitHub Check: tests (4, windows-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py311)
  • GitHub Check: tests (3, windows-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py311)
  • GitHub Check: tests (2, macos-latest, py312)
  • GitHub Check: tests (2, windows-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py311)
  • GitHub Check: tests (1, macos-latest, py312)
  • GitHub Check: tests (1, windows-latest, py312)
  • GitHub Check: tests (1, ubuntu-latest, py312)
  • GitHub Check: tests (1, ubuntu-latest, py311)
  • GitHub Check: apidocs
🔇 Additional comments (4)
src/snakemake/logging.py (4)

592-593: Good centralization of logfile setup.

Moving the logfile setup call here ensures that a log file is created during the initial logger configuration, which aligns with the PR's objective of fixing empty log files.


648-649: Return type annotation looks good, but note the previous review comment.

The type annotation follows proper typing conventions. For backwards compatibility, consider using the uppercase form as suggested in the previous review comment.

-    def get_logfile(self) -> List[str]:
+    def get_logfile(self) -> List[str]:

666-666: Good handler cleanup improvement.

Adding the handler removal before closing it is an excellent practice. This ensures proper cleanup and prevents potential issues with lingering handlers after cleanup, which could attempt to log messages after being closed.


669-688:

Details

❓ Verification inconclusive

Verify that log files are being properly created with this implementation.

The implementation looks solid. It creates log files in a consistent location with timestamped filenames and includes proper error handling.


🏁 Script executed:

#!/bin/bash
# Description: Verify that log files are created in the expected location

# Create a simple temporary Snakefile
cat > Snakefile << 'EOF'
rule all:
    input: "output.txt"

rule create_output:
    output: "output.txt"
    shell: "echo 'Hello, world!' > {output}"
EOF

# Run snakemake to create log file
python -m snakemake --cores 1

# Check if log file was created
ls -la .snakemake/log/*.snakemake.log | wc -l

Length of output: 210


Confirm proper environment configuration for log file verification

  • The implementation in src/snakemake/logging.py for setting up a timestamped log file appears solid, with correct file path construction and error handling.
  • The verification script did not produce a log file because it failed with “No module named snakemake”. This indicates that the test environment is missing the required snakemake module, preventing the logging code from being executed.
  • Please ensure that the testing environment has the snakemake module installed so that the log file creation can be properly triggered and verified. Once the environment is set up correctly, re-run the verification to confirm that the log files are indeed generated in the expected .snakemake/log directory.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/test_logfile/Snakefile (1)

1-11: LGTM! Clean test workflow for log file verification.

This Snakefile creates a simple workflow with two rules that will generate 5 output files, which is appropriate for testing the log file functionality that's being fixed in this PR.

One minor suggestion: you might want to consider adding a directory creation step for the "results" directory to ensure the workflow doesn't fail if the directory doesn't exist. This could be done either:

  1. Within the Snakefile using shell: "mkdir -p results && echo {wildcards.i} > {output}"
  2. Or in the Python test that uses this Snakefile

However, if the test framework automatically handles this, the current implementation is fine.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 262fc48 and 0cc44db.

📒 Files selected for processing (2)
  • tests/test_logfile/Snakefile (1 hunks)
  • tests/tests.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/tests.py
⏰ Context from checks skipped due to timeout of 90000ms (34)
  • GitHub Check: tests (10, windows-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py311)
  • GitHub Check: tests (9, windows-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py311)
  • GitHub Check: tests (8, windows-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py311)
  • GitHub Check: tests (7, windows-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py311)
  • GitHub Check: tests (6, windows-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py311)
  • GitHub Check: tests (5, windows-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py311)
  • GitHub Check: tests (4, macos-latest, py312)
  • GitHub Check: tests (4, windows-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py311)
  • GitHub Check: tests (3, windows-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py311)
  • GitHub Check: tests (2, macos-latest, py312)
  • GitHub Check: tests (2, windows-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py311)
  • GitHub Check: tests (1, macos-latest, py312)
  • GitHub Check: tests (1, windows-latest, py312)
  • GitHub Check: tests (1, ubuntu-latest, py312)
  • GitHub Check: tests (1, ubuntu-latest, py311)
  • GitHub Check: apidocs

Comment thread src/snakemake/logging.py Outdated
@johanneskoester johanneskoester merged commit 2ed13fc into snakemake:main Mar 26, 2025
48 checks passed
johanneskoester pushed a commit that referenced this pull request Mar 28, 2025
🤖 I have created a release *beep* *boop*
---


##
[9.1.3](v9.1.2...v9.1.3)
(2025-03-26)


### Bug Fixes

* pass correct local storage prefix in case an executor spawns an inner
job ([#3488](#3488))
([1454ca1](1454ca1))
* setup_logfile in setup logger to ensure logfile handler is added
([#3486](#3486))
([2ed13fc](2ed13fc))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
@coderabbitai coderabbitai Bot mentioned this pull request May 13, 2025
2 tasks
kjohnsen pushed a commit to kjohnsen/snakemake that referenced this pull request Dec 15, 2025
…snakemake#3486)

Fixes oversight that resulted in empty log files. Will add a test soon.

### QC
<!-- Make sure that you can tick the boxes below. -->

* [x] The PR contains a test case for the changes or the changes are
already covered by an existing test case.
* [x] The documentation (`docs/`) is updated to reflect the changes or
this is not necessary (e.g. if the change does neither modify the
language nor the behavior or functionalities of Snakemake).


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced a new test for verifying the existence and content of log
files generated during execution.
- Added a new workflow definition with rules for generating output
files.

- **Refactor**
- Enhanced the logging system to ensure log files are created during
setup and improved the cleanup process by detaching handlers.

- **Bug Fixes**
- Improved error handling during temporary directory cleanup, ensuring
resources are properly released on Windows systems.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Johannes Köster <[email protected]>
kjohnsen pushed a commit to kjohnsen/snakemake that referenced this pull request Dec 15, 2025
🤖 I have created a release *beep* *boop*
---


##
[9.1.3](snakemake/snakemake@v9.1.2...v9.1.3)
(2025-03-26)


### Bug Fixes

* pass correct local storage prefix in case an executor spawns an inner
job ([snakemake#3488](snakemake#3488))
([1454ca1](snakemake@1454ca1))
* setup_logfile in setup logger to ensure logfile handler is added
([snakemake#3486](snakemake#3486))
([2ed13fc](snakemake@2ed13fc))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants