Skip to content

Commit

Permalink
Refactor Scrolls
Browse files Browse the repository at this point in the history
This commit breaks backwards compatability for Scrolls. It
is a rework/refactor of the initial work done in the
following PR: #54

- The `global_context` is no longer mutable, instead we
instantiate a class internally inside Scrolls::Logger that
contains the `global_context`. This is to avoid various bad
thread behaviors, that previous to Ruby 2.0, were allowed.
- The result of the above change removes `#add_global_context`.
- Currently Scrolls needs to be instantiated to be used
(`Scrolls.init(options)`) however I'd like to work on a
method for allowing one to just use `Scrolls.log` and get
sane defaults.
- Due to this massive change I moved to "single line
exceptions" by default since it lowers the amount of data
produced by Scrolls when using `#log_exception` without
losing the information.
  • Loading branch information
asenchi committed Aug 23, 2017
1 parent 221b099 commit cebd41d
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 206 deletions.
104 changes: 31 additions & 73 deletions lib/scrolls.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
require "thread"
require "scrolls/atomic"
require "scrolls/log"
require "scrolls/logger"
require "scrolls/version"

module Scrolls
extend self

# Public: Initialize a Scrolls logger
#
# Convienence method to prepare for future releases. Currently mimics
# behavior found in other methods. This prepares the developer for a future
# backward incompatible change, see:
# https://github.com/asenchi/scrolls/pull/54
#
# options - A hash of key/values for configuring Scrolls
#
def init(options)
stream = options.fetch(:stream, STDOUT)
facility = options.fetch(:facility, Syslog::LOG_USER)
time_unit = options.fetch(:time_unit, "seconds")
timestamp = options.fetch(:timestamp, false)
exceptions = options.fetch(:exceptions, "multi")
global_ctx = options.fetch(:global_context, {})

Log.stream = stream
Log.facility = facility if facility
Log.time_unit = time_unit unless time_unit == "seconds"
Log.add_timestamp = timestamp unless timestamp == false

if exceptions == "single"
Log.single_line_exceptions = true
end

unless global_ctx == {}
Log.global_context = global_ctx
end
def init(options={})
@log = Log.new(options)
end

# Public: Set a context in a block for logs
Expand All @@ -45,37 +21,13 @@ def init(options)
# Examples:
#
def context(data, &blk)
Log.with_context(data, &blk)
end

# Deprecated: Get or set a global context that prefixs all logs
#
# data - A hash of key/values to prepend to each log
#
# This method will be deprecated two releases after 0.3.8.
# See https://github.com/asenchi/scrolls/releases/tag/v0.3.8
# for more details.
#
def global_context(data=nil)
$stderr.puts "global_context() will be deprecated after v0.3.8, please see https://github.com/asenchi/scrolls for more information."
if data
Log.global_context = data
else
Log.global_context
end
@log.with_context(data, &blk)
end

# Deprecated: Get or set a global context that prefixs all logs
#
# data - A hash of key/values to prepend to each log
#
# This method will be deprecated two releases after 0.3.8.
# See https://github.com/asenchi/scrolls/releases/tag/v0.3.8
# for more details.
# Public: Get the global context that prefixs all logs
#
def add_global_context(data)
$stderr.puts "add_global_context will be deprecated after v0.3.8, please see https://github.com/asenchi/scrolls for more information."
Log.add_global_context(data)
def global_context
@log.global_context
end

# Public: Log data and/or wrap a block with start/finish
Expand All @@ -96,7 +48,7 @@ def add_global_context(data)
# => nil
#
def log(data, &blk)
Log.log(data, &blk)
@log.log(data, &blk)
end

# Public: Log an exception
Expand All @@ -115,7 +67,7 @@ def log(data, &blk)
# ...
#
def log_exception(data, e)
Log.log_exception(data, e)
@log.log_exception(data, e)
end

# Public: Setup a logging facility (default: Syslog::LOG_USER)
Expand All @@ -127,7 +79,7 @@ def log_exception(data, e)
# Scrolls.facility = Syslog::LOG_LOCAL7
#
def facility=(f)
Log.facility=(f)
@log.facility=(f)
end

# Public: Return the Syslog facility
Expand All @@ -138,7 +90,7 @@ def facility=(f)
# => 8
#
def facility
Log.facility
@log.facility
end

# Public: Setup a new output (default: STDOUT)
Expand All @@ -154,7 +106,7 @@ def facility
# Scrolls.stream = StringIO.new
#
def stream=(out)
Log.stream=(out)
@log.stream=(out)
end

# Public: Return the stream
Expand All @@ -165,7 +117,7 @@ def stream=(out)
# => #<IO:<STDOUT>>
#
def stream
Log.stream
@log.stream
end

# Public: Set the time unit we use for 'elapsed' (default: "seconds")
Expand All @@ -177,7 +129,7 @@ def stream
# Scrolls.time_unit = "milliseconds"
#
def time_unit=(unit)
Log.time_unit=(unit)
@log.time_unit = unit
end

# Public: Return the time unit currently configured
Expand All @@ -188,7 +140,7 @@ def time_unit=(unit)
# => "seconds"
#
def time_unit
Log.time_unit
@log.time_unit
end

# Public: Set whether to include a timestamp (now=<ISO8601>) field in the log
Expand All @@ -199,7 +151,7 @@ def time_unit
# Scrolls.add_timestamp = true
#
def add_timestamp=(boolean)
Log.add_timestamp = boolean
@log.timestamp = boolean
end

# Public: Return whether the timestamp field will be included in the log
Expand All @@ -211,7 +163,7 @@ def add_timestamp=(boolean)
# => true
#
def add_timestamp
Log.add_timestamp
@log.timestamp
end

# Public: Set whether exceptions should generate a single log
Expand All @@ -222,7 +174,7 @@ def add_timestamp
# Scrolls.single_line_exceptions = true
#
def single_line_exceptions=(boolean)
Log.single_line_exceptions = boolean
@log.exceptions = boolean
end

# Public: Return whether exceptions generate a single log message.
Expand All @@ -233,7 +185,7 @@ def single_line_exceptions=(boolean)
# => true
#
def single_line_exceptions?
Log.single_line_exceptions
@log.single_line_exceptions?
end

# Public: Convience method for Logger replacement
Expand All @@ -249,7 +201,7 @@ def single_line_exceptions?
#
def debug(data, &blk)
data = data.merge(:level => "debug") if data.is_a?(Hash)
Log.log(data, &blk)
@log.log(data, &blk)
end

# Public: Convience method for Logger replacement
Expand All @@ -267,7 +219,7 @@ def debug(data, &blk)
#
def error(data, &blk)
data = data.merge(:level => "warning") if data.is_a?(Hash)
Log.log(data, &blk)
@log.log(data, &blk)
end

# Public: Convience method for Logger replacement
Expand All @@ -285,7 +237,7 @@ def error(data, &blk)
#
def fatal(data, &blk)
data = data.merge(:level => "error") if data.is_a?(Hash)
Log.log(data, &blk)
@log.log(data, &blk)
end

# Public: Convience method for Logger replacement
Expand All @@ -303,7 +255,7 @@ def fatal(data, &blk)
#
def info(data, &blk)
data = data.merge(:level => "info") if data.is_a?(Hash)
Log.log(data, &blk)
@log.log(data, &blk)
end

# Public: Convience method for Logger replacement
Expand All @@ -321,7 +273,7 @@ def info(data, &blk)
#
def warn(data, &blk)
data = data.merge(:level => "notice") if data.is_a?(Hash)
Log.log(data, &blk)
@log.log(data, &blk)
end

# Public: Convience method for Logger replacement
Expand All @@ -339,7 +291,13 @@ def warn(data, &blk)
#
def unknown(data, &blk)
data = data.merge(:level => "alert") if data.is_a?(Hash)
Log.log(data, &blk)
@log.log(data, &blk)
end

# Internal: The Logger initialized by #init
#
def internal
@log
end

end
59 changes: 0 additions & 59 deletions lib/scrolls/atomic.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/scrolls/iolog.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Scrolls
class IOLog
def initialize(stream)
stream.sync = true
if stream.respond_to?(:sync)
stream.sync = true
end
@stream = stream
end

Expand Down
Loading

0 comments on commit cebd41d

Please sign in to comment.