Logger interface to write to multiple loggers
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
spec
src
.editorconfig
.gitignore
.travis.yml
LICENSE
Makefile
README.md
shard.yml

README.md

composite_logger.cr Build Status

Logger interface to write to multiple loggers for Crystal.

logger = CompositeLogger.new(loggers)
logger.info("hello")
# => (stdout) "hello"
# => (a file) "hello"

In addition, this add a Logger#formatter=(str : String) method to core library. See: src/ext/logger.cr

Installation

Add this to your application's shard.yml:

dependencies:
  composite_logger:
    github: maiha/composite_logger.cr
    version: 0.3.2

Usage

require "composite_logger"

logging to both File and STDOUT

loggers = [
  Logger.new(STDOUT),
  Logger.new(File.open("app.log")),
]
logger = CompositeLogger.new(loggers)
logger.info("hello")

in memory logging

memory: option provides a handy in-memory logging.

loggers = [Logger.new(STDOUT)]
logger = CompositeLogger.new(loggers, memory: Logger::ERROR)
...
unless logger.memory.to_s.empty?
  STDERR.puts "Some errors occurred while running program."
  exit -1
end

Logger#level=(str : String)

This library enhanced stdlib Logger#level= to accept level string for handy accessor.

logger = Logger.new(nil)
logger.level = "DEBUG"

Logger#formatter=(fmt : String)

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"
available keywords
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}}"

Logger#colorize = true

This library enhanced stdlib Logger to colorize messages.

  • ERROR, FATAL: red
  • WARN: yellow

This is enabled only when colorize == true and above formatter= is used.

logger.formatter = "{{message}}"
logger.colorize = true
logger.error "foo" # => "\e[31mfoo\e[0m\n"

Contributing

  1. Fork it (https://github.com/maiha/composite_logger.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors