logger 0.7.0+2

Logger #

Travis Version Runtime GitHub license

Small, easy to use and extensible logger which prints beautiful logs.
Inspired by logger for Android.

Show some ❤️ and star the repo to support the project

Resources: #

Getting Started #

Just create an instance of Logger and start logging:

var logger = Logger();

logger.d("Logger is working!");

Instead of a string message, you can also pass other objects like List, Map or Set.

Output #

Log Console #

If you are creating a Flutter app, you can use the logger_flutter extension. Shake the phone to show an on device console.

Documentation #

Log level #

You can log with different levels:

logger.v("Verbose log");

logger.d("Debug log");

logger.i("Info log");

logger.w("Warning log");

logger.e("Error log");

logger.wtf("What a terrible failure log");

To show only specific log levels, you can set:

Logger.level = Level.warning;

This hides all verbose, debug and info log events.

Options #

When creating a logger, you can pass some options:

var logger = Logger(
  filter: null, // Use the default LogFilter (-> only log in debug mode)
  printer: PrettyPrinter(), // Use the PrettyPrinter to format and print log
  output: null, // Use the default LogOutput (-> send everything to console)
);

If you use the PrettyPrinter, there are more options:

var logger = Logger(
  printer: PrettyPrinter(
    methodCount: 2, // number of method calls to be displayed
    errorMethodCount: 8, // number of method calls if stacktrace is provided
    lineLength: 120, // width of the output
    colors: true, // Colorful log messages
    printEmojis: true, // Print an emoji for each log message
    printTime: false // Should each log print contain a timestamp
  ),
)

LogFilter #

The LogFilter decides which log events should be shown and which don't.
The default implementation (DebugFilter) shows all logs with level >= Logger.level while in debug mode. In release mode all logs are omitted.

You can create your own LogFilter like this:

class MyFilter extends LogFilter {
  @override
  bool shouldLog(LogEvent event) {
    return true;
  }
}

This will show all logs even in release mode. (NOT a good idea)

LogPrinter #

The LogPrinter creates and formats the output, which is then sent to the LogOutput.
You can implement your own LogPrinter. This gives you maximum flexibility.

A very basic printer could look like this:

class MyPrinter extends LogPrinter {
  @override
  void log(LogEvent event) {
    println(event.message);
  }
}

Important: Every implementation has to send its output using the println() method.

If you created a cool LogPrinter which might be helpful to others, feel free to open a pull request. :)

LogOutput #

LogOutput sends the log lines to the desired destination.
The default implementation (ConsoleOutput) send every line to the system console.

class ConsoleOutput extends LogOutput {
  @override
  void output(OutputEvent event) {
    for (var line in event.lines) {
      print(line);
    }
  }
}

Possible future LogOutputs could send to a file, firebase or to Logcat. Feel free to open pull requests.

logger_flutter extension #

The logger_flutter package is an extension for logger. You can add it to any Flutter app. Just shake the phone to show the console.

  1. Add logger_flutter to your pubspec.yaml
  2. Add the following code into your widget tree
LogConsoleOnShake(
  child: Container() // Your widgets
),

More documentation coming soon.

MIT License #

Copyright (c) 2019 Simon Leier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0.7.0+1 #

  • Added ProductionFilter, FileOutput, MemoryOutput, SimplePrinter
  • Breaking: Changed LogFilter, LogPrinter and LogOutput

0.6.0 #

  • Added option to output timestamp
  • Added option to disable color
  • Added LogOutput
  • Behaviour change of LogPrinter
  • Remove dependency

0.5.0 #

  • Added emojis
  • LogFilter is a class now

0.4.0 #

  • First version of the new logger

example/lib/main.dart

import 'package:logger/logger.dart';

var logger = Logger(
  printer: PrettyPrinter(),
);

var loggerNoStack = Logger(
  printer: PrettyPrinter(methodCount: 0),
);

void main() async {
  demo();
}

void demo() {
  logger.d("Log message with 2 methods");

  loggerNoStack.i("Info message");

  loggerNoStack.w("Just a warning!");

  logger.e("Error! Something bad happened", "Test Error");

  loggerNoStack.v({"key": 5, "value": "something"});

  print("Test\nTest2");
}

Use this package as a library

1. Depend on it

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


dependencies:
  logger: ^0.7.0+2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:logger/logger.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
95
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Aug 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, other

Primary library: package:logger/logger.dart with components: io.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
Dev dependencies
pedantic ^1.8.0
test any