logError static method

void logError(
  1. String? text, {
  2. String? name,
  3. String? error,
  4. StackTrace? stackTrace,
  5. LogColors? msgColor,
  6. LogColors? textColor,
  7. LogColors? fileNameColor,
  8. LogColors? errorColor,
  9. LogColors? stackTraceColor,
})

Logs an error message with the specified error, text, name, and stackTrace.

This method handles logging of error messages with optional color formatting for the text, error message, file name, and stack trace. It respects the printHandle flag and only logs messages if certain conditions are met.

Parameters:

  • text (String): The additional information or message to log.
  • name (String?): The name of the file to include in the log message. This parameter is optional.
  • error (String?): The error message to log. This parameter is optional.
  • stackTrace (StackTrace?): The stack trace associated with the error. This parameter is optional.
  • msgColor (LogColors?): The color to use for the main log message. If not provided, the default color is magenta.
  • textColor (LogColors?): The color to use for the text part of the log message. If not provided, the default color is yellow.
  • fileNameColor (LogColors?): The color to use for the file name part of the log message. If not provided, the default color is blue.
  • errorColor (LogColors?): The color to use for the error message part of the log message. If not provided, the default color is yellow.
  • stackTraceColor (LogColors?): The color to use for the stack trace part of the log message. If not provided, the default color is yellow.

Returns:

  • (void): This method does not return a value.

Notes:

  • If printHandle is true, no logging will occur.
  • If AppLogs.screens is not empty and name is not included in AppLogs.screens, logging will not occur.
  • The method will print the log message, error message, file name, and stack trace with specified or default colors.

Example:

AppLogs.logError(
  "This is additional information.",
  name: "example.dart",
  error: "An unexpected error occurred.",
  stackTrace: StackTrace.current,
  msgColor: LogColors.red,
  textColor: LogColors.blue,
  fileNameColor: LogColors.green,
  errorColor: LogColors.yellow,
  stackTraceColor: LogColors.cyan,
);

Implementation

static void logError(
  String? text, {
  String? name,
  String? error,
  StackTrace? stackTrace,
  LogColors? msgColor,
  LogColors? textColor,
  LogColors? fileNameColor,
  LogColors? errorColor,
  LogColors? stackTraceColor,
}) {
  if (printHandle) {
    return;
  }
  if (AppLogs.screens.isNotEmpty) {
    if (!AppLogs.screens.contains(name)) {
      return;
    }
  }
  if (kDebugMode) {
    print(
      "[${msgColor?.value ?? (name != null ? (fileNameColor?.value ?? LogColors.magenta.value) : LogColors.magenta.value)} ${name ?? 'LOG MESSAGE'} ${LogColors.reset.value}] ${textColor?.value ?? (name != null ? LogColors.white.value : LogColors.yellow.value)} $text ${LogColors.reset.value}",
    );
    print(
      "[${msgColor?.value ?? LogColors.magenta.value} LOG ERROR MESSAGE ${LogColors.reset.value}] ${errorColor?.value ?? LogColors.red.value} ${error.toString()} ${LogColors.reset.value}",
    );
    print(
      "[${msgColor?.value ?? LogColors.magenta.value} LOG STACKTRACE MESSAGE ${LogColors.reset.value}] ${stackTraceColor?.value ?? LogColors.yellow.value} ${stackTrace.toString()} ${LogColors.reset.value}",
    );
  }
}