Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
maiha committed Sep 7, 2018
0 parents commit fc7cf8a
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock
/.crystal-version
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: crystal
sudo: false
script:
- make test
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 maiha

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
35 changes: 35 additions & 0 deletions Makefile
@@ -0,0 +1,35 @@
SHELL=/bin/bash

VERSION=
CURRENT_VERSION=$(shell git tag -l | sort -V | tail -1)
GUESSED_VERSION=$(shell git tag -l | sort -V | tail -1 | awk 'BEGIN { FS="." } { $$3++; } { printf "%d.%d.%d", $$1, $$2, $$3 }')

.SHELLFLAGS = -o pipefail -c

.PHONY : test
test: check_version_mismatch spec

.PHONY : spec
spec:
crystal spec -v --fail-fast

.PHONY : check_version_mismatch
check_version_mismatch: shard.yml README.md
diff -w -c <(grep version: README.md) <(grep ^version: shard.yml)

.PHONY : version
version:
@if [ "$(VERSION)" = "" ]; then \
echo "ERROR: specify VERSION as bellow. (current: $(CURRENT_VERSION))";\
echo " make version VERSION=$(GUESSED_VERSION)";\
else \
sed -i -e 's/^version: .*/version: $(VERSION)/' shard.yml ;\
sed -i -e 's/^ version: [0-9]\+\.[0-9]\+\.[0-9]\+/ version: $(VERSION)/' README.md ;\
echo git commit -a -m "'$(COMMIT_MESSAGE)'" ;\
git commit -a -m 'version: $(VERSION)' ;\
git tag "v$(VERSION)" ;\
fi

.PHONY : bump
bump:
make version VERSION=$(GUESSED_VERSION) -s
64 changes: 64 additions & 0 deletions README.md
@@ -0,0 +1,64 @@
# composite_logger.cr

Logger interface to write to multiple loggers for [Crystal](http://crystal-lang.org/).

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

## Installation

Add this to your application's `shard.yml`:

```yaml
dependencies:
composite_logger:
github: maiha/composite_logger.cr
version: 0.1.0
```

## Usage

```crystal
require "composite_logger"
```

### logging to both File and STDOUT

```crystal
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.

```crystal
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
```

## 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

- maiha(https://github.com/maiha) maiha - creator, maintainer
8 changes: 8 additions & 0 deletions shard.yml
@@ -0,0 +1,8 @@
name: composite_logger
version: 0.1.0

authors:
- maiha <maiha@wota.jp>


license: MIT
36 changes: 36 additions & 0 deletions spec/composite_logger_spec.cr
@@ -0,0 +1,36 @@
require "./spec_helper"

private def messages_in(io) : Array(String)
ary = io.to_s.chomp.gsub(/^.*? -- : (.*?)$/m){$1}.split(/\n/)
(ary == [""]) ? Array(String).new : ary
end

describe CompositeLogger do
it "works with multiple loggers" do
debug = IO::Memory.new
info = IO::Memory.new
loggers = [
Logger.new(debug).tap(&.level = Logger::DEBUG),
Logger.new(info).tap(&.level = Logger::INFO),
]
logger = CompositeLogger.new(loggers)
logger.debug("debug")
logger.info("info")

messages_in(debug).should eq(["debug", "info"])
messages_in(info).should eq(["info"])
end

describe "#memory" do
it "works as a handy in-memory logger" do
loggers = [Logger.new(STDOUT)]
logger = CompositeLogger.new(loggers, memory: Logger::ERROR)

logger.info("info")
logger.memory.to_s.empty?.should be_true

logger.error("error")
logger.memory.to_s.empty?.should be_false
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
@@ -0,0 +1,2 @@
require "spec"
require "../src/composite_logger"
63 changes: 63 additions & 0 deletions src/composite_logger.cr
@@ -0,0 +1,63 @@
require "logger"

class CompositeLogger < Logger
include Enumerable(Logger)

@memory : IO::Memory?

def initialize(@loggers : Array(Logger), memory : Logger::Severity? = nil)
if memory
@memory = IO::Memory.new
@loggers << Logger.new(@memory).tap(&.level = memory)
end
super(nil)
end

def memory : IO::Memory
@memory || raise "Memory logger is not enabled"
end

delegate each, to: @loggers

{% for method in %w( level= formatter= ) %}
def {{method.id}}(v)
each do |logger|
logger.{{method.id}}(v)
end
end
{% end %}

{% for method in %w( close ) %}
def {{method.id}}(*args)
each do |logger|
logger.{{method.id}}(*args)
end
end
{% end %}

{% for method in %w( debug info warn error fatal ) %}
def {{method.id}}(*args, **options)
each do |logger|
logger.{{method.id}}(*args, **options)
end
end

def {{method.id}}(*args, **options)
each do |logger|
logger.{{method.id}}(*args, **options) do |*yield_args|
yield *yield_args
end
end
end
{% end %}
end

class CompositeLogger
def self.new(logger : CompositeLogger) : CompositeLogger
logger
end

def self.new(logger : Logger) : CompositeLogger
CompositeLogger.new([logger])
end
end

0 comments on commit fc7cf8a

Please sign in to comment.