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.
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(path) 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.crConstant Summary
-
DEBUG =
Severity::DEBUG
-
ERROR =
Severity::ERROR
-
FATAL =
Severity::FATAL
-
INFO =
Severity::INFO
-
UNKNOWN =
Severity::UNKNOWN
-
WARN =
Severity::WARN
Class Method Summary
-
.new(io : IO | Nil)
Creates a new logger that will log to the given io.
Instance Method Summary
-
#close
Calls the close method on the object passed to
initialize
-
#debug(message, progname = nil)
Logs message if the logger's current severity is lower or equal to
DEBUG
. -
#debug(progname = nil, &block)
Logs the message as returned from the given block if the logger's current severity is lower or equal to
DEBUG
. -
#debug?
Returns
true
if the logger's current severity is lower or equal toDEBUG
. -
#error(message, progname = nil)
Logs message if the logger's current severity is lower or equal to
ERROR
. -
#error(progname = nil, &block)
Logs the message as returned from the given block if the logger's current severity is lower or equal to
ERROR
. -
#error?
Returns
true
if the logger's current severity is lower or equal toERROR
. -
#fatal(progname = nil, &block)
Logs the message as returned from the given block if the logger's current severity is lower or equal to
FATAL
. -
#fatal(message, progname = nil)
Logs message if the logger's current severity is lower or equal to
FATAL
. -
#fatal?
Returns
true
if the logger's current severity is lower or equal toFATAL
. - #formatter : String, Time, String, String, IO -> Nil
- #formatter=(formatter)
-
#info(progname = nil, &block)
Logs the message as returned from the given block if the logger's current severity is lower or equal to
INFO
. -
#info(message, progname = nil)
Logs message if the logger's current severity is lower or equal to
INFO
. -
#info?
Returns
true
if the logger's current severity is lower or equal toINFO
. - #level : Severity
- #level=(level : Severity)
-
#log(severity, message, progname = nil)
Logs message if severity is higher or equal with the logger's current severity.
-
#log(severity, progname = nil, &block)
Logs the message as returned from the given block if severity is higher or equal with the loggers current severity.
- #progname : String
- #progname=(progname : String)
-
#unknown(message, progname = nil)
Logs message if the logger's current severity is lower or equal to
UNKNOWN
. -
#unknown(progname = nil, &block)
Logs the message as returned from the given block if the logger's current severity is lower or equal to
UNKNOWN
. -
#unknown?
Returns
true
if the logger's current severity is lower or equal toUNKNOWN
. -
#warn(progname = nil, &block)
Logs the message as returned from the given block if the logger's current severity is lower or equal to
WARN
. -
#warn(message, progname = nil)
Logs message if the logger's current severity is lower or equal to
WARN
. -
#warn?
Returns
true
if the logger's current severity is lower or equal toWARN
.
Instance methods inherited from class Reference
==(other)==(other : self) ==, dup dup, hash hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, same?(other : Reference)
same?(other : Nil) same?, to_s(io : IO) : Nil to_s
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, crystal_type_id crystal_type_id, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml
to_yaml(io : IO) to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
clone
clone,
dup
dup,
from_json(string_or_io) : selffrom_json(string_or_io, root : String) : self from_json, from_yaml(string : String) : self from_yaml, hash hash, inspect(io) inspect, name : String name, nilable? nilable?, to_s(io) to_s, |(other : U.class) forall U |
Class Method Detail
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
Logs message if the logger's current severity is lower or equal to DEBUG
.
progname overrides a default progname set in this logger.
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.
Logs message if the logger's current severity is lower or equal to ERROR
.
progname overrides a default progname set in this logger.
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.
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.
Logs message if the logger's current severity is lower or equal to FATAL
.
progname overrides a default progname set in this logger.
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.
Logs message if the logger's current severity is lower or equal to INFO
.
progname overrides a default progname set in this logger.
Logs message if severity is higher or equal with the logger's current severity. progname overrides a default progname set in this logger.
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.
Logs message if the logger's current severity is lower or equal to UNKNOWN
.
progname overrides a default progname set in this logger.
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.
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.