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")
logger.close

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

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:

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}!!!"
    end
end

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.!!!"

All subgroups and modules

module logger

logger :: logger

A simple logger for Nit
package_diagram logger\> logger console console logger\>->console core core console->core ...core ... ...core->core github github github->logger\> popcorn popcorn popcorn->logger\> popcorn->github popcorn... ... popcorn...->popcorn

Ancestors

group codecs

core > codecs

Group module for all codec-related manipulations
group collection

core > collection

This module define several collection classes.
group core

core

Nit common library of core classes and methods
group text

core > text

All the classes and methods related to the manipulation of text entities

Parents

group console

console

Defines some ANSI Terminal Control Escape Sequences.

Children

group github

github

Nit wrapper for Github API
group popcorn

popcorn

Popcorn

Descendants