codenic_logger 0.2.0 codenic_logger: ^0.2.0 copied to clipboard
An enhanced logger for providing structured and detailed log messages.
A logger for providing structured and detailed log messages.
This uses the logger package to produce log messages.
Features #
Use this plugin in your app to:
- Structurally add log message, details and data.
- 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(message: 'Save age failed', details: 'No internet', data: { '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
- Customizing the logger
- Sample Integration: Firebase Crashlytics
Logging with different log levels #
final codenicLogger = CodenicLogger();
const messageLog = MessageLog(message: 'Sample message', data: { 'foo': false, 'lorep': 'ipsum' });
codenicLogger.verbose(messageLog);
codenicLogger.debug(messageLog);
codenicLogger.info(messageLog);
codenicLogger.warn(messageLog);
codenicLogger.error(messageLog);
codenicLogger.wtf(messageLog);
Logging an exception #
try {
throw Exception('Test exception');
} catch (exception, stackTrace) {
messageLog.details = '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;
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 message, {
Map<String, dynamic>? data,
error,
StackTrace? stackTrace,
}) {
super.error(message, data: data, error: error, stackTrace: stackTrace);
FirebaseCrashlytics.instance.recordError(
error,
stackTrace,
reason: formatMessageData(message, data),
);
}
}
Additional information #
Contributing to this plugin #
If you would like to contribute to the package, check out the contribution guide.