printx_logger 0.0.1
printx_logger: ^0.0.1 copied to clipboard
A powerful, extensible logging package for Flutter with advanced features like multiple sinks, custom formatters, filtering, and performance optimizations.
import 'package:flutter/material.dart';
import 'package:printx_logger/printx_logger.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Manually print colored text to verify ANSI support
print('\x1B[31mThis should be red\x1B[0m');
print('This should be normal');
print(PlatformUtils.supportsAnsiColors);
// Initialize PrintX (optional - it will auto-initialize with defaults)
await PrintX.initialize(
minimumLevel: LogLevel.info,
enableConsoleLogging: true,
enableFileLogging: false,
enableDeveloperLogging: true,
captureFlutterErrors: true,
enableColorOutput: true,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'PrintX Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'PrintX Logger Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
// Basic logging (like print)
PrintX('Counter incremented to $_counter');
// Different log levels
PrintX.info('Info: Button pressed, counter = $_counter');
PrintX.warn('Warning: Counter getting high!', tag: 'UI');
PrintX.debug('Debug: Counter is now $_counter');
if (_counter > 5) {
PrintX.error(
'Error: Counter exceeded 5!',
error: Exception('Counter too high'),
context: {'counter': _counter, 'threshold': 5},
);
}
// Lazy evaluation (expensive operations only run when needed)
PrintX.debugLazy(() {
// This expensive operation only runs if debug level is enabled
final expensiveCalculation = List.generate(
1000,
(i) => i * i,
).fold(0, (a, b) => a + b);
return 'Expensive calculation result: $expensiveCalculation';
});
// Specialized loggers
PrintX.api.info('API call completed successfully');
PrintX.ui.debug('Button animation started');
PrintX.network.warn('Slow network detected');
PrintX.auth.error(
'Authentication failed',
error: Exception('Invalid token'),
);
// Custom logger
final customLogger = PrintX.getLogger('CustomFeature');
customLogger.info(
'Custom feature executed',
tag: 'feature-x',
context: {'user_id': '123', 'session_id': 'abc'},
);
}
void _demonstrateErrorHandling() {
try {
throw Exception('This is a test exception');
} catch (error, stackTrace) {
PrintX.error(
'Caught an exception!',
error: error,
stackTrace: stackTrace,
tag: 'error-demo',
);
}
}
void _demonstrateContextLogging() {
PrintX.info(
'User action performed',
context: {
'user_id': '12345',
'action': 'button_press',
'timestamp': DateTime.now().toIso8601String(),
'session_duration': const Duration(minutes: 15).inSeconds,
'metadata': {'app_version': '1.0.0', 'platform': 'mobile'},
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _demonstrateErrorHandling,
child: const Text('Demonstrate Error Logging'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: _demonstrateContextLogging,
child: const Text('Demonstrate Context Logging'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () async {
final logs = await PrintX.exportLogs();
if (logs != null) {
PrintX.info('Logs exported to: $logs');
} else {
PrintX.warn('Could not export logs (web platform?)');
}
},
child: const Text('Export Logs'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () async {
await PrintX.clearLogs();
PrintX.info('All logs cleared');
},
child: const Text('Clear Logs'),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}