-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stdio_logger extension for a minimal logger that Sequel::Database…
… can use This is mostly taken from the bin/sequel implementation, with the main change that it should be more thread-safe by using write instead of puts. Change bin/sequel to use this extension instead of defining an anonymous class. Also have bin/sequel use sync for log files, as that is what logger does.
- Loading branch information
1 parent
2a8bc37
commit 0fb4446
Showing
4 changed files
with
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen-string-literal: true | ||
# | ||
# The stdio_logger extension exposes a Sequel::StdioLogger class that | ||
# can be used for logging with Sequel, as a minimal alternative to | ||
# the logger library. It exposes debug/info/warn/error methods for the | ||
# different warning levels. The debug method is a no-op, so that setting | ||
# the Database sql_log_level to debug will result in no output for normal | ||
# queries. The info/warn/error methods log the current time, log level, | ||
# and the given message. | ||
# | ||
# To use this extension: | ||
# | ||
# Sequel.extension :stdio_logger | ||
# | ||
# Then you you can use Sequel::StdioLogger to wrap IO objects that you | ||
# would like Sequel to log to: | ||
# | ||
# DB.loggers << Sequel::StdioLogger.new($stdout) | ||
# | ||
# log_file = File.open("db_queries.log", 'a') | ||
# log_file.sync = true | ||
# DB.loggers << Sequel::StdioLogger.new(log_file) | ||
# | ||
# This is implemented as a global extension instead of a Database extension | ||
# because Database loggers must be set before Database extensions are loaded. | ||
# | ||
# Related module: Sequel::StdioLogger | ||
|
||
# | ||
module Sequel | ||
class StdioLogger | ||
def initialize(device) | ||
@device = device | ||
end | ||
|
||
# Do not log debug messages. This is so setting the Database | ||
# sql_log_level to debug will result in no output. | ||
def debug(msg) | ||
end | ||
|
||
[:info, :warn, :error].each do |meth| | ||
define_method(meth) do |msg| | ||
@device.write("#{Time.now.strftime('%F %T')} #{meth.to_s.upcase}: #{msg}\n") | ||
nil | ||
end | ||
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,25 @@ | ||
require_relative "spec_helper" | ||
|
||
require 'stringio' | ||
|
||
describe "stdio_logger extension: Sequel::StdioLogger" do | ||
before do | ||
Sequel.extension :stdio_logger | ||
@output = StringIO.new | ||
@logger = Sequel::StdioLogger.new(@output) | ||
end | ||
|
||
it "#debug should not log" do | ||
@logger.debug("foo").must_be_nil | ||
@output.rewind | ||
@output.read.must_equal '' | ||
end | ||
|
||
[:info, :warn, :error].each do |level| | ||
it "##{level} should log message with given level" do | ||
@logger.send(level, "foo").must_be_nil | ||
@output.rewind | ||
@output.read.must_match(/ #{level.to_s.upcase}: foo\n\z/) | ||
end | ||
end | ||
end |