the_logger 0.0.17 copy "the_logger: ^0.0.17" to clipboard
the_logger: ^0.0.17 copied to clipboard

A modular logging library for Flutter: supports multiple loggers, storage and filters

example/lib/main.dart

// ignore_for_file: public_member_api_docs

import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:the_logger/the_logger.dart';

void main() {
  TheLogger.i().init();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late final AppLifecycleListener _listener;

  @override
  void initState() {
    super.initState();
    _listener = AppLifecycleListener(
      onStateChange: _onStateChanged,
    );
  }

  @override
  void dispose() {
    _listener.dispose();

    super.dispose();
  }

  void _onStateChanged(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        TheLogger.i().startSession();
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
        break;
    }
  }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'TheLogger Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({super.key});

  final _log = Logger('MyHomePage');

  void printLogs() {
    _log
      ..finest('some finest log')
      ..finer('some finer log')
      ..fine('some fine log')
      ..config('some config log')
      ..info('some info log')
      ..warning('some warning log')
      ..severe('some severe log')
      ..shout('some shout log');
  }

  void emulateError() {
    try {
      throw Exception('Some error');
    } catch (e, s) {
      _log
        ..severe('Some severe error', e, s)
        ..shout('some shout error', e, s);
    }
  }

  void logJson() {
    _log.fine(
      '''Some JSON right in the message {"messageKey0": "value0","messageKey1": "value1","messageKey2": "value2"} that will be formatted''',
      '''Error strings can contain JSON too {"errorKey0": "value0","errorKey1": "value1","errorKey2": "value2"} and will be formatted''',
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ElevatedButton(
              onPressed: printLogs,
              child: const Text('Press me'),
            ),
            ElevatedButton(
              onPressed: emulateError,
              child: const Text('Emulate error'),
            ),
            ElevatedButton(
              onPressed: logJson,
              child: const Text('Log json'),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
160
pub points
59%
popularity

Publisher

verified publisherthenes.xyz

A modular logging library for Flutter: supports multiple loggers, storage and filters

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

collection, flutter, logging, path, path_provider, plugin_platform_interface, sqflite

More

Packages that depend on the_logger