Flutter extension for the logging package.
This package provides a simple tool for logging messages in your applications and a set of additional utilities.
- Print logs to the console using a standard format.
- Send logs to 3rd party services (ie: Crashlytics, DataDog, etc.)
- Print class and method names where the log was triggered.
- View and share all logs from inside the app.
- Capture and format logging logs from 3rd party packages.
Use the Flogger static class to access all logging methods.
-
Initialize the logger.
Flogger.init();
-
Register a listener to print logs to the developer console.
if (kDebugMode){ Flogger.registerListener( (record) => log(record.printable(), stackTrace: record.stackTrace), ); }
Log messages with their severity using the following methods:
Flogger.d("Debug message");
Flogger.i("Info message");
Flogger.w("Warning message");
Flogger.e("Error message", stackTrace: null);
These calls will result in the logs below when using the default configuration:
[log] D/App SampleClass: Debug message
[log] I/App SampleClass: Info message
[log] W/App SampleClass: Warning message
[log] E/App SampleClass: Error message
Use the FloggerConfig class when initializing the Flogger to configure how logs are printed:
Flogger.init(config: FloggerConfig(...));
FloggerConfig({
// The name of the default logger
this.loggerName = "App",
// Print the class name where the log was triggered
this.printClassName = true,
// Print the method name where the log was triggered
this.printMethodName = false,
// Print the date and time when the log occurred
this.showDateTime = false,
// Print logs with Debug severity
this.showDebugLogs = true,
// Print logs with a custom format
// If set, ignores all other print options
final FloggerPrinter? printer,
});
Use the LogConsole class to view your logs inside the app.
-
Add logs to the console buffer by registering a new listener.
Flogger.registerListener( (record) => LogConsole.add( OutputEvent(record.level, [record.printable()]), bufferSize: 1000, // Remember the last X logs ), );
-
Open the logs console to view all recorded logs.
LogConsole.open(context)
Use the loggerName
parameter when adding logs to print them as a different logger. This can be useful for differentiating calls made from the different layers in your app. For example:
Flogger.i("Info message", loggerName: "Network");
Flogger.w("Warning message", loggerName: "Database");
Register additional listeners to send logs to different services, for example:
if (kReleaseMode) {
Flogger.registerListener((record) {
// Filter logs that may contain sensitive data
if(record.loggerName != "App") return;
if(record.message.contains("apiKey")) return;
if(record.message.contains("password")) return;
// Log to 3rd party services
FirebaseCrashlytics.instance.log(record.printable());
DatadogSdk.instance.logs?.info(record.printable());
});
}
Contributions are most welcome! Feel free to open a new issue or pull request to make this project better.
- Set the new version on the pubspec.yaml
version
field. - Update the CHANGELOG.md file documenting the changes.
- Update the README.md file if necessary.
- Run
dart doc
to update the documentation. - Run
dart pub publish --dry-run
to ensure the package can be published successfully. - Create a new tag with the release version
git tag -a x.y.z -m "x.y.z" && git push --tags
. - Navigate to GitHub Releases and create a new release for the previously created tag, including the CHANGELOG.md changes.
- Finally run
dart pub publish
to deploy the project.
- Logging - Copyright (c) 2013 the Dart project authors BSD 3-Clause for providing the logging framework this library depends on.
- Logger Flutter - Copyright (c) 2019 Simon Leier MIT License for creating the log console.
This repo is covered under the MIT License.