logging_on_oslog 1.1.0 logging_on_oslog: ^1.1.0 copied to clipboard
Log using os_log on iOS or macOS so that you can check the logs using Console.app on a macOS machine.
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:logging_on_oslog/logging_on_oslog.dart';
import 'package:logging/logging.dart';
void main() {
runApp(const MyApp());
}
final appLogger = Logger('ExampleApp');
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late final StreamSubscription<LogRecord>? _logOnOSLogSubscription;
var _counter = 0;
@override
void initState() {
super.initState();
if (Platform.isIOS || Platform.isMacOS) {
_logOnOSLogSubscription = Logger.root.activateOsLog();
}
Logger.root.level = Level.INFO;
}
@override
void dispose() {
_logOnOSLogSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('logging_on_oslog example app'),
),
body: Center(
child: Text('Counter: $_counter'),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.plus_one),
onPressed: () {
setState(() {
_counter++;
});
// The first four messages will not appear in Console.app
// because the level of the logger is Level.INFO (see line 33).
appLogger.finest('finest! Value: $_counter');
appLogger.finer('finer! Value: $_counter');
appLogger.fine('fine! Value: $_counter');
appLogger.config('config! Value: $_counter');
// The next four messages will appear in Console.app.
appLogger.info('info! Value: $_counter');
appLogger.warning('warning! Value: $_counter');
appLogger.severe('severe! Value: $_counter');
appLogger.shout('shout! Value: $_counter');
},
),
),
);
}
}