Usage

Getting started

To log messages and data, we need to create two objects:

  1. A MessageLog, which stores a message and any associated data that we want to log.

  2. A CodenicLogger, which enables us to log the contents of a MessageLog in a formatted structure.

For example:

final codenicLogger = CodenicLogger();

final messageLog = MessageLog(
  id: 'test',
  message: 'Test message logged',
  data: { 'name': 'Revan', 'age': 27 },
);

codenicLogger.info(messageLog);

Different Log levels

The CodenicLogger offers multiple log levels, each with a different color to make them easier to spot in the console. This can help you quickly identify and locate important log messages.

final messageLog = MessageLog(id: 'log-levels');

codenicLogger
  ..verbose(messageLog)
  ..debug(messageLog)
  ..info(messageLog)
  ..warn(messageLog)
  ..error(messageLog)
  ..wtf(messageLog);

Logging an Exception

To log an error together with its stack trace, you can pass it to the logger. This can help you troubleshoot and diagnose issues more effectively.

try {
  throw Exception('error-with-stacktrace');
} 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:

codenicLogger.userId = null;

Last updated