When developing Flutter apps, proper logging is essential—not only for debugging during development but also for monitoring behavior in production. In this article, we’ll break down the available logging methods in Flutter and Dart, comparing print()
, debugPrint()
, the logger
package, and techniques to handle logs in production environments.
1. print() — The Basic Logger
The print()
function is the simplest way to output information to the console. It’s great for quick debugging but has limitations:
- Long messages may get truncated.
- No timestamp or log level.
- Always active—even in production builds.
2. debugPrint() — Smarter Debug Output
debugPrint()
addresses the truncation issue by ensuring that long messages are broken up properly. It also automatically disables itself in release mode (unless manually overridden), making it safer for development use.
3. Using the logger Package
For structured, readable, and configurable logs, the logger
package is highly recommended. It supports:
- Log levels: verbose, debug, info, warning, error, wtf
- Colorized console output
- Custom output options and filters
Example usage:
final logger = Logger();
logger.i("Info log");
logger.e("Error log", error, stackTrace);
4. Checking kReleaseMode for Production
When handling logs, it’s important to avoid exposing sensitive information in release builds. Dart’s kReleaseMode
constant (from foundation.dart
) lets you determine if the app is running in production:
if (!kReleaseMode) {
logger.d("This is a debug-only message");
}
5. Where Do Logs Go in Production?
In release mode, logs are still sent to the device’s system console, but users don’t see them. However, unless the logs are stored (e.g., in local files or sent to a remote logging service like Firebase Crashlytics), they are inaccessible for post-mortem debugging.
6. Best Practices
- Use
logger
for consistent log formatting. - Use
kReleaseMode
to conditionally log only in debug mode. - Never log sensitive user data.
- For production diagnostics, consider integrating tools like Firebase Crashlytics or Sentry.
Conclusion
Logging might feel simple at first, but choosing the right tool and configuration for your needs makes a huge difference—especially when bugs appear in production. A mix of debugPrint()
for development and the logger
package with release-mode awareness gives you a strong foundation.
Comment