logger - 

A simple logger for Nit

Basic Usage

Create a new Logger with a severity level threshold set to warn_level:

var logger = new Logger(warn_level)

Messages with a severity equal or higher than warn_level will be displayed:

logger.error "Displays an error."
logger.warn "Displays a warning."

Messages with a lower severity are silenced:

logger.info "Displays nothing."

FileLogger can be used to output the messages into a file:

var log_file = "my.log"

logger = new FileLogger(warn_level, log_file, append = false)
logger.error("An error")
logger.info("Some info")

assert log_file.to_path.read_all == "An error\n"

Severity levels

Each message is associated with a level that indicate its severity. Only messages with a severity equal to or higher than the logger level threshold will be displayed.

Severity levels from the most severe to the least severe:

  • unknown_level: An unknown message that should always be outputted.
  • fatal_level: An unhandleable error that results in a program crash.
  • error_level: A handleable error condition.
  • warn_level: A warning.
  • info_level: Generic (useful) information about system operation.
  • debug_level: Low-level information for developpers.

Formatting messages

You can create custom formatters by implementing the Formatter interface.

class MyFormatter
    super Formatter

    redef fun format(level, message) do
        if level < warn_level then return super
        return "!!!{message}!!!"

See DefaultFormatter for a more advanced implementation example.

Each Logger can be given a default formatter used to format the every messages before outputting them:

var formatter = new MyFormatter
var stderr = new StringWriter
var logger = new Logger(warn_level, stderr, formatter)

logger.warn("This is a warning.")
assert stderr.to_s.trim.split("\n").last == "!!!This is a warning.!!!"

Optionally, a Formatter can be given to replace the default_formatter used by default:

logger = new Logger(warn_level, stderr, null)

# Display a message without any formatter
logger.warn("This is a warning.")
assert stderr.to_s.trim.split("\n").last == "This is a warning."

# Display a message with a custom formatter
logger.warn("This is a warning.", formatter)
assert stderr.to_s.trim.split("\n").last == "!!!This is a warning.!!!"