en_logger 1.2.0 copy "en_logger: ^1.2.0" to clipboard
en_logger: ^1.2.0 copied to clipboard

EnLogger allows you to write log messages according to your needs without restricting you to writing messages to the debug console or other systems.

En Logger #

ci License: MIT coverage

EnLogger allows you to write log messages according to your needs without restricting you to writing messages to the debug console or other systems. It maintains a list of EnLoggerHandlers internally. You can implement your own EnLoggerHandler based on your specific requirements. Each time you want to log a message with EnLogger, each EnLoggerHandler will be invoked to perform the write operation.

  • If you need your system to write logs to a file, implement an EnLoggerHandler that performs the write operation to a file.

  • If you want to send logs to Sentry, implement an EnLoggerHandler that maintains an instance of Sentry internally. Each time a log is written, call the write method of Sentry.

PrinterHandler is an EnLoggerHandler provided within the library that allows you to write colored messages to the developer console.

Some examples are shown in the example project.

The logs adhere to the syslog severity levels.

Installation #

dependencies:
  en_logger:
copied to clipboard

Configuration #

Fast setup #

  • Create en EnLogger instance: final logger = EnLogger()
  • Add handlers to your logger: logger.addHandler(PrinterLogger())
  • Write log: logger.debug('Arrived here');, logger.error('Error deserializing data')

Prefix #

Logs can have a prefix. A style can be applied to this prefix.

final logger = EnLogger(defaultPrefixFormat: PrefixFormat(
    startFormat: '[',
    endFormat: ']',
    style: PrefixStyle.uppercaseSnakeCase,
  ))
  ..addHandler(PrinterLogger())
  ..debug('get data',prefix: "API repository")

  // printer output --> '[API_REPOSITORY] get data'
copied to clipboard

Data #

Messages can include attachments such as serialized data, file contents, and more.

// error with data
logger.error(
  "error",
  data: [
    EnLoggerData(
      name: "response",
      content: jsonEncode("BE data"),
      description: "serialized BE response",
    ),
  ],
);
copied to clipboard

Instances #

To avoid having to rewrite the prefix each time, you can create instances based on EnLogger. For instance, if you have a specific scope (like the API repository), you can instantiate a EnLogger with the prefix "API repository," and this prefix will be included in every log unless you explicitly override it.

final instLogger = logger.getConfiguredInstance(prefix: 'API Repository');
instLogger.debug('a debug message'); // [API Repository] a debug message
instLogger.error(
  'error',
  prefix: 'Custom prefix',
); // [Custom prefix] a debug message
copied to clipboard

PrinterHandler #

A default Develop console handler colored. There is a basic color setup that can be updated.

  final printer = PrinterHandler()
    ..configure({Severity.notice: PrinterColor.green()});

copied to clipboard

You can also create custom colors.

PrinterColor.custom(schema: '\x1B[31m')
copied to clipboard

CustomHandler #

  1. Create a custom handler that extends EnLoggerHandler;

  2. Override write method with your custom write operation.

class FileHandler extends EnLoggerHandler {
  @override
  void write(String message) {
    _logToFile(message);
  }
}
copied to clipboard

Some examples are shown in the example project.

2
likes
160
points
159
downloads

Publisher

verified publishermattiapispisa.it

Weekly Downloads

2024.08.16 - 2025.02.28

EnLogger allows you to write log messages according to your needs without restricting you to writing messages to the debug console or other systems.

Homepage
Repository (GitHub)

Topics

#logging #severity #debug

Documentation

API reference

License

MIT (license)

More

Packages that depend on en_logger