Logger

class Logger

Overview

The Logger class provides a simple but sophisticated logging utility that you can use to output messages-

The messages have associated levels, such as INFO or ERROR that indicate their importance. You can then give the Logger a level, and only messages at that level of higher will be printed-

For instance, in a production system, you may have your Logger set to INFO or even WARN. When you are developing the system, however, you probably want to know about the program’s internal state, and would set the Logger to DEBUG.

If logging to multiple locations is required, an IO::MultiWriter can be used.

Example

require "logger"

log = Logger.new(STDOUT)
log.level = Logger::WARN

log.debug("Created logger")
log.info("Program started")
log.warn("Nothing to do!")

begin
  File.each_line("/foo/bar.log") do |line|
    unless line =~ /^(\w+) = (.*)$/
      log.error("Line in wrong format: #{line}")
    end
  end
rescue err
  log.fatal("Caught exception; exiting")
  log.fatal(err)
end

Defined in:

logger.cr

Constant Summary

DEBUG = Severity::DEBUG
ERROR = Severity::ERROR
FATAL = Severity::FATAL
INFO = Severity::INFO
UNKNOWN = Severity::UNKNOWN
WARN = Severity::WARN

Class Method Summary

Instance Method Summary

Class Method Detail

def self.new(io : IO?)Source

Creates a new logger that will log to the given io. If io is nil then all log calls will be silently ignored.

Instance Method Detail

def closeSource

Calls the close method on the object passed to initialize.

def debug(progname = nil, &block)Source

Logs the message as returned from the given block if the logger's current severity is lower or equal to DEBUG. The block is not run if the severity is higher. progname overrides a default progname set in this logger.

def debug(message, progname = nil)Source

Logs message if the logger's current severity is lower or equal to DEBUG. progname overrides a default progname set in this logger.

def debug?Source

Returns true if the logger's current severity is lower or equal to DEBUG.

def error(progname = nil, &block)Source

Logs the message as returned from the given block if the logger's current severity is lower or equal to ERROR. The block is not run if the severity is higher. progname overrides a default progname set in this logger.

def error(message, progname = nil)Source

Logs message if the logger's current severity is lower or equal to ERROR. progname overrides a default progname set in this logger.

def error?Source

Returns true if the logger's current severity is lower or equal to ERROR.

def fatal(progname = nil, &block)Source

Logs the message as returned from the given block if the logger's current severity is lower or equal to FATAL. The block is not run if the severity is higher. progname overrides a default progname set in this logger.

def fatal(message, progname = nil)Source

Logs message if the logger's current severity is lower or equal to FATAL. progname overrides a default progname set in this logger.

def fatal?Source

Returns true if the logger's current severity is lower or equal to FATAL.

def formatter : String, Time, String, String, IO -> NilSource

def formatter=(formatter)Source

def info(message, progname = nil)Source

Logs message if the logger's current severity is lower or equal to INFO. progname overrides a default progname set in this logger.

def info(progname = nil, &block)Source

Logs the message as returned from the given block if the logger's current severity is lower or equal to INFO. The block is not run if the severity is higher. progname overrides a default progname set in this logger.

def info?Source

Returns true if the logger's current severity is lower or equal to INFO.

def level : SeveritySource

def level=(level : Severity)Source

def log(severity, message, progname = nil)Source

Logs message if severity is higher or equal with the logger's current severity. progname overrides a default progname set in this logger.

def log(severity, progname = nil, &block)Source

Logs the message as returned from the given block if severity is higher or equal with the loggers current severity. The block is not run if severity is lower. progname overrides a default progname set in this logger.

def progname : StringSource

def progname=(progname : String)Source

def unknown(message, progname = nil)Source

Logs message if the logger's current severity is lower or equal to UNKNOWN. progname overrides a default progname set in this logger.

def unknown(progname = nil, &block)Source

Logs the message as returned from the given block if the logger's current severity is lower or equal to UNKNOWN. The block is not run if the severity is higher. progname overrides a default progname set in this logger.

def unknown?Source

Returns true if the logger's current severity is lower or equal to UNKNOWN.

def warn(progname = nil, &block)Source

Logs the message as returned from the given block if the logger's current severity is lower or equal to WARN. The block is not run if the severity is higher. progname overrides a default progname set in this logger.

def warn(message, progname = nil)Source

Logs message if the logger's current severity is lower or equal to WARN. progname overrides a default progname set in this logger.

def warn?Source

Returns true if the logger's current severity is lower or equal to WARN.

© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/Logger.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部