Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Save logging to cache directory #196

Merged
merged 3 commits into from
Jan 2, 2025
Merged

Conversation

PartyDonut
Copy link
Collaborator

Pull Request Description

Save logs to a .json file in the cache directory and load on application start as to preserve any crashes.

@PartyDonut PartyDonut added the feature New feature or request label Jan 2, 2025
Copy link

sourcery-ai bot commented Jan 2, 2025

Reviewer's Guide by Sourcery

This pull request implements saving logs to the cache directory and loading them on application start.

Sequence diagram for error log persistence flow

sequenceDiagram
    participant App
    participant CrashLogNotifier
    participant FileSystem
    participant Logger

    App->>CrashLogNotifier: init()
    activate CrashLogNotifier
    CrashLogNotifier->>FileSystem: getApplicationCacheDirectory()
    FileSystem-->>CrashLogNotifier: directory
    CrashLogNotifier->>FileSystem: read crash_logs.json
    FileSystem-->>CrashLogNotifier: stored logs
    CrashLogNotifier->>CrashLogNotifier: initialize logger
    deactivate CrashLogNotifier

    Note over CrashLogNotifier,FileSystem: When new error occurs

    Logger->>CrashLogNotifier: logPrint(record)
    activate CrashLogNotifier
    CrashLogNotifier->>CrashLogNotifier: update state
    CrashLogNotifier->>FileSystem: save to crash_logs.json
    deactivate CrashLogNotifier
Loading

Class diagram showing the refactoring of error logging model

classDiagram
    class ErrorType {
        <<enumeration>>
        severe
        warning
        shout
    }

    class ErrorLogModel {
        +ErrorType type
        +String message
        +DateTime time
        +StackTrace? stackTrace
        +String label
        +String content
        +String clipBoard
        +Color color
        +fromLogRecord(LogRecord) ErrorLogModel
        +toJson() Map~String,dynamic~
        +fromJson(Map~String,dynamic~) ErrorLogModel
    }

    class CrashLogNotifier {
        -List~ErrorLogModel~ state
        -Logger logger
        -int maxLength
        -String? logFilePath
        +init() Future~void~
        -initializeLogFile() Future~void~
        -loadLogsFromFile() Future~void~
        -saveLogsToFile() Future~void~
        +clearLogs() void
        +logPrint(LogRecord) void
        +logFile(FlutterErrorDetails) void
    }

    ErrorLogModel -- ErrorType
    CrashLogNotifier --> ErrorLogModel : manages

    note for ErrorLogModel "Extracted from ErrorViewModel
Added JSON serialization"
    note for CrashLogNotifier "Added file persistence
Reduced max length to 50"
Loading

File-Level Changes

Change Details Files
Logs are saved to a JSON file in the cache directory and loaded when the application starts.
  • Added _initializeLogFile to get the cache directory path and store it in logFilePath.
  • Added _loadLogsFromFile to read and parse logs from the JSON file.
  • Added _saveLogsToFile to serialize and save logs to the JSON file.
  • Modified init to call _initializeLogFile and _loadLogsFromFile.
  • Modified clearLogs to call _saveLogsToFile.
  • Modified logPrint to call _saveLogsToFile.
  • Updated the state type to List<ErrorLogModel>.
lib/providers/crash_log_provider.dart
Created a new model class ErrorLogModel to represent error logs.
  • Created ErrorLogModel with fields for type, message, time, and stack trace.
  • Added methods to convert between ErrorLogModel and LogRecord.
  • Added methods to serialize and deserialize ErrorLogModel to/from JSON.
  • Added helper methods for formatting log messages and colors based on log type.
lib/models/error_log_model.dart
Updated the UI to use the new ErrorLogModel.
  • Updated imports to use ErrorLogModel.
  • Updated UI elements to display data from ErrorLogModel.
  • Updated the crashLogProvider type to use ErrorLogModel.
  • Removed unused code related to the old error handling logic.
  • Adjusted padding in detail_scaffold.dart to accommodate the app bar.
lib/screens/crash_screen/crash_screen.dart
lib/main.dart
lib/screens/shared/detail_scaffold.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @PartyDonut - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding error handling around file operations in _loadLogsFromFile() and _saveLogsToFile() to gracefully handle I/O failures
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Review instructions: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


void init() {
Future<void> init() async {
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): The async init() method is called from the constructor without being awaited, which could lead to race conditions

Consider making the constructor private and providing a static factory method that properly awaits initialization

}
}

Future<void> _saveLogsToFile() async {
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): File operations should include error handling for I/O exceptions

Add try-catch blocks to handle potential file system errors and implement appropriate error recovery

@PartyDonut PartyDonut merged commit 1a42be4 into develop Jan 2, 2025
1 check passed
@PartyDonut PartyDonut deleted the feature/save-logging branch January 2, 2025 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant