Codenic Clean Architecture
  • Introduction
  • The Clean Architecture
    • Presentation Layer
    • Domain Layer
    • Infrastructure Layer
  • Tutorial
    • Overview
    • Creating a Flutter Modular Project
      • Tips for Managing a Modular Project
    • Implementing the Domain Layer
      • Core Dependencies
      • Creating an Entity
      • Creating a Failure
      • Creating a Repository Interface
      • Creating Use Cases
        • CRUD Operations (Runner)
        • Data Streams (Watcher)
    • Implementing the Infrastructure Layer
      • External Dependencies
      • Creating a Data Model
      • Creating a Data Source
      • Implementing a Repository
    • Implementing the Presentation Layer
      • External Dependencies
      • Dependency Injection and Service Locator
      • Widgets
        • Snackbar Handler
        • Global Blocs Widget
        • Note Widgets
  • Packages
    • Codenic Bloc Use Case
      • Runner
      • Watcher
    • Codenic Logger
      • Usage
      • Example
      • Modifying the Logger
      • Integrating Firebase Crashlytics to the logger
    • Codenic Exception Converter
      • Failure Object
      • Exception Converter
      • Exception Converter Suite
      • Example
Powered by GitBook
On this page
  • Getting started
  • Different Log levels
  • Logging an Exception
  • Setting a user ID
  1. Packages
  2. Codenic Logger

Usage

PreviousCodenic LoggerNextExample

Last updated 2 years ago

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;