extensible logger
small, easy to use and extensible logger which prints beautiful logs.
inspired by logger for android.
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 extensible logger .
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 logoutput
s 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.
- add logger_flutter to your
pubspec.yaml
- add the following code into your widget tree
logconsoleonshake(
child: container() // your widgets
),
more documentation coming soon.
Comments are closed.