Skip to content

Conversation

@Alexboiboi
Copy link
Member

Improved Logging Implementation

Overview

This PR refactors the logging system to follow Python library best practices using proper Loguru configuration.

Key Changes

🔧 Centralized Configuration

  • Moved hardcoded logger config from demag.py to dedicated logging_config.py
  • Added configure_logging() and disable_logging() functions

📚 Library-Friendly Defaults

  • Breaking Change: No log output by default (users must call configure_logging())
  • Follows Python library best practices - no forced output

⚙️ User Control

  • Configurable log levels, colors, timestamps, output destinations
  • Environment variable support (MAGPYLIB_LOG_LEVEL, etc.)
  • Named loggers with package hierarchy

🚀 Performance & Consistency

  • Replaced f-strings with structured logging
  • Standardized log patterns across all modules

Files Changed

New:

  • src/magpylib_material_response/logging_config.py - Centralized logging
  • docs/logging.md - User documentation

Modified: All core modules updated to use new logging system

Usage

Before (always logged):

apply_demag(collection)  # Forced output to stdout

After (user controlled):

# Default: silent
apply_demag(collection)  

# Enable when needed
from magpylib_material_response import configure_logging
configure_logging(level="INFO")
apply_demag(collection)  # Now shows progress

Testing

  • ✅ All 25 existing tests pass
  • ✅ No breaking changes to existing API
  • ✅ Library behavior unchanged for users who don't configure logging

Benefits

  • Library Best Practices: Silent by default, user-controlled output
  • Better Performance: Structured logging, lazy string formatting
  • Enhanced UX: Easy configuration, environment variable support
  • Maintainability: Centralized config, proper module hierarchy

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