Skip to content

Replace printStackTrace() and System.out calls with proper logging #50

@coderabbitai

Description

@coderabbitai

Overview

This issue addresses the need to replace all instances of printStackTrace() and System.out.println()/print() with proper logging using SLF4J Logger.

Implementation

The following instances need to be replaced:

printStackTrace() Occurrences

  1. src/main/java/botcommons/utilities/JsonUtils.java:60

    • Current: e.printStackTrace();
    • Replace with: logger.error("Failed to read cache file {}: {}", file.getName(), e.getMessage(), e);
  2. src/main/java/botcommons/utilities/JsonUtils.java:76

    • Current: e.printStackTrace();
    • Replace with: logger.error("Failed to write cache file {}: {}", file.getName(), e.getMessage(), e);
  3. src/main/java/botcommons/config/ConfigManager.java:77

    • Current: e.printStackTrace();
    • Replace with: logger.error("Failed to write config file for server {}: {}", serverId, e.getMessage(), e);
  4. src/main/java/botcommons/config/ConfigManager.java:106

    • Current: e.printStackTrace();
    • Replace with: logger.error("Error loading configuration for guild {}: {}", event.getGuild().getId(), e.getMessage(), e);
  5. src/main/java/botcommons/config/Config.java:32

    • Current: e.printStackTrace();
    • Replace with: logger.error("Failed to load configuration from file: {}", e.getMessage(), e);
  6. src/main/java/botcommons/config/Config.java:48

    • Current: e.printStackTrace();
    • Replace with: logger.error("Failed to write configuration to file: {}", e.getMessage(), e);
  7. src/main/java/botcommons/commands/ReplyContext.java:196

    • Current: e.printStackTrace();
    • Replace with: logger.error("Failed to execute menu action: {}", e.getMessage(), e);
  8. src/main/java/botcommons/commands/CommandManager.java:168

    • Current: e.printStackTrace();
    • Replace with: logger.error("Error during command execution: {}", e.getMessage(), e);
  9. src/main/java/botcommons/commands/CommandManager.java:194

    • Current: e.printStackTrace();
    • Replace with: logger.error("Error handling button event: {}", e.getMessage(), e);

System.out.println/print Occurrences

  1. src/main/java/botcommons/utilities/JsonUtils.java:57

    • Current: System.out.println("Loaded existing data from " + file.getName());
    • Replace with: logger.debug("Loaded existing data from {}", file.getName());
  2. src/main/java/botcommons/config/ConfigManager.java:69

    • Current: System.out.println("[ConfigManager] Successfully wrote config for server " + serverId);
    • Replace with: logger.info("Successfully wrote config for server {}", serverId);
  3. src/main/java/botcommons/commands/impl/ConfigCommand.java:55

    • Current: System.out.println("Yes");
    • Replace with: logger.debug("User confirmed config key creation");
  4. src/main/java/botcommons/commands/impl/ConfigCommand.java:61

    • Current: System.out.println("No");
    • Replace with: logger.debug("User declined config key creation");
  5. src/main/java/botcommons/commands/ReplyContext.java:175

    • Current: System.out.println("Timeout duration must be positive, defaulting to 1 second");
    • Replace with: logger.warn("Timeout duration must be positive, defaulting to 1 second");
  6. src/main/java/botcommons/commands/GenericCommandEvent.java:195

    • Current: System.out.println("Registered menu with id: " + id);
    • Replace with: logger.debug("Registered menu with id: {}", id);
  7. src/main/java/botcommons/cache/CacheManager.java:80

    • Current: System.out.printf("Loaded %d members for guild %s%n", members.size(), event.getGuild().getName());
    • Replace with: logger.info("Loaded {} members for guild {}", members.size(), event.getGuild().getName());

Guide for Implementation

  1. Add the following imports if not already present:

  2. Initialize the logger at the class level:

  3. Replace each occurrence with the suggested logger statement.

Best Practices

  • Use appropriate log levels:

    • ERROR: For errors that affect application functionality
    • WARN: For important issues that don't break functionality
    • INFO: For informational messages about application progress
    • DEBUG: For detailed debugging information
    • TRACE: For the most detailed information
  • Use parameterized logging (e.g., logger.info("Message {}", value);) instead of string concatenation to avoid unnecessary string creation when logging is disabled.

  • Include the exception object as the last parameter in error logs for stack trace information.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions