flutter_logs 2.0.6 copy "flutter_logs: ^2.0.6" to clipboard
flutter_logs: ^2.0.6 copied to clipboard

outdated

An extensive filebased logging framework for flutter apps. (ELK Stack Supported)

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_logs/flutter_logs.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _my_log_file_name = "MyLogFile";
  var toggle = false;

  @override
  void initState() {
    super.initState();
    setUpLogs();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter Logs'),
        ),
        body: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                onPressed: () async {
                  setUpLogs();
                  //doSetupForELKSchema();
                  //doSetupForMQTT();
                },
                child: Text('Setup Logs', style: TextStyle(fontSize: 20)),
              ),
              RaisedButton(
                onPressed: () async {
                  logData(isException: false);
                },
                child: Text('Log Something', style: TextStyle(fontSize: 20)),
              ),
              RaisedButton(
                onPressed: () async {
                  logData(isException: true);
                },
                child: Text('Log Exception', style: TextStyle(fontSize: 20)),
              ),
              RaisedButton(
                onPressed: () async {
                  logToFile();
                },
                child: Text('Log To File', style: TextStyle(fontSize: 20)),
              ),
              RaisedButton(
                onPressed: () async {
                  printAllLogs();
                },
                child: Text('Print All Logs', style: TextStyle(fontSize: 20)),
              ),
              RaisedButton(
                onPressed: () async {
                  exportAllLogs();
                },
                child: Text('Export All Logs', style: TextStyle(fontSize: 20)),
              ),
              RaisedButton(
                onPressed: () async {
                  printFileLogs();
                },
                child: Text('Print File Logs', style: TextStyle(fontSize: 20)),
              ),
              RaisedButton(
                onPressed: () async {
                  exportFileLogs();
                },
                child: Text('Export File Logs', style: TextStyle(fontSize: 20)),
              ),
              RaisedButton(
                onPressed: () async {
                  FlutterLogs.clearLogs();
                },
                child: Text('Clear Logs', style: TextStyle(fontSize: 20)),
              )
            ],
          ),
        ),
      ),
    );
  }

  void setUpLogs() async {
    print("setUpLogs: Setting up logs..");
    await FlutterLogs.initLogs(
        logLevelsEnabled: [
          LogLevel.INFO,
          LogLevel.WARNING,
          LogLevel.ERROR,
          LogLevel.SEVERE
        ],
        timeStampFormat: TimeStampFormat.TIME_FORMAT_READABLE,
        directoryStructure: DirectoryStructure.FOR_DATE,
        logTypesEnabled: [_my_log_file_name],
        logFileExtension: LogFileExtension.LOG,
        logsWriteDirectoryName: "MyLogs",
        logsExportDirectoryName: "MyLogs/Exported",
        debugFileOperations: true,
        isDebuggable: true);
  }

  void doSetupForELKSchema() async {
    await FlutterLogs.setMetaInfo(
      appId: "flutter_logs_example",
      appName: "Flutter Logs Demo",
      appVersion: "1.0",
      language: "en-US",
      deviceId: "00012",
      environmentId: "7865",
      environmentName: "dev",
      organizationId: "5767",
      organizationUnitId: "5767",
      userId: "883023-2832-2323",
      userName: "umair13adil",
      userEmail: "m.umair.adil@gmail.com",
      deviceSerial: "YJBKKSNKDNK676",
      deviceBrand: "LG",
      deviceName: "LG-Y08",
      deviceManufacturer: "LG",
      deviceModel: "989892BBN",
      deviceSdkInt: "26",
      deviceBatteryPercent: "27",
      latitude: "55.0",
      longitude: "-76.0",
      labels: "",
    );
  }

  void doSetupForMQTT() async {
    await FlutterLogs.initMQTT(
        topic: "",
        brokerUrl: "",
        //Add URL without schema
        certificate: "assets/m2mqtt_ca.crt",
        port: "8883",
        writeLogsToLocalStorage: true,
        debug: true,
        initialDelaySecondsForPublishing: 10);
  }

  void logData({bool isException}) {
    if (!isException) {
      FlutterLogs.logThis(
          tag: 'MyApp',
          subTag: 'logData',
          logMessage:
              'This is a log message: ${DateTime.now().millisecondsSinceEpoch}',
          level: LogLevel.INFO);
    } else {
      try {
        if (toggle) {
          toggle = false;
          var i = 100 ~/ 0;
          print("$i");
        } else {
          toggle = true;
          var i = null;
          print(i * 10);
        }
      } catch (e) {
        if (e is Error) {
          FlutterLogs.logThis(
              tag: 'MyApp',
              subTag: 'Caught an error.',
              logMessage: 'Caught an exception!',
              error: e,
              level: LogLevel.ERROR);
        } else {
          FlutterLogs.logThis(
              tag: 'MyApp',
              subTag: 'Caught an exception.',
              logMessage: 'Caught an exception!',
              exception: e,
              level: LogLevel.ERROR);
        }
      }
    }
  }

  void logToFile() {
    FlutterLogs.logToFile(
        logFileName: _my_log_file_name,
        overwrite: false,
        //If set 'true' logger will append instead of overwriting
        logMessage:
            "This is a log message: ${DateTime.now().millisecondsSinceEpoch}, it will be saved to my log file named: \'$_my_log_file_name\'",
        appendTimeStamp: true); //Add time stamp at the end of log message
  }

  void printAllLogs() {
    FlutterLogs.printLogs(
        exportType: ExportType.ALL, decryptBeforeExporting: true);
  }

  void exportAllLogs() {
    FlutterLogs.exportLogs(
        exportType: ExportType.ALL, decryptBeforeExporting: true);
  }

  void exportFileLogs() {
    FlutterLogs.exportFileLogForName(
        logFileName: _my_log_file_name, decryptBeforeExporting: true);
  }

  void printFileLogs() {
    FlutterLogs.printFileLogForName(
        logFileName: _my_log_file_name, decryptBeforeExporting: true);
  }
}
96
likes
0
pub points
95%
popularity

Publisher

verified publisherumairadil.com

An extensive filebased logging framework for flutter apps. (ELK Stack Supported)

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_logs