Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce abstraction for stream logging #70

Merged
merged 1 commit into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/scrolls/iolog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Scrolls
class IOLog
def initialize(stream)
stream.sync = true
@stream = stream
end

def log(data)
@stream.write("#{data}\n")
end
end
end
14 changes: 4 additions & 10 deletions lib/scrolls/log.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "scrolls/parser"
require "scrolls/utils"
require "scrolls/iolog"
require "scrolls/syslog"

module Scrolls
Expand Down Expand Up @@ -231,21 +232,14 @@ def calc_time(start, finish)
((finish - start).to_f * @t)
end

def mtx
@mtx ||= Mutex.new
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mutex wasn't used anymore so it could be cleaned up.


def sync_stream(out=nil)
out = STDOUT if out.nil?
s = out
s.sync = true
s
def sync_stream(out = STDOUT)
IOLog.new(out)
end

def write(data)
if log_level_ok?(data[:level])
msg = unparse(data)
stream.print(msg + "\n")
stream.log(msg)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/scrolls/syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(ident = 'scrolls', facility = Syslog::LOG_USER)
end
end

def puts(data)
def log(data)
@syslog.log(Syslog::LOG_INFO, "%s", data)
end

Expand Down
10 changes: 8 additions & 2 deletions test/test_scrolls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def teardown
end

def test_construct
assert_equal StringIO, Scrolls.stream.class
assert_equal Scrolls::IOLog, Scrolls.stream.class
end

def test_default_global_context
Expand Down Expand Up @@ -130,7 +130,7 @@ def test_log_exception

oneline_backtrace = @out.string.gsub("\n", 'XX')

assert_match /test=exception at=exception.*test_log_exception.*XX.*minitest/,
assert_match /test=exception at=exception.*test_log_exception.*XX/,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed to get the tests to pass since more recent Ruby versions don't run the tests with minitest by default anymore.

oneline_backtrace
end

Expand Down Expand Up @@ -165,6 +165,12 @@ def test_setting_syslog_facility_after_instantiation
assert_match /facility=184/, Scrolls.stream.inspect
end

def test_logging_message_with_syslog
Scrolls.stream = 'syslog'
Scrolls.facility = 'local7'
Scrolls.log "scrolls test"
end

def test_add_timestamp
Scrolls.add_timestamp = true
Scrolls.log(:test => "foo")
Expand Down