Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
simc committed Jun 4, 2019
0 parents commit 7d8c8eb
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.DS_Store
.dart_tool/
.idea/
.iml

.packages
.pub/

build/
ios/
android/
demo/

pubspec.lock
doc/
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 7a4c33425ddd78c54aba07d86f3f9a4a0051769b
channel: stable

project_type: package
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Example",
"request": "launch",
"type": "dart",
"program": "example/lib/main.dart"
}
]
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 0.4.0
- First version of the new logger
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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.
128 changes: 128 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Logger

[![Version](https://img.shields.io/pub/v/logger.svg)](https://pub.dartlang.org/packages/logger) ![Runtime](https://img.shields.io/badge/dart-%3E%3D2.1-brightgreen.svg) ![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)

Small, easy to use and extensible logger which prints beautiful logs.
Inspired by [logger](https://github.com/orhanobut/logger) for Android.

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

### Resources:
- [Documentation](https://pub.dartlang.org/documentation/logger/latest/logger/)
- [Pub Package](https://pub.dartlang.org/packages/logger)
- [GitHub Repository](https://github.com/leisim/logger)

## Getting Started

Just create an instance of `Logger` and start logging:
```dart
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

![](https://raw.githubusercontent.com/leisim/logger/master/art/screenshot.png)

## Log level

You can log with different levels:

```dart
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:

```dart
Logger.level = Level.warning;
```

This hides all `verbose`, `debug` and `info` log events.

## Options

When creating a logger, you can pass some options:

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

If you use the `PrettyPrinter`, there are more options:

```dart
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
),
)
```

## LogPrinter

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(Level level, dynamic message, dynamic error, StackTrace stackTrace) {
print(message);
}
}
```

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

## LogFilter

The `LogFilter` filters which logs should be shown and which don't.
The default implementation 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:
```dart
var filter = (Level level, dynamic message, dynamic error, StackTrace stackTrace) {
return true;
}
```
This will show all logs even in release mode. (**NOT** a good idea)

## 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.
```
Binary file added art/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/test
29 changes: 29 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:logger/logger.dart';

var logger = Logger();

var loggerNoStack = Logger(printer: PrettyPrinter());

void main() {
teesr();
}

void foo() {
logger.v("Log message with 2 methods");

logger.d("Log message with 2 methods", "MIST");

logger.i("Log message with 2 methods");

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

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

logger.wtf("Hello world.", "PROBLEM!", StackTrace.current);

logger.d({"key": 5, "value": "something"});
}

void teesr() {
foo();
}
11 changes: 11 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: example
description: Logger Example

version: 1.0.0

environment:
sdk: ">=2.1.0 <3.0.0"

dependencies:
logger:
path: ../
8 changes: 8 additions & 0 deletions lib/logger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Small, easy to use and extensible logger which prints beautiful logs.
library logger;

import 'dart:convert';
import 'package:ansicolor/ansicolor.dart';

part 'src/pretty_printer.dart';
part 'src/logger.dart';
106 changes: 106 additions & 0 deletions lib/src/logger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
part of logger;

/// [Level]s to control logging output. Logging can be enabled to include all
/// levels above certain [Level].
enum Level {
verbose,
debug,
info,
warning,
error,
wtf,
nothing,
}

/// `LogFilter` is called every time a new log message is sent and decides if
/// it will be printed or canceled.
///
/// The default implementation is [Logger._defaultFilter].
/// Every implementation should consider [Logger.level].
typedef LogFilter = bool Function(
Level level,
dynamic message, [
dynamic error,
StackTrace stackTrace,
]);

/// An abstract handler of log messages.
///
/// You can implement a `LogPrinter` from scratch or extend [PrettyPrinter].
abstract class LogPrinter {
/// Is called by the Logger for each log message.
void log(Level level, dynamic message, dynamic error, StackTrace stackTrace);
}

class Logger {
/// The current logging level of the app.
///
/// All logs with levels below this level will be omitted.
static Level level = Level.verbose;

final LogPrinter _printer;
final LogFilter _filter;

/// Create a new Logger.
///
/// You can provide a custom [printer] and [filter]. Otherwise the default
/// [PrettyPrinter] and [_defaultFilter] will be used.
Logger({LogPrinter printer, LogFilter filter})
: _printer = printer ?? PrettyPrinter(),
_filter = filter ?? _defaultFilter;

/// Log message at level [Level.verbose].
void v(dynamic message, [dynamic error, StackTrace stackTrace]) {
log(Level.verbose, message, error, stackTrace);
}

/// Log message at level [Level.debug].
void d(dynamic message, [dynamic error, StackTrace stackTrace]) {
log(Level.debug, message, error, stackTrace);
}

/// Log message at level [Level.info].
void i(dynamic message, [dynamic error, StackTrace stackTrace]) {
log(Level.info, message, error, stackTrace);
}

/// Log message at level [Level.warning].
void w(dynamic message, [dynamic error, StackTrace stackTrace]) {
log(Level.warning, message, error, stackTrace);
}

/// Log message at level [Level.error].
void e(dynamic message, [dynamic error, StackTrace stackTrace]) {
log(Level.error, message, error, stackTrace);
}

/// Log message at level [Level.wtf].
void wtf(dynamic message, [dynamic error, StackTrace stackTrace]) {
log(Level.wtf, message, error, stackTrace);
}

/// Log message with [level].
void log(Level level, dynamic message,
[dynamic error, StackTrace stackTrace]) {
if (_filter(level, message, error, stackTrace)) {
_printer.log(level, message, error, stackTrace);
}
}

/// Default implementation of [LogFilter]. All log
static bool _defaultFilter(
Level level,
dynamic message, [
dynamic error,
StackTrace stackTrace,
]) {
var shouldLog = false;
assert(() {
if (level.index >= Logger.level.index) {
shouldLog = true;
}
return true;
}());
return shouldLog;
}
}
Loading

0 comments on commit 7d8c8eb

Please sign in to comment.