diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4d47d06..5944b0a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -40,6 +40,10 @@ jobs: - name: Download pub dependencies run: flutter pub get + - name: Download example pub dependencies + run: flutter pub get + working-directory: example + - name: Run analyzer run: flutter analyze diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c33c75..2ec28c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 6.0.1 + +- Add an example app. + # 6.0.0 - Bump flutter_bugfender version to 5.0.0 diff --git a/README.md b/README.md index 88136cf..cf771fe 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ A library helping integrate Bugfender with the [logging] package. +See the [example app][example] for a runnable version of the snippets below. + ## Usage ### Setup @@ -108,6 +110,7 @@ We are **top-tier experts** focused on Flutter Enterprise solutions. [build-badge]: https://img.shields.io/github/actions/workflow/status/leancodepl/logging_bugfender/test.yml?branch=master [build-badge-link]: https://github.com/leancodepl/logging_bugfender/actions/workflows/test.yml [logging]: https://pub.dev/packages/logging +[example]: https://github.com/leancodepl/logging_bugfender/tree/master/example [banner-img]: https://raw.githubusercontent.com/leancodepl/logging_bugfender/refs/heads/master/docs/imgs/banner.png [leancode-landing]: https://leancode.co/?utm_source=github.com&utm_medium=referral&utm_campaign=logging-bugfender [leancode-estimate]: https://leancode.co/get-estimate?utm_source=github.com&utm_medium=referral&utm_campaign=logging-bugfender diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml new file mode 100644 index 0000000..4af9cbc --- /dev/null +++ b/example/analysis_options.yaml @@ -0,0 +1 @@ +include: package:leancode_lint/analysis_options.yaml diff --git a/example/lib/main.dart b/example/lib/main.dart new file mode 100644 index 0000000..c9a1b77 --- /dev/null +++ b/example/lib/main.dart @@ -0,0 +1,108 @@ +// An example app showing how to wire `logging_bugfender` into a Flutter app. +// +// This directory ships without the platform folders – run `flutter create .` +// in it once before `flutter run`. + +import 'package:flutter/material.dart'; +import 'package:logging/logging.dart'; +import 'package:logging_bugfender/logging_bugfender.dart'; + +/// The key under which the signed in user is reported to Bugfender. +const usernameKey = 'username'; + +void main() { + // During debugging, you'll usually want to log everything. On production, + // `Level.INFO` and above is usually enough. + Logger.root.level = Level.ALL; + + final loggingListener = LoggingBugfenderListener( + 'my-very-secret-app-key', + // Logs are only sent to Bugfender by default – printing them to the + // console too is handy while debugging. + consolePrintStrategy: const PlainTextPrintStrategy(), + )..listen(Logger.root); + + runApp(ExampleApp(loggingListener: loggingListener)); +} + +class ExampleApp extends StatelessWidget { + const ExampleApp({super.key, required this.loggingListener}); + + final LoggingBugfenderListener loggingListener; + + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'logging_bugfender example', + home: ExamplePage(loggingListener: loggingListener), + ); + } +} + +class ExamplePage extends StatefulWidget { + const ExamplePage({super.key, required this.loggingListener}); + + final LoggingBugfenderListener loggingListener; + + @override + State createState() => _ExamplePageState(); +} + +class _ExamplePageState extends State { + /// Every class that logs should have its own [Logger] – its name is included + /// in the log message, so you always know where a record came from. + final _logger = Logger('ExamplePage'); + + bool _signedIn = false; + + /// Custom data is attached to every log sent from this device, which makes + /// it easy to tell whose session you're looking at in the Bugfender console. + Future _toggleSignIn() async { + final signedIn = _signedIn; + + try { + if (signedIn) { + await widget.loggingListener.removeCustomData(usernameKey); + } else { + await widget.loggingListener.setCustomData(usernameKey, 'jane.doe'); + } + } catch (err, st) { + // Both the error and the stack trace are sent to Bugfender. + _logger.severe('Failed updating the custom data', err, st); + return; + } + + _logger.info(signedIn ? 'Signed out' : 'Signed in'); + + if (mounted) { + setState(() => _signedIn = !signedIn); + } + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: const Text('logging_bugfender')), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + FilledButton( + onPressed: () => _logger.fine('The details button was tapped'), + child: const Text('Log at FINE'), + ), + FilledButton( + onPressed: () => _logger.warning('The cache is almost full'), + child: const Text('Log at WARNING'), + ), + const SizedBox(height: 16), + FilledButton( + onPressed: _toggleSignIn, + child: Text(_signedIn ? 'Sign out' : 'Sign in'), + ), + ], + ), + ), + ); + } +} diff --git a/example/pubspec.yaml b/example/pubspec.yaml new file mode 100644 index 0000000..1144152 --- /dev/null +++ b/example/pubspec.yaml @@ -0,0 +1,20 @@ +name: logging_bugfender_example +description: An example app showing how to integrate Bugfender with the logging package. +publish_to: none + +environment: + sdk: '>=3.3.0 <4.0.0' + flutter: '>=3.19.0' + +dependencies: + flutter: + sdk: flutter + logging: ^1.0.2 + logging_bugfender: + path: ../ + +dev_dependencies: + leancode_lint: '>=2.1.0 <2.2.0' + +flutter: + uses-material-design: true diff --git a/pubspec.yaml b/pubspec.yaml index edcfafc..31e88ce 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: logging_bugfender description: A library helping integrate Bugfender with the logging package. -version: 6.0.0 +version: 6.0.1 homepage: https://github.com/leancodepl/logging_bugfender environment: