A simple tool for beautiful and colorful console prints.
- Log in multiple colors
- Log texts
- Log Maps
- Log Error's/stacktrace.
- Log Objects
- Determine logs max horizontal lenght
In the pubspec.yaml
of your flutter project, add the following dependency:
dependencies:
...
sandbox_logger:
Import it:
import 'package:sandbox_logger/sandbox_logger.dart';
Use it:
// Call the log function
SandLog.text('Hello world');
class Person {
final String name; final int age; final bool isAProgrammer;
const Person(this.name, this.age, this.isAProgrammer);
// Needs to have a toString like this:
@override
String toString() => 'Person(name: $name, age: $age, isAProgrammer: $isAProgrammer)';
}
final map1 = {'name': 'Igor', 'age': 22};
final map2 = {'name': 'Daniel', 'age': 38};
final StackTrace stackTrace = StackTrace.fromString(fakeStackText);
// Building a log:
SandLog
.text('This is a header text')
.error('FormatException: Invalid double', stackTrace)
.map( map1 )
.listOfMap( [ map1 , map2 ] )
.object( Person('Igor', 22, true) )
.listOfObject( [ Person('Igor', 22, true) , Person('Daniel', 38, true) ] );
SandLog.text('This is a test text\nLets see the result');
final map = {'name': 'Igor', 'age': 22};
SandLog.map(map);
final map1 = {'name': 'Igor', 'age': 22};
final map2 = {'name': 'Daniel', 'age': 38};
SandLog.listOfMap([map1, map2]);
try {
double.parse('text impossible to parse');
} catch (error, stackTrace) {
SandLog.error(error, stackTrace);
}
A object that has a .toString()
with this pattern:
class Person {
final String name;
final int age;
final bool isAProgrammer;
const Person(this.name, this.age, this.isAProgrammer);
// Needs to be a toString like this:
@override
String toString() => 'Person(name: $name, age: $age, isAProgrammer: $isAProgrammer)';
}
That pattern of the "toString()" above can be casted to a map that will be logged
final person1 = Person('Igor', 22, true);
final person2 = Person('Daniel', 38, true);
SandLog.object(person1); // Log 1 object
SandLog.listOfObject([person1, person2]); // Log a list of object
This setting will only apply to this log. It is not a global/default setting as we will see below
SandLog
.text('Test of color & max horizontal lenght setters')
.setColorTo(LogColor.green) // Choose the desired color
.setMaxLength(30); // Set the max horizontal lenght
Recomended to set this in the main function of the project
// This configuration will be valid to all logs
SandLog.setLogConfiguration(LogConfigurations(
...
defaultMaxHorizontalLenght: 40,
defaultColor: LogColor.yellow,
defaultErrorColor: LogColor.magenta,
));
final LogBorderSytle myCustomBorderStyle = LogBorderSytle(
topRightBorder: '╗',
topLeftBorder: '╔',
traceBorder: '═',
middleRightBorder: '╠',
middleLeftBorder: '╣',
connectorLeftBorder: '╠',
connectorRightBorder: '╣',
bottomRightBorder: '╝',
bottomLeftBorder: '╚',
);
SandLog.setDefaultLogConfiguration(LogConfigurations(
...
defaultBorderStyle: myCustomBorderStyle,
));
SandLog.setDefaultLogConfiguration(LogConfigurations(
...
linesPrinterFunction: (List<String> lines) {
print('''\n${lines.join('\n')}''');
},
));
The most important configuration is having a way to enable or disable the logs. For exemple: in a release apk that final users will use, you don't want logs to be printed.
SandLog.setDefaultLogConfiguration(LogConfigurations(
...
// Will only print when running debug mode, nothing will be logged in release
isLogActivated: kDebugMode, // Will only print is boolean is true,
));
You can make a plain text log with any of the log colors
These logs will not
have any type of formatting.
logInBlack('Black text');
logInBlue('Blue text');
logInCyan('Cyan text');
logInGreen('Green text');
logInMagenta('Magenta text');
logInRed('Red text');
logInWhite('White text');
logInYellow('Yellow text');
Made with ❤ by https://github.com/igormidev
if you like the package, give a 👍