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

[ADDED] Crashlytics tree for release builds #108

Merged
merged 1 commit into from
Jan 10, 2025
Merged

Conversation

hossain-khan
Copy link
Owner

This pull request introduces a new custom logging mechanism to the WeatherAlertApp by integrating Crashlytics for production builds and refactoring the existing logging setup. The key changes include the addition of a custom CrashlyticsTree class and the refactoring of the logging tree installation process.

Logging Mechanism Enhancement:

@hossain-khan hossain-khan self-assigned this Jan 10, 2025
@Copilot Copilot bot review requested due to automatic review settings January 10, 2025 21:57

Choose a reason for hiding this comment

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

Copilot wasn't able to review any files in this pull request.

Files not reviewed (2)
  • app/src/main/java/dev/hossain/weatheralert/WeatherAlertApp.kt: Language not supported
  • app/src/main/java/dev/hossain/weatheralert/util/CrashlyticsTree.kt: Language not supported
@hossain-khan
Copy link
Owner Author

ChatGPT

Yes, you can plant a custom Timber.Tree in your Android app that forwards error logs from Timber to Crashlytics. This approach allows you to integrate Timber's logging mechanism with Crashlytics seamlessly.

Here’s how you can implement a custom Timber.Tree for Crashlytics:

Step 1: Create a Custom Timber.Tree

import com.google.firebase.crashlytics.FirebaseCrashlytics
import timber.log.Timber

class CrashlyticsTree : Timber.Tree() {

    override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
        // Log only ERROR or higher severity messages to Crashlytics
        if (priority >= Log.ERROR) {
            FirebaseCrashlytics.getInstance().apply {
                // Log the message
                log(message)

                // Record the exception if available
                t?.let { recordException(it) }
            }
        }
    }
}

Step 2: Plant the Custom Tree in the Production Build

In your Application class, plant the CrashlyticsTree in the onCreate method for release builds:

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()

        if (BuildConfig.DEBUG) {
            // Plant a debug tree for development builds
            Timber.plant(Timber.DebugTree())
        } else {
            // Plant the custom Crashlytics tree for production builds
            Timber.plant(CrashlyticsTree())
        }
    }
}

Step 3: Use Timber for Logging

Use Timber.e(), Timber.w(), or any other logging methods in your codebase. For example:

Timber.e("This is an error message")
Timber.e(Exception("This is an exception"))

What Happens?

  • For debug builds, logs are printed to the logcat using DebugTree.
  • For release builds, only error and above severity logs are sent to Crashlytics along with any associated exceptions.

Additional Tips

  1. Custom Tags: If you want to include custom tags, ensure they are meaningful since CrashlyticsTree can capture them in the tag parameter.
  2. User Information: Consider adding user-specific information to Crashlytics logs using setCustomKey if applicable.
  3. Suppress Non-Critical Logs: To avoid cluttering Crashlytics with unnecessary logs, adjust the log levels and filter non-critical logs.

This setup ensures efficient logging during development while leveraging Crashlytics for error tracking in production.

@hossain-khan
Copy link
Owner Author

Gemini

Yes, you can create a custom Timber tree to send error logs to Crashlytics in your production Android app. Here's how you can do it:

1. Set up Crashlytics:

2. Create a custom Timber Tree:

import android.util.Log;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import timber.log.Timber;

public class CrashlyticsTree extends Timber.Tree {

    private final FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();

    @Override
    protected void log(int priority, String tag, String message, Throwable t) {
        if (priority == Log.VERBOSE || priority == Log.DEBUG || priority == Log.INFO) {
            return; // Don't log VERBOSE, DEBUG, INFO in Crashlytics
        }

        if (t != null) {
            crashlytics.recordException(t);
        } else {
            crashlytics.log(message);
        }
    }
}

3. Plant the tree in your Application class:

import android.app.Application;
import timber.log.Timber;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        if (!BuildConfig.DEBUG) { // Only plant the CrashlyticsTree in production
            Timber.plant(new CrashlyticsTree());
        } else {
            Timber.plant(new Timber.DebugTree()); // Use DebugTree for development
        }
    }
}

Explanation:

  • This code defines a custom CrashlyticsTree that extends Timber.Tree.
  • The log() method filters log messages based on priority, ignoring VERBOSE, DEBUG, and INFO levels.
  • If there's a Throwable (exception), it's reported to Crashlytics using recordException(). Otherwise, the log message is sent using log().
  • In your Application class, this tree is planted only for production builds, ensuring that error logs are sent to Crashlytics while allowing you to use the regular DebugTree for development.

Important considerations:

  • Filtering: Adjust the filtering logic in log() to match your needs. You might want to include WARN logs or use different criteria.
  • Performance: Be mindful of the volume of logs you send to Crashlytics. Excessive logging can impact app performance.
  • Privacy: Avoid logging sensitive user data to Crashlytics.
  • Context: Consider adding more context to your Crashlytics logs, such as user IDs or relevant application states, to help with debugging. You can use crashlytics.setCustomKeys() for this purpose.

By following these steps, you can effectively use Timber to send error logs to Crashlytics in your production Android app, improving your ability to track and fix crashes.

@hossain-khan hossain-khan merged commit 876d501 into main Jan 10, 2025
3 checks passed
@hossain-khan hossain-khan deleted the crashlytics-tree branch January 10, 2025 22:00
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.

1 participant