Code Monkey home page Code Monkey logo

mylogger's Introduction

MyLogger

MyLogger is improved fork of Flogs package developed in flutter that provides quick & simple logging solution.
All logs are saved into DB which can be exported as a text file.

Overview

MyLogger is written in Dart.
Logs are saved into Sembast database which can be exported into document directory and uploaded into server.

Logs are helpful when developer wants to analyze user activities within the app. Many times we want to log a set of data to analyze certain activity. For example:

  • Location (GPS Coordinates),
  • Device info,
  • Network requests
  • etc..

This helps us to quickly identify and fix issues that are hard to debug when app is in the production.
MyLogger provide functionality for log these data sets into database and fetch it by different filters.

Features

  1. Log messages by various levels (DEBUG, TRACE, INFO, WARNING, ERROR, SEVERE, FATAL)
  2. Save logs into database
  3. Export logs into file
  4. Fetch or delete logs easily
  5. Log filtering support
  6. Custom timestamps support
  7. Custom data type logging support
  8. Custom log format support
  9. Encryption support

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  my_logger: ^1.0.2

2. Install it

You can install packages from the command line:

with Flutter

$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:my_logger/logger.dart';

How to use

Log files are exported on storage directory so it's very important to add these permissions to your project's manifest file first.

Android

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

iOS

<key>NSPhotoLibraryAddUsageDescription</key>
<string>MyLogger would like to save photos from the app to your gallery</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>MyLogger would like to access your photo gallery for uploading images to the app</string>

To save logs, simply call any of the method mentioned below:

    MyLogger.trace("My trace log"); 

    MyLogger.debug("My debug log");

    MyLogger.info("My info log");

    MyLogger.warning("My warning log");

    MyLogger.error("My error log");

    MyLogger.severe("My severe log");

    MyLogger.fatal("My fatal log");

    MyLoggerlog(
      className: "HomePage",
      methodName: "_buildRow1",
      text: "My severe log with exception and stacktrace",
      type: LogLevel.SEVERE, 
      exception: Exception("This is an Exception!"),
      stacktrace: StackTrace.current,
    );

    MyLoggerlog(
      className: "HomePage",
      methodName: "_buildRow1",
      text: "My severe log with dataLogType",
      type: LogLevel.SEVERE,
      dataLogType: DataLogType.DEVICE,
    );

Available Methods

MyLogger provide many other methods for save, filter or fetch logs. Below is list of all this methods:

Get logs:

MyLogger.logs.getAll();   // Get all saved logs
MyLogger.logs.getLastHour();   // Get all saved logs for last hour

// Get logs by LogFilter:
MyLogger.logs.getByFilter(
  LogFilter(
    startDateTime: DateTime(2019), 
    endDateTime: DateTime(2020), 
    dataLogsType: [DataLogType.NETWORK],
    logLevels: [LogLevel.ERROR, LogLevel.WARNING],
  ),  
);

// LogFilters also have some named constructors:
MyLogger.logs.getByFilter(LogFilter.last24Hours());

Write logs:

MyLogger.logs.write(Log log);  // Save your own Log object

Delete logs:

MyLogger.logs.deleteAll();   // Delete all saved logs
MyLogger.logs.deleteLastHour();   // Delete all saved logs for last hour

// Delete logs by LogFilter:
MyLogger.logs.deleteByFilter(
  LogFilter(
    startDateTime: DateTime(2019), 
    endDateTime: DateTime(2020), 
    dataLogsType: [DataLogType.NETWORK],
    logLevels: [LogLevel.ERROR, LogLevel.WARNING],
  ),  
);

Export logs:

File fileExport = await MyLogger.logs.export(
  fileName: "export-all-logs",
  exportType: FileType.TXT,
  filter: LogFilter.last24Hours(),
);

Change configuration:

    LogConfig config = MyLogger.config
      ..outputFormat = "{{level}} {{time}} - {{message}}"
      ..dataLogTypeValues = DataLogType.values
      ..encryption = EncryptionType.XXTEA
      ..encryptionKey = encryptionKey
      ..timestampFormat = TimestampFormat.DEFAULT;

    MyLogger.applyConfig(config);

mylogger's People

Contributors

bierbaumtim avatar champ96k avatar martingeorgiu avatar mjablecnik avatar olo95 avatar rodydavis avatar slc1973 avatar tusharnikam-htp avatar uur5662 avatar zubairehman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mylogger's Issues

Best way to display logs

Hello,

I would like to display the logs not export them, what do you suggest is the best way to display them in the app in a page of it's own?

Bad idea to use in production on every function?

I love this package and i want to use it to print all my functions and important events in production regardless. Will this have a significant impact on my app you think?

I am asking instead of testing it first because it will be a few days of intense work and i would like to know if this is a good idea or not before i venture down that path.

Thanks for your insight.

Nothing in the logs

Hello there,

this is strange, i am print logs and then when i look at my log files there is nothing there.

	MyLogger.debug('TEST DELETE THIS NOW');
		MyLogger.debug('TEST DELETE THIS NOW');
		MyLogger.debug('TEST DELETE THIS NOW');
		MyLogger.debug('TEST DELETE THIS NOW');
		MyLogger.debug('TEST DELETE THIS NOW');
		
		File logsExport = await MyLogger.logs.export(
			fileName: 'all-logs',
			exportType: FileType.TXT,
			filter: LogFilter.last24Hours(),
		);
		
		print(logsExport.path);
		

Compatibility with android 13+

Hi,
Does this code even work in Android 13?

    } else {
      directory = await getExternalStorageDirectory();
    }

in Logexporter. As we know Android 13 won't allow access to external storage

Web support

Web support would be a fantastic addition to this package.

Currently, the initialization fails on getApplicationSupportDirectory call in LogsDatabase, which lacks web implementation.
Fortunately, sembast works fine on the web. It just needs to be initialized differently.

What else needs an update?

AFAICT the core functionality (logging, filtering, retrieving logs) should work fine without any changes except the sembast initialization.

Best way to assess log file size

Hello,

I was wondering if there was a way to figure out the log file size. I would like to upload the log file once it reaches a certain size but not quite sure how to go about that.

Cheers and thanks again for a wonderful package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.