composite_logger.cr has been closed. Use Pretty::Logger instead.
Logger interface to write to multiple loggers for Crystal.
In addition, this extends handy methods to stdlib Logger
.
require "composite_logger"
logger = CompositeLogger.new
logger << Logger.new(STDOUT, level: "=INFO")
logger << Logger.new(STDERR, level: ">=WARN")
logger << Logger.new("err.log", level: "ERROR")
logger.warn("API: HTTP 500")
logger.error("DB: file not found")
logger.info("done")
--- STDOUT ---
done
--- STDERR ---
API: HTTP 500
DB: file not found
--- err.log ---
DB: file not found
class CompositeLogger < Logger
def initialize(loggers : Array(Logger), ...)
class Logger
def colorize=(bool : Bool)
def formatter=(fmt : String)
def level=(str : String)
def level_op=(op : LevelOp)
Add this to your application's shard.yml
:
dependencies:
composite_logger:
github: maiha/composite_logger.cr
version: 0.4.0
already described in top usage.
memory:
option provides a handy in-memory logging.
logger = CompositeLogger.new(memory: Logger::ERROR)
...
unless logger.memory.to_s.empty?
STDERR.puts "Some errors occurred while running program."
exit -1
end
This library enhanced stdlib Logger
to colorize messages.
- ERROR, FATAL: red
- WARN: yellow
This is enabled only when colorize == true
and formatter=
is used.
logger.formatter = "{{message}}"
logger.colorize = true
logger.error "foo" # => "\e[31mfoo\e[0m\n"
This library enhanced stdlib Logger#formtter=
to accept format string.
The traditional way to set formatter is not pretty.
logger.formatter = Logger::Formatter.new do |severity, datetime, progname, message, io|
io << severity.to_s << "," << message
end
logger.info "foo" # => "INFO,foo\n"
This can be simply refactored by {{KEYWORD(=FORMAT)}}
as follows.
logger.formatter = "{{severity}},{{message}}"
logger.info "foo" # => "INFO,foo\n"
Keyword | Alias | Converted to | Example |
---|---|---|---|
{{level}} |
severity |
severity |
"INFO" |
{{level=[%-5s]}} |
severity |
"[INFO ]" | |
{{mark}} |
severity.to_s[0] |
"I" | |
{{time}} |
datetime |
datetime |
"2019-01-24 21:03:45" |
{{time=%H:%M}} |
datetime.to_s("...") |
"21:03" | |
{{prog}} |
progname |
progname |
"main" |
{{message}} |
message |
"foo" | |
{{pid=%6s}} |
Process.pid |
" 5361" | |
{{xxx}} |
(leaves unknowns) | "{{xxx}}" |
The default formatter in stdlib can be represented as follows.
"{{mark}}, [{{time}}\#{{pid}}] {{prog=%s: }}{{message}}"
This library enhanced stdlib Logger#level=
to accept level string for handy accessor.
logger = Logger.new(nil)
logger.level = "DEBUG"
In addition, it provides a level op as level quantifier.
logger = Logger.new(STDOUT, level: "=INFO")
logger.info "foo" # Of course stored
logger.warn "foo" # ignored **(NEW)**
>=
(default),=
,>
,<
,<=
,<>
,!=
(same as<>
)
- Fork it (https://github.com/maiha/composite_logger.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- maiha(https://github.com/maiha) maiha - creator, maintainer