codenic_logger 0.5.5-dev.1 codenic_logger: ^0.5.5-dev.1 copied to clipboard
A logger extension for providing structured and mutable log messages.
A logger extension for providing structured and mutable log messages.
This uses the logger package to produce log messages.
Features #
Use this package in your app to:
- Structurally create mutable log messages.
- Initialize log messages early, populate it with data throughout the code execution, then printing them at a later point in time.
- Log messages on multiple log levels – verbose, debug, info, warning, error and wtf.
- Automatically add a user ID to all log data upon logging.
Getting started #
To get started, just create a Codenic logger instance:
final codenicLogger = CodenicLogger();
const messageLog = MessageLog(
id: 'save_user_info',
message: 'Save completed',
data: { 'name': 'Jayce', 'age': 24 },
);
codenicLogger.info(messageLog);
Usage #
This section has examples of code for the following tasks:
- Logging with different log levels
- Logging an exception
- Setting a user ID – Adding log data
- Customizing the logger
- Sample Integration: Firebase Crashlytics
Logging with different log levels #
final codenicLogger = CodenicLogger();
final messageLog = MessageLog(id: 'log_levels');
codenicLogger
..verbose(messageLog..message = 'Verbose log success')
..debug(messageLog..message = 'Debug log success')
..info(messageLog..message = 'Info log success')
..warn(messageLog..message = 'Warn log success')
..error(messageLog..message = 'Error log success')
..wtf(messageLog..message = 'Wtf log success');
Logging an exception #
try {
throw Exception('Test exception');
} catch (exception, stackTrace) {
messageLog.message = 'An error occurred';
codenicLogger.error(messageLog, error: exception, stackTrace: stackTrace);
}
Setting a user ID #
When a user ID is provided, it will automatically be included in the log data.
codenicLogger.userId = 'sample-uid';
codenicLogger.info(messageLog);
To remove the user ID, simply set it back to null
:
codenic.userId = null;
Updating message log properties #
final messageLog = MessageLog(id: 'update_message_log');
messageLog
..message = 'Update message log success'
..data.addAll({'lorep': 'ipsum', 'mauris': 42});
codenicLogger.verbose(messageLog);
Customizing the log output #
To customize the log output, provide a custom logger instance:
final 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
),
);
final codenicLogger = CodenicLogger(logger: logger);
For more info, visit the logger package.
Sample Integration: Firebase Crashlytics #
class FirebaseLogger extends CodenicLogger {
@override
set userId(String? _userId) {
super.userId = _userId;
FirebaseCrashlytics.instance.setUserIdentifier(_userId ?? '');
}
@override
void error(
MessageLog messageLog, {
dynamic error,
StackTrace? stackTrace,
}) {
super.error(messageLog, error: error, stackTrace: stackTrace);
FirebaseCrashlytics.instance.recordError(
error,
stackTrace,
reason: messageLog,
);
}
}
Additional information #
Contributing to this plugin #
If you would like to contribute to the package, check out the contribution guide.