Skip to content

Instantly share code, notes, and snippets.

@magillus
Last active November 2, 2018 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magillus/713575fae96ba06e3a107be8d84db904 to your computer and use it in GitHub Desktop.
Save magillus/713575fae96ba06e3a107be8d84db904 to your computer and use it in GitHub Desktop.
Fimber = Timber like logging for Flutter. May extend to different trees (like native channel logging (?) TBD)
import 'package:fimber/fimber.dart';
// initialization in main.dart
Fimber.addTree(DebugTree());
// usage:
Fimber.d("Test debug $value");
Fimber.de(exIo, "Error reading file: $path");
var log = FimberLog("TestTAG");
log.d("Test debug $value");
log.we(exIo, "Error reading file: $path");
/** example output:
11-01 19:21:51.797 4485-4588/com.test.example.mobile I/flutter: D TestPage.buildInnerItems.<ac>: List updated: count = 1
*/
library fimber;
/**
* Main static Fimber logging.
*/
class Fimber {
static we(Exception ex, String msg) {
log("W", msg, ex: ex);
}
static w(String msg) {
log("W", msg);
}
static de(Exception ex, String msg) {
log("D", msg, ex: ex);
}
static d(String msg) {
log("D", msg);
}
static ie(Exception ex, String msg) {
log("I", msg, ex: ex);
}
static i(String msg) {
log("I", msg);
}
static ee(Exception ex, String msg) {
log("E", msg, ex: ex);
}
static e(String msg) {
log("E", msg);
}
static log(String level, String msg,
{String tag = null, Exception ex = null}) {
_trees[level]?.forEach((logger) => logger.log(level, msg, ex: ex));
}
static addTree(LogTree tree) {
tree.getLevels().forEach((level) {
var logList = _trees[level];
if (logList == null) {
logList = List<LogTree>();
_trees[level] = logList;
}
logList.add(tree);
});
}
static Map<String, List<LogTree>> _trees = new Map<String, List<LogTree>>();
}
/**
* Debug log tree.
* Tag generation
*/
class DebugTree extends LogTree {
static List<String> DEFAULT = ["D", "I", "W", "E"];
@override
log(String level, String msg, {String tag: null, Exception ex: null}) {
var logTag = tag ?? getTag();
if (logTag != null) {
print("${level}\t${logTag}:\t ${msg} \n${ex?.toString() ?? ''}");
} else {
print("${level} ${msg} \n${ex?.toString() ?? ''}");
}
}
@override
List<String> getLevels() {
return DEFAULT;
}
}
/**
* Interface for LogTree
*/
abstract class LogTree {
log(String level, String msg, {String tag = null, Exception ex = null});
List<String> getLevels();
String getTag() {
var stackTraceList = StackTrace.current.toString().split('\n');
return stackTraceList[6].replaceFirst("<anonymous closure>", "<ac>").split(' ')[6]; // need better error handling
}
}
/**
* Stand alone logger with custom tag defined.
*/
class FimberLog {
String tag;
FimberLog(this.tag);
we(Exception ex, String msg) {
_log("W", tag, msg, ex: ex);
}
w(String msg) {
_log("W", tag, msg);
}
de(Exception ex, String msg) {
_log("D", tag, msg, ex: ex);
}
d(String msg) {
_log("D", tag, msg);
}
ie(Exception ex, String msg) {
_log("I", tag, msg, ex: ex);
}
i(String msg) {
_log("I", tag, msg);
}
ee(Exception ex, String msg) {
_log("E", tag, msg, ex: ex);
}
e(String msg) {
_log("E", tag, msg);
}
_log(String level, String tag, String msg, {Exception ex = null}) {
Fimber.log(level, msg, tag: tag, ex: ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment