-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from ruby-formatter/allow-logging-level-to-be…
…-configured Allow logging level to be configured
- Loading branch information
Showing
5 changed files
with
150 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Rufo | ||
class Logger | ||
LEVELS = { | ||
silent: 0, | ||
error: 1, | ||
warn: 2, | ||
log: 3, | ||
debug: 4, | ||
} | ||
|
||
def initialize(level) | ||
@level = LEVELS.fetch(level) | ||
end | ||
|
||
def debug(*args) | ||
$stdout.puts(*args) if should_output?(:debug) | ||
end | ||
|
||
def log(*args) | ||
$stdout.puts(*args) if should_output?(:log) | ||
end | ||
|
||
def warn(*args) | ||
$stderr.puts(*args) if should_output?(:warn) | ||
end | ||
|
||
def error(*args) | ||
$stderr.puts(*args) if should_output?(:error) | ||
end | ||
|
||
private | ||
|
||
attr_reader :level | ||
|
||
def should_output?(level_to_check) | ||
LEVELS.fetch(level_to_check) <= level | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe Rufo::Logger do | ||
subject { described_class.new(level) } | ||
|
||
let(:level) { :debug } | ||
|
||
it "logs all levels" do | ||
expect { | ||
subject.log("a") | ||
subject.debug("b") | ||
}.to output("a\nb\n").to_stdout | ||
|
||
expect { | ||
subject.warn("c") | ||
subject.error("d") | ||
}.to output("c\nd\n").to_stderr | ||
end | ||
|
||
context "when set to log" do | ||
let(:level) { :log } | ||
|
||
it "logs all levels except debug" do | ||
expect { | ||
subject.log("a") | ||
subject.debug("b") | ||
}.to output("a\n").to_stdout | ||
|
||
expect { | ||
subject.warn("c") | ||
subject.error("d") | ||
}.to output("c\nd\n").to_stderr | ||
end | ||
end | ||
|
||
context "when set to warn" do | ||
let(:level) { :warn } | ||
|
||
it "logs only warn and error levels" do | ||
expect { | ||
subject.log("a") | ||
subject.debug("b") | ||
}.to output("").to_stdout | ||
|
||
expect { | ||
subject.warn("c") | ||
subject.error("d") | ||
}.to output("c\nd\n").to_stderr | ||
end | ||
end | ||
|
||
context "when set to error" do | ||
let(:level) { :error } | ||
|
||
it "logs only error levels" do | ||
expect { | ||
subject.log("a") | ||
subject.debug("b") | ||
}.to output("").to_stdout | ||
|
||
expect { | ||
subject.warn("c") | ||
subject.error("d") | ||
}.to output("d\n").to_stderr | ||
end | ||
end | ||
|
||
context "when set to silent" do | ||
let(:level) { :silent } | ||
|
||
it "does not log anything" do | ||
expect { | ||
subject.log("a") | ||
subject.debug("b") | ||
}.to output("").to_stdout | ||
|
||
expect { | ||
subject.warn("c") | ||
subject.error("d") | ||
}.to output("").to_stderr | ||
end | ||
end | ||
end |