Skip to content

Conversation

@Qazalbash
Copy link
Contributor

@Qazalbash Qazalbash commented Apr 5, 2025

Summary

Logging functionality for better verbose and messages. flowMC.logger.logger is a modified form of loguru.logger to work with JAX, especially under jax.jit transform 1. loguru has different logging modes, making logs easy to read.

from flowMC.logger import enable_logging, logger

enable_logging()

logger.critical("This is a critical message")
logger.debug("This is a debug message")
logger.error("This is an error message")
logger.exception("This is an exception message")
logger.info("This is an info message")
logger.success("This is a success message")
logger.trace("This is a trace message")
logger.warning("This is a warning message")
2025-04-05 19:34:47.169 | CRITICAL | This is a critical message
2025-04-05 19:34:47.169 | DEBUG    | This is a debug message
2025-04-05 19:34:47.170 | ERROR    | This is an error message
2025-04-05 19:34:47.170 | ERROR    | This is an exception message
NoneType: None
2025-04-05 19:34:47.171 | INFO     | This is an info message
2025-04-05 19:34:47.171 | SUCCESS  | This is a success message
2025-04-05 19:34:47.171 | TRACE    | This is a trace message
2025-04-05 19:34:47.172 | WARNING  | This is a warning message

Pointer to discuss

  • Do you prefer environment variables like FLOWMC_LOG_LEVEL=<log level or name>?
  • What would be better logging onto the terminal or logging into a file?

Related To

Summary by CodeRabbit

  • New Features

    • Introduced enhanced logging capabilities with multiple configurable log levels for improved debugging and system insights.
    • Made logging easily accessible through a streamlined interface for diagnostics and monitoring.
  • Chores

    • Updated project dependencies to include a robust, third-party logging library.

Footnotes

  1. https://github.com/jax-ml/jax/discussions/25685

@coderabbitai
Copy link

coderabbitai bot commented Apr 5, 2025

Walkthrough

The changes introduce enhanced logging capabilities into the project. A new dependency (loguru>=0.7.0) is added in pyproject.toml, and the logging functionality is expanded by adding a new logging module in src/flowMC/logger/_logger.py. This module implements a custom Logger class with various logging methods and a dedicated enable_logging function. Additionally, the src/flowMC/logger/__init__.py file exposes the logging functions and logger object for easier use in the project.

Changes

File(s) Change Summary
pyproject.toml Added new dependency "loguru>=0.7.0" to support enhanced logging features.
src/flowMC/logger/__init__.py
src/flowMC/logger/_logger.py
Introduced a new logging module with a custom Logger class and multiple logging methods. Added and exported the enable_logging function and logger object.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant Logger as Logger Module
    participant Loguru as Loguru Library
    participant Callback as jax.debug.callback

    App->>Logger: enable_logging("DEBUG")
    Logger->>Loguru: Remove existing handlers
    Logger->>Loguru: Add new logging handler with config
    App->>Logger: Log message (e.g., info, error)
    Logger-->>Logger: Check if logging is enabled (global LOG)
    alt Logging Enabled
        Logger->>Callback: Invoke logging callback with message details
    else Logging Disabled
        Note over Logger,App: Skip logging action
    end
Loading

Poem

I'm the rabbit of code, hopping through each line,
With loguru’s magic, logging now feels divine.
I skipped through pyproject and danced in the logs,
New functions and features, no more coding fogs.
Huzzah for clear debugging—my hops sing in digital rhymes!
🐰✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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

@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 (2)
src/flowMC/logger/_logger.py (2)

109-123: Document the purpose of options modification in the exception method.

In the exception method, options[0] is set to True without explaining what this option controls. Consider adding a comment explaining the significance of this modification.

- options = (True,) + __self._options[1:]
+ # Set the first option to True to capture the current exception
+ options = (True,) + __self._options[1:]

177-198: Consider adding unit tests for the logging functionality.

The PR adds significant new functionality but no tests are visible in this changeset. Consider adding unit tests that verify:

  1. The logger works correctly with different log levels
  2. The integration with JAX's JIT works as expected
  3. The enable_logging function properly configures the logger

This will ensure the logging functionality remains robust as the codebase evolves.

Would you like me to suggest a basic structure for testing this logging functionality?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 00df966 and 4c85461.

📒 Files selected for processing (3)
  • pyproject.toml (1 hunks)
  • src/flowMC/logger/__init__.py (1 hunks)
  • src/flowMC/logger/_logger.py (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
src/flowMC/logger/__init__.py (1)
src/flowMC/logger/_logger.py (1)
  • enable_logging (177-198)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: build (3.12)
  • GitHub Check: build (3.11)
  • GitHub Check: pre-commit (3.12)
  • GitHub Check: pre-commit (3.11)
🔇 Additional comments (4)
pyproject.toml (1)

26-26: LGTM! Appropriate dependency addition for enhanced logging.

The addition of loguru with version constraint >=0.7.0 aligns perfectly with the PR objective of introducing enhanced logging capabilities. This is consistent with the project's dependency management approach.

src/flowMC/logger/__init__.py (1)

1-2: LGTM! Clean public API exposure.

The explicit imports provide a well-defined public interface for the logging functionality, following Python's best practices for package organization. This makes the API intuitive for users to access the logger and enable_logging function directly.

src/flowMC/logger/_logger.py (2)

1-199: LGTM! Well-implemented JAX-compatible logging module.

The implementation successfully creates a JAX-compatible logger by wrapping loguru functionality with jax.debug.callback. The code is well-structured with comprehensive documentation and examples.


13-14:

❓ Verification inconclusive

Consider thread safety for the global LOG variable.

The global LOG variable is modified by the enable_logging function. In multi-threaded environments, this could lead to race conditions. Consider using thread-local storage or another thread-safe approach if this logger might be used in multi-threaded contexts.


🏁 Script executed:

#!/bin/bash
# Check if this codebase uses threading or multiprocessing
echo "Checking for threading or multiprocessing usage in the codebase..."
rg -l "import threading|from threading|import multiprocessing|from multiprocessing" --type py

Length of output: 238


Review Change: Verify Thread Safety for Global LOG Variable

The current implementation of the LOG flag in src/flowMC/logger/_logger.py remains vulnerable to race conditions if it ever becomes subject to concurrent access (e.g., via the enable_logging function). Although an initial search for threading or multiprocessing usage yielded no results, this check may not be conclusive. Please manually verify whether this module is—or might be used in the future—as part of a multi-threaded context. If concurrent access is anticipated, consider refactoring the variable to use a thread-safe mechanism (such as thread-local storage or proper locking).

@kazewong
Copy link
Owner

kazewong commented Apr 6, 2025

loguru looks like an interesting package and thanks for bringing it to my attention!

While I am positive of integrating loguru into the verbose system, I am not exactly sure about whether we need the extra logger class you provide here. The main reason is I am quite allergic to have non-pure function in jax-transformer function in general. While the jax team is trying to keep external callback as innocuous as possible, external callbacks in transformed function still have a history of modifying the behavior of the function and return unexpected results. For this reason, I think we will keep logging outside any jax transform function.

This means the logging should go into strategy level but not resource level. I will have a look of this PR and start migrating some of these logging functionality into the main program, which I believe it should still be very beneficial

@Qazalbash
Copy link
Contributor Author

That seems reasonable, let me know if I can help you with that!

@Qazalbash
Copy link
Contributor Author

But what about the idea of checking gradients and other values at runtime? Is there any other way for it?

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.

2 participants