Rohith Gilla
Posted on August 31, 2024
Why do I need logging?
Imagine this: You’re sipping your coffee, basking in the satisfaction of a smooth Flutter app deployment. Everything seems perfect until that message pops up—your app has a bug in production. Panic sets in as you frantically sift through code, wishing you had a crystal ball to see exactly what your users are experiencing. What if there was a way to capture crucial data at the moment it happens, giving you instant insights into those elusive bugs?
Telemetry.sh
Enter telemetry.sh—the tool you didn’t know you needed, until now. With just a few lines of code, you can start logging and tracking user behaviours, errors, and more, right from your Flutter app. It’s as simple as logging:
final response = await telemetry.log('user_${uid}', data);
In this post, we’ll dive into how you can harness the power of telemetry.sh
Setup
Setting up Telemetry.sh in your dart/flutter project is very simple.
Step 0
- Register at Telemetry.sh
- Get your API key
Step 1
If dart only project
dart pub add telemetry_sh
If flutter project
flutter pub add telemetry_sh
Step 2
Initialize the TelemetrySh
instance with your API key
import 'package:telemetry_sh/telemetry_sh.dart';
final telemetry = TelemetrySh('YOUR_API_KEY');
Step 3
Log what you need :)
final response = await telemetry.log('table_name', data);
This tool behaves like a SQL database, where you can insert data in a table
and query on it.
Then you can query the data in the console
How I like to use the tool
Telemetry.sh is incredibly versatile, and its flexibility allows you to structure your logs in a way that makes sense for your application. Here’s how I like to set it up for various scenarios.
Structuring Your Logs with Custom Tables
One of the best features of Telemetry.sh is that it treats your logs like entries in a SQL database. This means you can create different “tables” for different logging contexts. For example, let’s say you want to track user activities, errors, and API interactions separately. You can structure your logs like this:
• User Activity Logs: Use a table structure like user_activity_${uid}
where uid
is the user’s unique identifier. This allows you to track all activities performed by a particular user. Each log entry can include detailed data such as timestamp, action performed, and any relevant metadata.
final activityData = <String,dynamic>{
'timestamp': DateTime.now().toIso8601String(),
'action': 'clicked_button',
'metadata': {'button_id': 'save_button'}
};
await telemetry.log('user_activity_${uid}', activityData);
• Error Logs: For error tracking, you can use a structure like error_logs
. Each entry can capture the error message, stack trace, and any additional context that might be helpful for debugging.
final errorData = <String,dynamic>{
'timestamp': DateTime.now().toIso8601String(),
'error': 'NullPointerException',
'stack_trace': errorStackTrace,
'user_id': uid
};
await telemetry.log('error_logs', errorData);
• API Interaction Logs: If your app makes numerous API calls, it’s useful to log these interactions in a separate table like api_interactions. Here, you can store the request details, response status, and time taken for the request. This can help you diagnose issues related to API latency or failures.
final apiData = <String,dynamic>{
'timestamp': DateTime.now().toIso8601String(),
'endpoint': '/api/user/profile',
'status': response.statusCode,
'response_time_ms': duration.inMilliseconds
};
await telemetry.log('api_interactions', apiData);
Incorporating a robust logging solution like Telemetry.sh into your Flutter app isn’t just a nice-to-have; it’s a game changer. By structuring your logs effectively and leveraging Telemetry.sh’s powerful querying capabilities, you can transform your logging data into actionable insights. Whether you’re tracking user behaviours, debugging errors, or monitoring API performance, this tool empowers you to stay ahead of potential issues and continuously improve your app’s stability and user experience.
So, as you sip your coffee and watch your app thrive in production, remember that with Telemetry.sh, you’re not just logging data—you’re building a stronger, more resilient application. Happy coding!
Useful links
https://github.com/Rohithgilla12/telemetry_sh
https://pub.dev/packages/telemetry_sh
P.S. While I don’t own Telemetry.sh, I’m a big fan of the product, so much so that I wrote the Dart SDK for it as an open-source project.
Posted on August 31, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.