By default, the logging package does not do anything useful with the log messages. You must configure the logging level and add a handler for the log messages.
Here is a simple logging configuration that logs all messages
via print
.
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.time}: ${rec.message}');
});
First, set the root [Level]. All messages at or above the level are sent to the [onRecord] stream.
Then, listen on the [onRecord] stream for [LogRecord] events. The [LogRecord] class has various properties for the message, error, logger name, and more.
Create a [Logger] with a unique name to easily identify the source of the log messages.
final Logger log = new Logger('MyClassName');
Here is an example of logging a debug message and an error:
var future = doSomethingAsync().then((result) {
log.fine('Got the result: $result');
processResult(result);
}).catchError((e, stackTrace) => log.severe('Oh noes!', e, stackTrace));
When logging more complex messages, you can pass a closure instead that will be evaluated only if the message is actually logged:
log.fine(() => [1, 2, 3, 4, 5].map((e) => e * 4).join("-"));
See the [Logger] class for the different logging methods.
0.10.0
. stackTrace
must be an instance of StackTrace
.
Use the Trace
class from the stack_trace package to convert strings.stackTrace
from StackTrace
to Object
.Added optional LogRecord.zone
field.
Record current zone (or user specified zone) when creating new LogRecord
s.
Add this to your package's pubspec.yaml file:
dependencies:
logging: ^0.11.1
You can install packages from the command line:
with pub:
$ pub get
Alternatively, your editor might support pub get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:logging/logging.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.11.3+2 | Jul 18, 2018 |
|
|
0.11.3+1 | Jul 18, 2016 |
|
|
0.11.3 | Apr 20, 2016 |
|
|
0.11.2 | Oct 1, 2015 |
|
|
0.11.1+1 | Aug 19, 2015 |
|
|
0.11.1 | May 7, 2015 |
|
|
0.11.0 | May 6, 2015 |
|
|
0.10.0 | Apr 10, 2015 |
|
|
0.9.3 | Jan 7, 2015 |
|
|
0.9.2 | Jul 25, 2014 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
98
|
Health:
Code health derived from static analysis.
[more]
|
--
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
--
|
Overall:
Weighted score of the above.
[more]
|
49
|
This package version is not analyzed, because it is more than two years old. Check the latest stable version for its analysis.
The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.
Running dartdoc
failed. (-10 points)
Make sure dartdoc
runs without any issues.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.5.0 <2.0.0 |