From 7b48d9996ffc96a82e3898bf9ab3a3dd855190d4 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 21:15:26 -0700 Subject: [PATCH 01/12] add support for removing all tags in a block --- CHANGELOG.md | 4 +++ VERSION | 2 +- lib/lumberjack.rb | 22 ++++++++++------ lib/lumberjack/logger.rb | 22 ++++++++++++++-- spec/logger_spec.rb | 54 ++++++++++++++++++++++++++++++++++++++++ spec/lumberjack_spec.rb | 8 ++++++ 6 files changed, 101 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f45f820..fe1fc68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.8 + +* Add `Logger#untag` to remove previously set logging tags from a block. + ## 1.2.7 * Allow passing frozen hashes to `Logger.tag`. Tags passed to this method are now duplicated so the logger maintains it's own copy of the hash. diff --git a/VERSION b/VERSION index c04c650..db6fb4a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.7 +1.2.8 diff --git a/lib/lumberjack.rb b/lib/lumberjack.rb index f505332..5df9f46 100644 --- a/lib/lumberjack.rb +++ b/lib/lumberjack.rb @@ -54,20 +54,26 @@ def unit_of_work_id # # Otherwise, it will return the current context. If one doesn't exist, it will return a new one # but that context will not be in any scope. - def context + def context(&block) current_context = Thread.current[:lumberjack_context] - if block_given? - Thread.current[:lumberjack_context] = Context.new(current_context) - begin - yield - ensure - Thread.current[:lumberjack_context] = current_context - end + if block + use_context(Context.new(current_context), &block) else current_context || Context.new end end + # Set the context to use within a block. + def use_context(context, &block) + current_context = Thread.current[:lumberjack_context] + begin + Thread.current[:lumberjack_context] = (context || Context.new) + yield + ensure + Thread.current[:lumberjack_context] = current_context + end + end + # Return true if inside a context block. def context? !!Thread.current[:lumberjack_context] diff --git a/lib/lumberjack/logger.rb b/lib/lumberjack/logger.rb index dc4536a..39e3ec0 100644 --- a/lib/lumberjack/logger.rb +++ b/lib/lumberjack/logger.rb @@ -380,16 +380,34 @@ def remove_tag(*tag_names) end # Return all tags in scope on the logger including global tags set on the Lumberjack - # context, tags set on the logger, and tags set on the current block for the logger + # context, tags set on the logger, and tags set on the current block for the logger. def tags tags = {} context_tags = Lumberjack.context_tags tags.merge!(context_tags) if context_tags && !context_tags.empty? - tags.merge!(@tags) if !@tags.empty? + tags.merge!(@tags) if !@tags.empty? && !thread_local_value(:lumberjack_logger_untagged) scope_tags = thread_local_value(:lumberjack_logger_tags) tags.merge!(scope_tags) if scope_tags && !scope_tags.empty? tags end + + # Remove all tags on the current logger and logging context within a block. + # You can still set new block scoped tags within the untagged block and provide + # tags on individual log methods. + def untag(&block) + Lumberjack.use_context(nil) do + scope_tags = thread_local_value(:lumberjack_logger_tags) + untagged = thread_local_value(:lumberjack_logger_untagged) + begin + set_thread_local_value(:lumberjack_logger_untagged, true) + set_thread_local_value(:lumberjack_logger_tags, nil) + tag({}, &block) + ensure + set_thread_local_value(:lumberjack_logger_untagged, untagged) + set_thread_local_value(:lumberjack_logger_tags, scope_tags) + end + end + end private diff --git a/spec/logger_spec.rb b/spec/logger_spec.rb index a2255dd..a267e91 100644 --- a/spec/logger_spec.rb +++ b/spec/logger_spec.rb @@ -532,6 +532,60 @@ end end + describe "untag" do + it "should remove tags from the Lumberjack::Context" do + Lumberjack.context do + Lumberjack.tag(foo: "bar") + expect(logger.tags).to eq({"foo" => "bar"}) + logger.untag do + expect(logger.tags).to eq({}) + end + expect(logger.tags).to eq({"foo" => "bar"}) + end + end + + it "should remove global tags from the logger" do + logger.tag(foo: "bar") + begin + expect(logger.tags).to eq({"foo" => "bar"}) + logger.untag do + expect(logger.tags).to eq({}) + end + expect(logger.tags).to eq({"foo" => "bar"}) + ensure + logger.remove_tag(:foo) + end + end + + it "should remove scoped tags from teh logger" do + logger.tag(foo: "bar") do + expect(logger.tags).to eq({"foo" => "bar"}) + logger.untag do + expect(logger.tags).to eq({}) + end + expect(logger.tags).to eq({"foo" => "bar"}) + end + end + + it "should allow adding tags inside the block" do + logger.tag(foo: "bar") do + expect(logger.tags).to eq({"foo" => "bar"}) + logger.untag do + logger.tag(stuff: "other") do + expect(logger.tags).to eq({"stuff" => "other"}) + logger.untag do + expect(logger.tags).to eq({}) + logger.tag(thing: 1) + expect(logger.tags).to eq({"thing" => 1}) + end + expect(logger.tags).to eq({"stuff" => "other"}) + end + end + expect(logger.tags).to eq({"foo" => "bar"}) + end + end + end + describe "log helper methods" do let(:device){ Lumberjack::Device::Writer.new(output, :buffer_size => 0, :template => ":message") } diff --git a/spec/lumberjack_spec.rb b/spec/lumberjack_spec.rb index 1922c5a..568babe 100644 --- a/spec/lumberjack_spec.rb +++ b/spec/lumberjack_spec.rb @@ -48,6 +48,14 @@ expect(Lumberjack.context_tags).to eq("foo" => "bar") end end + + it "should be specify the context" do + context = Lumberjack::Context.new + context.tag(fog: "bar") + Lumberjack.use_context(context) do + expect(Lumberjack.context_tags).to eq("fog" => "bar") + end + end end describe "unit of work" do From fb6738fe9f4f2325a7791bd240372024e909385a Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 21:45:09 -0700 Subject: [PATCH 02/12] standardrb --- .standard.yml | 12 ++ Gemfile | 21 ++- Rakefile | 2 +- lib/lumberjack.rb | 44 +++--- lib/lumberjack/device.rb | 24 +-- .../device/date_rolling_log_file.rb | 14 +- lib/lumberjack/device/log_file.rb | 12 +- lib/lumberjack/device/multi.rb | 12 +- lib/lumberjack/device/null.rb | 2 +- lib/lumberjack/device/rolling_log_file.rb | 62 +++++--- .../device/size_rolling_log_file.rb | 18 +-- lib/lumberjack/device/writer.rb | 24 +-- lib/lumberjack/formatter.rb | 30 ++-- .../formatter/date_time_formatter.rb | 5 +- .../formatter/exception_formatter.rb | 2 - lib/lumberjack/formatter/id_formatter.rb | 4 +- .../formatter/pretty_print_formatter.rb | 8 +- lib/lumberjack/log_entry.rb | 18 +-- lib/lumberjack/logger.rb | 42 +++--- lib/lumberjack/rack.rb | 6 +- lib/lumberjack/rack/request_id.rb | 2 +- lib/lumberjack/rack/unit_of_work.rb | 2 +- lib/lumberjack/severity.rb | 3 +- lib/lumberjack/tag_formatter.rb | 4 +- lib/lumberjack/tagged_logger_support.rb | 1 - lib/lumberjack/tagged_logging.rb | 2 +- lib/lumberjack/tags.rb | 2 +- lib/lumberjack/template.rb | 4 +- lumberjack.gemspec | 25 ++-- spec/context_spec.rb | 2 - spec/device/date_rolling_log_file_spec.rb | 24 ++- spec/device/log_file_spec.rb | 12 +- spec/device/multi_spec.rb | 4 +- spec/device/null_spec.rb | 4 +- spec/device/rolling_log_file_spec.rb | 32 ++-- spec/device/size_rolling_log_file_spec.rb | 32 ++-- spec/device/writer_spec.rb | 68 ++++++--- spec/formatter/date_time_formatter_spec.rb | 4 +- spec/formatter/exception_formatter_spec.rb | 6 +- spec/formatter/id_formatter_spec.rb | 8 +- spec/formatter/inspect_formatter_spec.rb | 4 +- spec/formatter/object_formatter_spec.rb | 4 +- spec/formatter/pretty_print_formatter_spec.rb | 4 +- spec/formatter/string_formatter_spec.rb | 4 +- spec/formatter/strip_formatter_spec.rb | 4 +- spec/formatter/structured_formatter_spec.rb | 28 ++-- spec/formatter_spec.rb | 17 +-- spec/log_entry_spec.rb | 6 +- spec/logger_spec.rb | 137 ++++++++++-------- spec/lumberjack_spec.rb | 4 +- spec/rack/context_spec.rb | 6 +- spec/rack/request_id_spec.rb | 24 ++- spec/rack/unit_of_work_spec.rb | 12 +- spec/severity_spec.rb | 4 +- spec/spec_helper.rb | 8 +- spec/tag_formatter_spec.rb | 6 +- spec/tagged_logger_support_spec.rb | 8 +- spec/tagged_logging_spec.rb | 4 +- spec/tags_spec.rb | 2 - spec/template_spec.rb | 25 ++-- 60 files changed, 470 insertions(+), 444 deletions(-) create mode 100644 .standard.yml diff --git a/.standard.yml b/.standard.yml new file mode 100644 index 0000000..6c9ccb2 --- /dev/null +++ b/.standard.yml @@ -0,0 +1,12 @@ +# I really just have issues with the automatic "semantic blocks" + +ruby_version: 2.3 + +format: progress + +ignore: + - '**/*': + - Standard/SemanticBlocks + - 'spec/**/*': + - Lint/UselessAssignment + - Lint/Void diff --git a/Gemfile b/Gemfile index 851fabc..20a9020 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,19 @@ -source 'https://rubygems.org' -gemspec +source "https://rubygems.org" + +group :runtime do + gemspec +end + +group :development, :test do + gem "rake" + gem "rspec", "~> 3.9" + gem "timecop" + gem "appraisal" + + # Lock standard to a particular version, esp. cause it's still 0.x.x according to Semver + gem "standard", "0.5.2" +end + +group :doc do + gem "yard" +end diff --git a/Rakefile b/Rakefile index 368aa0f..bf3f641 100644 --- a/Rakefile +++ b/Rakefile @@ -3,7 +3,7 @@ require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) -task :default => :appraisals +task default: :appraisals desc "run the specs using appraisal" task :appraisals do diff --git a/lib/lumberjack.rb b/lib/lumberjack.rb index 5df9f46..066d18d 100644 --- a/lib/lumberjack.rb +++ b/lib/lumberjack.rb @@ -1,27 +1,26 @@ # frozen_string_literals: true -require 'rbconfig' -require 'time' -require 'thread' -require 'securerandom' -require 'logger' +require "rbconfig" +require "time" +require "securerandom" +require "logger" module Lumberjack - LINE_SEPARATOR = (RbConfig::CONFIG['host_os'].match(/mswin/i) ? "\r\n" : "\n") + LINE_SEPARATOR = (/mswin/i.match?(RbConfig::CONFIG["host_os"]) ? "\r\n" : "\n") - require_relative "lumberjack/severity.rb" - require_relative "lumberjack/formatter.rb" + require_relative "lumberjack/severity" + require_relative "lumberjack/formatter" - require_relative "lumberjack/context.rb" - require_relative "lumberjack/log_entry.rb" - require_relative "lumberjack/device.rb" - require_relative "lumberjack/logger.rb" - require_relative "lumberjack/tags.rb" - require_relative "lumberjack/tag_formatter.rb" - require_relative "lumberjack/tagged_logger_support.rb" - require_relative "lumberjack/tagged_logging.rb" - require_relative "lumberjack/template.rb" - require_relative "lumberjack/rack.rb" + require_relative "lumberjack/context" + require_relative "lumberjack/log_entry" + require_relative "lumberjack/device" + require_relative "lumberjack/logger" + require_relative "lumberjack/tags" + require_relative "lumberjack/tag_formatter" + require_relative "lumberjack/tagged_logger_support" + require_relative "lumberjack/tagged_logging" + require_relative "lumberjack/template" + require_relative "lumberjack/rack" class << self # Define a unit of work within a block. Within the block supplied to this @@ -62,7 +61,7 @@ def context(&block) current_context || Context.new end end - + # Set the context to use within a block. def use_context(context, &block) current_context = Thread.current[:lumberjack_context] @@ -73,7 +72,7 @@ def use_context(context, &block) Thread.current[:lumberjack_context] = current_context end end - + # Return true if inside a context block. def context? !!Thread.current[:lumberjack_context] @@ -82,14 +81,13 @@ def context? # Return the tags from the current context or nil if there are no tags. def context_tags context = Thread.current[:lumberjack_context] - context.tags if context + context&.tags end # Set tags on the current context def tag(tags) context = Thread.current[:lumberjack_context] - context.tag(tags) if context + context&.tag(tags) end - end end diff --git a/lib/lumberjack/device.rb b/lib/lumberjack/device.rb index 2db49d3..1ee219c 100644 --- a/lib/lumberjack/device.rb +++ b/lib/lumberjack/device.rb @@ -4,37 +4,37 @@ module Lumberjack # This is an abstract class for logging devices. Subclasses must implement the +write+ method and # may implement the +close+ and +flush+ methods if applicable. class Device - require_relative "device/writer.rb" - require_relative "device/log_file.rb" - require_relative "device/rolling_log_file.rb" - require_relative "device/date_rolling_log_file.rb" - require_relative "device/size_rolling_log_file.rb" - require_relative "device/multi.rb" - require_relative "device/null.rb" + require_relative "device/writer" + require_relative "device/log_file" + require_relative "device/rolling_log_file" + require_relative "device/date_rolling_log_file" + require_relative "device/size_rolling_log_file" + require_relative "device/multi" + require_relative "device/null" # Subclasses must implement this method to write a LogEntry. def write(entry) raise NotImplementedError end - + # Subclasses may implement this method to close the device. def close flush end - + # Subclasses may implement this method to reopen the device. def reopen(logdev = nil) flush end - + # Subclasses may implement this method to flush any buffers used by the device. def flush end - + # Subclasses may implement this method to get the format for log timestamps. def datetime_format end - + # Subclasses may implement this method to set a format for log timestamps. def datetime_format=(format) end diff --git a/lib/lumberjack/device/date_rolling_log_file.rb b/lib/lumberjack/device/date_rolling_log_file.rb index 9d924db..b255303 100644 --- a/lib/lumberjack/device/date_rolling_log_file.rb +++ b/lib/lumberjack/device/date_rolling_log_file.rb @@ -1,6 +1,6 @@ # frozen_string_literals: true -require 'date' +require "date" module Lumberjack class Device @@ -16,7 +16,7 @@ class DateRollingLogFile < RollingLogFile def initialize(path, options = {}) @manual = options[:manual] @file_date = Date.today - if options[:roll] && options[:roll].to_s.match(/(daily)|(weekly)|(monthly)/i) + if options[:roll]&.to_s&.match(/(daily)|(weekly)|(monthly)/i) @roll_period = $~[0].downcase.to_sym options.delete(:roll) else @@ -28,11 +28,11 @@ def initialize(path, options = {}) def archive_file_suffix case @roll_period when :weekly - "#{@file_date.strftime('week-of-%Y-%m-%d')}" + @file_date.strftime("week-of-%Y-%m-%d").to_s when :monthly - "#{@file_date.strftime('%Y-%m')}" + @file_date.strftime("%Y-%m").to_s else - "#{@file_date.strftime('%Y-%m-%d')}" + @file_date.strftime("%Y-%m-%d").to_s end end @@ -54,9 +54,9 @@ def roll_file? end end end - + protected - + def after_roll @file_date = Date.today end diff --git a/lib/lumberjack/device/log_file.rb b/lib/lumberjack/device/log_file.rb index e7f346d..7fd31b7 100644 --- a/lib/lumberjack/device/log_file.rb +++ b/lib/lumberjack/device/log_file.rb @@ -1,6 +1,6 @@ # frozen_string_literals: true -require 'fileutils' +require "fileutils" module Lumberjack class Device @@ -10,23 +10,23 @@ class LogFile < Writer # The absolute path of the file being logged to. attr_reader :path - + # Create a logger to the file at +path+. Options are passed through to the Writer constructor. def initialize(path, options = {}) @path = File.expand_path(path) FileUtils.mkdir_p(File.dirname(@path)) super(file_stream, options) end - + def reopen(logdev = nil) close @stream = file_stream end - + private - + def file_stream - File.new(@path, 'a', :encoding => EXTERNAL_ENCODING) + File.new(@path, "a", encoding: EXTERNAL_ENCODING) end end end diff --git a/lib/lumberjack/device/multi.rb b/lib/lumberjack/device/multi.rb index e9da439..d8b77c5 100644 --- a/lib/lumberjack/device/multi.rb +++ b/lib/lumberjack/device/multi.rb @@ -7,35 +7,35 @@ class Multi < Device def initialize(*devices) @devices = devices.flatten end - + def write(entry) @devices.each do |device| device.write(entry) end end - + def flush @devices.each do |device| device.flush end end - + def close @devices.each do |device| device.close end end - + def reopen(logdev = nil) @devices.each do |device| device.reopen(logdev = nil) end end - + def datetime_format @devices.detect(&:datetime_format).datetime_format end - + def datetime_format=(format) @devices.each do |device| device.datetime_format = format diff --git a/lib/lumberjack/device/null.rb b/lib/lumberjack/device/null.rb index 1f85084..d41f1f6 100644 --- a/lib/lumberjack/device/null.rb +++ b/lib/lumberjack/device/null.rb @@ -7,7 +7,7 @@ class Device class Null < Device def initialize(*args) end - + def write(entry) end end diff --git a/lib/lumberjack/device/rolling_log_file.rb b/lib/lumberjack/device/rolling_log_file.rb index 9ccb507..0c208de 100644 --- a/lib/lumberjack/device/rolling_log_file.rb +++ b/lib/lumberjack/device/rolling_log_file.rb @@ -14,35 +14,43 @@ class Device class RollingLogFile < LogFile attr_reader :path attr_accessor :keep - + def initialize(path, options = {}) @path = File.expand_path(path) @keep = options[:keep] super(path, options) - @file_inode = stream.lstat.ino rescue nil + @file_inode = begin + stream.lstat.ino + rescue + nil + end @@rolls = [] @next_stat_check = Time.now.to_f @min_roll_check = (options[:min_roll_check] || 1.0).to_f end - + # Returns a suffix that will be appended to the file name when it is archived.. The suffix should # change after it is time to roll the file. The log file will be renamed when it is rolled. def archive_file_suffix raise NotImplementedError end - + # Return +true+ if the file should be rolled. def roll_file? raise NotImplementedError end - + # Roll the log file by renaming it to the archive file name and then re-opening a stream to the log # file path. Rolling a file is safe in multi-threaded or multi-process environments. def roll_file! #:nodoc: do_once(stream) do archive_file = "#{path}.#{archive_file_suffix}" stream.flush - current_inode = File.stat(path).ino rescue nil + current_inode = begin + File.stat(path).ino + rescue + nil + end if @file_inode && current_inode == @file_inode && !File.exist?(archive_file) && File.exist?(path) begin File.rename(path, archive_file) @@ -59,41 +67,49 @@ def roll_file! #:nodoc: end protected - + # This method will be called after a file has been rolled. Subclasses can # implement code to reset the state of the device. This method is thread safe. def after_roll end - + # Handle rolling the file before flushing. def before_flush # :nodoc: if @min_roll_check <= 0.0 || Time.now.to_f >= @next_stat_check @next_stat_check += @min_roll_check - path_inode = File.lstat(path).ino rescue nil + path_inode = begin + File.lstat(path).ino + rescue + nil + end if path_inode != @file_inode @file_inode = path_inode reopen_file - else - roll_file! if roll_file? + elsif roll_file? + roll_file! end end end - + private def reopen_file old_stream = stream - new_stream = File.open(path, 'a', encoding: EXTERNAL_ENCODING) + new_stream = File.open(path, "a", encoding: EXTERNAL_ENCODING) new_stream.sync = true if buffer_size > 0 - @file_inode = new_stream.lstat.ino rescue nil + @file_inode = begin + new_stream.lstat.ino + rescue + nil + end self.stream = new_stream old_stream.close end end - + def cleanup_files! if keep - files = Dir.glob("#{path}.*").collect{|f| [f, File.ctime(f)]}.sort{|a,b| b.last <=> a.last}.collect{|a| a.first} + files = Dir.glob("#{path}.*").collect { |f| [f, File.ctime(f)] }.sort { |a, b| b.last <=> a.last }.collect { |a| a.first } if files.size > keep files[keep, files.length].each do |f| File.delete(f) @@ -101,7 +117,7 @@ def cleanup_files! end end end - + def do_once(file) begin file.flock(File::LOCK_EX) @@ -110,11 +126,19 @@ def do_once(file) return end begin - verify = file.lstat rescue nil + verify = begin + file.lstat + rescue + nil + end # Execute only if the file we locked is still the same one that needed to be rolled yield if verify && verify.ino == @file_inode && verify.size > 0 ensure - file.flock(File::LOCK_UN) rescue nil + begin + file.flock(File::LOCK_UN) + rescue + nil + end end end end diff --git a/lib/lumberjack/device/size_rolling_log_file.rb b/lib/lumberjack/device/size_rolling_log_file.rb index dad1de2..699ade5 100644 --- a/lib/lumberjack/device/size_rolling_log_file.rb +++ b/lib/lumberjack/device/size_rolling_log_file.rb @@ -8,30 +8,30 @@ class Device # production.log.1, then production.log.2, etc. class SizeRollingLogFile < RollingLogFile attr_reader :max_size - + # Create an new log device to the specified file. The maximum size of the log file is specified with # the :max_size option. The unit can also be specified: "32K", "100M", "2G" are all valid. def initialize(path, options = {}) @manual = options[:manual] @max_size = options[:max_size] if @max_size.is_a?(String) - if @max_size.match(/^(\d+(\.\d+)?)([KMG])?$/i) + if @max_size =~ /^(\d+(\.\d+)?)([KMG])?$/i @max_size = $~[1].to_f units = $~[3].to_s.upcase case units when "K" @max_size *= 1024 when "M" - @max_size *= 1024 ** 2 + @max_size *= 1024**2 when "G" - @max_size *= 1024 ** 3 + @max_size *= 1024**3 end @max_size = @max_size.round else raise ArgumentError.new("illegal value for :max_size (#{@max_size})") end end - + super end @@ -44,15 +44,15 @@ def roll_file? rescue SystemCallError false end - + protected - + # Calculate the next archive file name extension. def next_archive_number # :nodoc: max = 0 Dir.glob("#{path}.*").each do |filename| - if filename.match(/\.\d+$/) - suffix = filename.split('.').last.to_i + if /\.\d+$/.match?(filename) + suffix = filename.split(".").last.to_i max = suffix if suffix > max end end diff --git a/lib/lumberjack/device/writer.rb b/lib/lumberjack/device/writer.rb index 153f134..24f129c 100644 --- a/lib/lumberjack/device/writer.rb +++ b/lib/lumberjack/device/writer.rb @@ -71,7 +71,7 @@ def initialize(stream, options = {}) @template = template else additional_lines = (options[:additional_lines] || DEFAULT_ADDITIONAL_LINES_TEMPLATE) - @template = Template.new(template, :additional_lines => additional_lines, :time_format => options[:time_format]) + @template = Template.new(template, additional_lines: additional_lines, time_format: options[:time_format]) end end @@ -133,14 +133,10 @@ def datetime_format=(format) protected # Set the underlying stream. - def stream=(stream) - @stream = stream - end + attr_writer :stream # Get the underlying stream. - def stream - @stream - end + attr_reader :stream private @@ -149,10 +145,10 @@ def write_to_stream(lines) lines = lines.first if lines.is_a?(Array) && lines.size == 1 out = nil - if lines.is_a?(Array) - out = "#{lines.join(Lumberjack::LINE_SEPARATOR)}#{Lumberjack::LINE_SEPARATOR}" + out = if lines.is_a?(Array) + "#{lines.join(Lumberjack::LINE_SEPARATOR)}#{Lumberjack::LINE_SEPARATOR}" else - out = "#{lines}#{Lumberjack::LINE_SEPARATOR}" + "#{lines}#{Lumberjack::LINE_SEPARATOR}" end begin @@ -170,9 +166,13 @@ def write_to_stream(lines) end end end - stream.flush rescue nil + begin + stream.flush + rescue + nil + end rescue => e - $stderr.write("#{e.class.name}: #{e.message}#{' at ' + e.backtrace.first if e.backtrace}") + $stderr.write("#{e.class.name}: #{e.message}#{" at " + e.backtrace.first if e.backtrace}") $stderr.write(out) $stderr.flush end diff --git a/lib/lumberjack/formatter.rb b/lib/lumberjack/formatter.rb index 38aeb38..ea8e212 100644 --- a/lib/lumberjack/formatter.rb +++ b/lib/lumberjack/formatter.rb @@ -12,15 +12,15 @@ module Lumberjack # # Enumerable objects (including Hash and Array) will call the formatter recursively for each element. class Formatter - require_relative "formatter/date_time_formatter.rb" - require_relative "formatter/exception_formatter.rb" - require_relative "formatter/id_formatter.rb" - require_relative "formatter/inspect_formatter.rb" - require_relative "formatter/object_formatter.rb" - require_relative "formatter/pretty_print_formatter.rb" - require_relative "formatter/string_formatter.rb" - require_relative "formatter/strip_formatter.rb" - require_relative "formatter/structured_formatter.rb" + require_relative "formatter/date_time_formatter" + require_relative "formatter/exception_formatter" + require_relative "formatter/id_formatter" + require_relative "formatter/inspect_formatter" + require_relative "formatter/object_formatter" + require_relative "formatter/pretty_print_formatter" + require_relative "formatter/string_formatter" + require_relative "formatter/strip_formatter" + require_relative "formatter/structured_formatter" class << self # Returns a new empty formatter with no mapping. For historical reasons, a formatter @@ -30,7 +30,7 @@ def empty new.clear end end - + def initialize @class_formatters = {} @module_formatters = {} @@ -72,10 +72,10 @@ def add(klass, formatter = nil, &block) remove(klass) else if formatter.is_a?(Symbol) - formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/){|m| $~[2].upcase}}Formatter" + formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/) { |m| $~[2].upcase }}Formatter" formatter = Formatter.const_get(formatter_class_name).new end - + Array(klass).each do |k| if k.class == Module @module_formatters[k] = formatter @@ -106,7 +106,7 @@ def remove(klass) end self end - + # Remove all formatters including the default formatter. Can be chained to add method calls. def clear @class_formatters.clear @@ -117,7 +117,7 @@ def clear # Format a message object as a string. def format(message) formatter = formatter_for(message.class) - if formatter && formatter.respond_to?(:call) + if formatter&.respond_to?(:call) formatter.call(message) else message @@ -135,7 +135,7 @@ def call(severity, timestamp, progname, msg) # Find the formatter for a class by looking it up using the class hierarchy. def formatter_for(klass) #:nodoc: check_modules = true - while klass != nil do + until klass.nil? formatter = @class_formatters[klass.name] return formatter if formatter diff --git a/lib/lumberjack/formatter/date_time_formatter.rb b/lib/lumberjack/formatter/date_time_formatter.rb index 2f2d15a..88dc720 100644 --- a/lib/lumberjack/formatter/date_time_formatter.rb +++ b/lib/lumberjack/formatter/date_time_formatter.rb @@ -5,13 +5,12 @@ class Formatter # Format a Date, Time, or DateTime object. If you don't specify a format in the constructor, # it will use the ISO-8601 format. class DateTimeFormatter - attr_reader :format - + def initialize(format = nil) @format = format.dup.to_s.freeze unless format.nil? end - + def call(obj) if @format && obj.respond_to?(:strftime) obj.strftime(@format) diff --git a/lib/lumberjack/formatter/exception_formatter.rb b/lib/lumberjack/formatter/exception_formatter.rb index c0066ca..6a7cf24 100644 --- a/lib/lumberjack/formatter/exception_formatter.rb +++ b/lib/lumberjack/formatter/exception_formatter.rb @@ -7,7 +7,6 @@ class Formatter # passed to this object and the returned array is what will be logged. You can # use this to clean out superfluous lines. class ExceptionFormatter - attr_accessor :backtrace_cleaner def initialize(backtrace_cleaner = nil) @@ -33,7 +32,6 @@ def clean_backtrace(trace) trace end end - end end end diff --git a/lib/lumberjack/formatter/id_formatter.rb b/lib/lumberjack/formatter/id_formatter.rb index b6eedd6..deaba7b 100644 --- a/lib/lumberjack/formatter/id_formatter.rb +++ b/lib/lumberjack/formatter/id_formatter.rb @@ -9,11 +9,11 @@ class IdFormatter def initialize(id_attribute = :id) @id_attribute = id_attribute end - + def call(obj) if obj.respond_to?(@id_attribute) id = obj.send(@id_attribute) - { "class" => obj.class.name, "id" => id } + {"class" => obj.class.name, "id" => id} else obj.to_s end diff --git a/lib/lumberjack/formatter/pretty_print_formatter.rb b/lib/lumberjack/formatter/pretty_print_formatter.rb index 36e2a47..3a2cd1b 100644 --- a/lib/lumberjack/formatter/pretty_print_formatter.rb +++ b/lib/lumberjack/formatter/pretty_print_formatter.rb @@ -1,20 +1,20 @@ # frozen_string_literals: true -require 'pp' -require 'stringio' +require "pp" +require "stringio" module Lumberjack class Formatter # Format an object with it's pretty print method. class PrettyPrintFormatter attr_accessor :width - + # Create a new formatter. The maximum width of the message can be specified with the width # parameter (defaults to 79 characters). def initialize(width = 79) @width = width end - + def call(obj) s = StringIO.new PP.pp(obj, s) diff --git a/lib/lumberjack/log_entry.rb b/lib/lumberjack/log_entry.rb index 4b3069f..9094128 100644 --- a/lib/lumberjack/log_entry.rb +++ b/lib/lumberjack/log_entry.rb @@ -17,10 +17,10 @@ def initialize(time, severity, message, progname, pid, tags) @progname = progname @pid = pid # backward compatibility with 1.0 API where the last argument was the unit of work id - if tags.nil? || tags.is_a?(Hash) - @tags = tags + @tags = if tags.nil? || tags.is_a?(Hash) + tags else - @tags = { UNIT_OF_WORK_ID => tags } + {UNIT_OF_WORK_ID => tags} end end @@ -29,7 +29,7 @@ def severity_label end def to_s - "[#{time.strftime(TIME_FORMAT)}.#{(time.usec / 1000.0).round.to_s.rjust(3, '0')} #{severity_label} #{progname}(#{pid})#{tags_to_s}] #{message}" + "[#{time.strftime(TIME_FORMAT)}.#{(time.usec / 1000.0).round.to_s.rjust(3, "0")} #{severity_label} #{progname}(#{pid})#{tags_to_s}] #{message}" end def inspect @@ -46,10 +46,10 @@ def unit_of_work_id=(value) if tags tags[UNIT_OF_WORK_ID] = value else - @tags = { UNIT_OF_WORK_ID => value } + @tags = {UNIT_OF_WORK_ID => value} end end - + # Return the tag with the specified name. def tag(name) tags[name.to_s] if tags @@ -58,10 +58,8 @@ def tag(name) private def tags_to_s - tags_string = String.new - if tags - tags.each { |name, value| tags_string << " #{name}:#{value.inspect}" } - end + tags_string = "" + tags&.each { |name, value| tags_string << " #{name}:#{value.inspect}" } tags_string end end diff --git a/lib/lumberjack/logger.rb b/lib/lumberjack/logger.rb index 39e3ec0..ab1345a 100644 --- a/lib/lumberjack/logger.rb +++ b/lib/lumberjack/logger.rb @@ -63,7 +63,7 @@ class Logger # * :max_size - If the log device is a file path, it will be a Device::SizeRollingLogFile if this is set. # # All other options are passed to the device constuctor. - def initialize(device = STDOUT, options = {}) + def initialize(device = $stdout, options = {}) options = options.dup self.level = options.delete(:level) || INFO self.progname = options.delete(:progname) @@ -100,19 +100,19 @@ def level thread_local_value(:lumberjack_logger_level) || @level end - alias_method :sev_threshold, :level + alias sev_threshold level # Set the log level using either an integer level like Logger::INFO or a label like # :info or "info" def level=(value) - if value.is_a?(Integer) - @level = value + @level = if value.is_a?(Integer) + value else - @level = Severity::label_to_level(value) + Severity.label_to_level(value) end end - alias_method :sev_threshold=, :level= + alias sev_threshold= level= # Set the Lumberjack::Formatter used to format objects for logging as messages. def formatter=(value) @@ -137,7 +137,7 @@ def formatter # in an array under the "tagged" tag. So calling `logger.tagged("foo", "bar")` will result # in tags `{"tagged" => ["foo", "bar"]}`. def tagged_logger! - self.extend(TaggedLoggerSupport) + extend(TaggedLoggerSupport) self end @@ -173,10 +173,10 @@ def add_entry(severity, message, progname = nil, tags = nil) if current_tags.empty? tags = Tags.stringify_keys(tags) unless tags.nil? else - if tags.nil? - tags = current_tags.dup + tags = if tags.nil? + current_tags.dup else - tags = current_tags.merge(Tags.stringify_keys(tags)) + current_tags.merge(Tags.stringify_keys(tags)) end end tags = Tags.expand_runtime_values(tags) @@ -203,7 +203,7 @@ def add(severity, message = nil, progname = nil, &block) add_entry(severity, message, progname) end - alias_method :log, :add + alias log add # Flush the logging device. Messages are not guaranteed to be written until this method is called. def flush @@ -325,7 +325,7 @@ def <<(msg) def silence(temporary_level = ERROR, &block) if silencer unless temporary_level.is_a?(Integer) - temporary_level = Severity::label_to_level(temporary_level) + temporary_level = Severity.label_to_level(temporary_level) end push_thread_local_value(:lumberjack_logger_level, temporary_level, &block) else @@ -390,7 +390,7 @@ def tags tags.merge!(scope_tags) if scope_tags && !scope_tags.empty? tags end - + # Remove all tags on the current logger and logging context within a block. # You can still set new block scoped tags within the untagged block and provide # tags on individual log methods. @@ -491,12 +491,12 @@ def open_device(device, options) #:nodoc: end def write_to_device(entry) #:nodoc: - begin - device.write(entry) - rescue => e - $stderr.puts("#{e.class.name}: #{e.message}#{' at ' + e.backtrace.first if e.backtrace}") - $stderr.puts(entry.to_s) - end + device.write(entry) + rescue => e + # rubocop:disable Style/StderrPuts + $stderr.puts("#{e.class.name}: #{e.message}#{" at " + e.backtrace.first if e.backtrace}") + $stderr.puts(entry.to_s) + # rubocop:enable Style/StderrPuts end # Create a thread that will periodically call flush. @@ -505,12 +505,12 @@ def create_flusher_thread(flush_seconds) #:nodoc: begin logger = self Thread.new do - while !closed? + until closed? begin sleep(flush_seconds) logger.flush if Time.now - logger.last_flushed_at >= flush_seconds rescue => e - $stderr.puts("Error flushing log: #{e.inspect}") + warn("Error flushing log: #{e.inspect}") end end end diff --git a/lib/lumberjack/rack.rb b/lib/lumberjack/rack.rb index 5f1e295..bdcabc7 100644 --- a/lib/lumberjack/rack.rb +++ b/lib/lumberjack/rack.rb @@ -2,8 +2,8 @@ module Lumberjack module Rack - require_relative "rack/unit_of_work.rb" - require_relative "rack/request_id.rb" - require_relative "rack/context.rb" + require_relative "rack/unit_of_work" + require_relative "rack/request_id" + require_relative "rack/context" end end diff --git a/lib/lumberjack/rack/request_id.rb b/lib/lumberjack/rack/request_id.rb index a738be8..065bec0 100644 --- a/lib/lumberjack/rack/request_id.rb +++ b/lib/lumberjack/rack/request_id.rb @@ -16,7 +16,7 @@ def initialize(app, abbreviated = false) def call(env) request_id = env[REQUEST_ID] if request_id && @abbreviated - request_id = request_id.split('-', 2).first + request_id = request_id.split("-", 2).first end Lumberjack.unit_of_work(request_id) do @app.call(env) diff --git a/lib/lumberjack/rack/unit_of_work.rb b/lib/lumberjack/rack/unit_of_work.rb index dc2e4d8..5cbb7ad 100644 --- a/lib/lumberjack/rack/unit_of_work.rb +++ b/lib/lumberjack/rack/unit_of_work.rb @@ -6,7 +6,7 @@ class UnitOfWork def initialize(app) @app = app end - + def call(env) Lumberjack.unit_of_work do @app.call(env) diff --git a/lib/lumberjack/severity.rb b/lib/lumberjack/severity.rb index 38c5b7d..2f9bc1e 100644 --- a/lib/lumberjack/severity.rb +++ b/lib/lumberjack/severity.rb @@ -11,7 +11,7 @@ module Severity FATAL = ::Logger::Severity::FATAL UNKNOWN = ::Logger::Severity::UNKNOWN - SEVERITY_LABELS = %w(DEBUG INFO WARN ERROR FATAL UNKNOWN).freeze + SEVERITY_LABELS = %w[DEBUG INFO WARN ERROR FATAL UNKNOWN].freeze class << self def level_to_label(severity) @@ -22,6 +22,5 @@ def label_to_level(label) SEVERITY_LABELS.index(label.to_s.upcase) || UNKNOWN end end - end end diff --git a/lib/lumberjack/tag_formatter.rb b/lib/lumberjack/tag_formatter.rb index c07c7e4..c493838 100644 --- a/lib/lumberjack/tag_formatter.rb +++ b/lib/lumberjack/tag_formatter.rb @@ -9,7 +9,6 @@ module Lumberjack # tag_formatter.add(["password", "email"]) { |value| "***" } # tag_formatter.add("finished_at", Lumberjack::Formatter::DateTimeFormatter.new("%Y-%m-%dT%H:%m:%S%z")) class TagFormatter - def initialize @formatters = {} @default_formatter = nil @@ -87,12 +86,11 @@ def dereference_formatter(formatter) if formatter.is_a?(TaggedLoggerSupport::Formatter) formatter.__formatter elsif formatter.is_a?(Symbol) - formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/){|m| $~[2].upcase}}Formatter" + formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/) { |m| $~[2].upcase }}Formatter" Formatter.const_get(formatter_class_name).new else formatter end end - end end diff --git a/lib/lumberjack/tagged_logger_support.rb b/lib/lumberjack/tagged_logger_support.rb index a5cfb89..ff69cbb 100644 --- a/lib/lumberjack/tagged_logger_support.rb +++ b/lib/lumberjack/tagged_logger_support.rb @@ -6,7 +6,6 @@ module Lumberjack # Methods to make Lumberjack::Logger API compatible with ActiveSupport::TaggedLogger. module TaggedLoggerSupport - class Formatter < DelegateClass(Lumberjack::Formatter) extend Forwardable def_delegators :@logger, :tagged, :push_tags, :pop_tags, :clear_tags! diff --git a/lib/lumberjack/tagged_logging.rb b/lib/lumberjack/tagged_logging.rb index 7635795..bc3fde6 100644 --- a/lib/lumberjack/tagged_logging.rb +++ b/lib/lumberjack/tagged_logging.rb @@ -10,7 +10,7 @@ def included(base) base.singleton_class.send(:prepend, ClassMethods) end end - + module ClassMethods def new(logger) if logger.is_a?(Lumberjack::Logger) diff --git a/lib/lumberjack/tags.rb b/lib/lumberjack/tags.rb index 0c96272..b282a45 100644 --- a/lib/lumberjack/tags.rb +++ b/lib/lumberjack/tags.rb @@ -27,7 +27,7 @@ def expand_runtime_values(hash) if hash.all? { |key, value| key.is_a?(String) && !value.is_a?(Proc) } return hash end - + copy = {} hash.each do |key, value| if value.is_a?(Proc) && (value.arity == 0 || value.arity == -1) diff --git a/lib/lumberjack/template.rb b/lib/lumberjack/template.rb index f0d9353..cfc2b9c 100644 --- a/lib/lumberjack/template.rb +++ b/lib/lumberjack/template.rb @@ -14,7 +14,7 @@ module Lumberjack # If your tag name contains characters other than alpha numerics and the underscore, you must surround it # with curly brackets: `:{http.request-id}`. class Template - TEMPLATE_ARGUMENT_ORDER = %w(:time :severity :progname :pid :message :tags).freeze + TEMPLATE_ARGUMENT_ORDER = %w[:time :severity :progname :pid :message :tags].freeze MILLISECOND_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%3N" MICROSECOND_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%6N" PLACEHOLDER_PATTERN = /:(([a-z0-9_]+)|({[^}]+}))/i.freeze @@ -86,7 +86,7 @@ def call(entry) def tag_args(tags, tag_vars) return [nil] * (tag_vars.size + 1) if tags.nil? || tags.size == 0 - tags_string = String.new + tags_string = "" tags.each do |name, value| unless value.nil? || tag_vars.include?(name) value = value.to_s diff --git a/lumberjack.gemspec b/lumberjack.gemspec index dfb73a2..a4fcbec 100644 --- a/lumberjack.gemspec +++ b/lumberjack.gemspec @@ -1,8 +1,8 @@ Gem::Specification.new do |spec| - spec.name = 'lumberjack' - spec.version = File.read(File.expand_path("../VERSION", __FILE__)).strip - spec.authors = ['Brian Durand'] - spec.email = ['bbdurand@gmail.com'] + spec.name = "lumberjack" + spec.version = File.read(File.join(__dir__, "VERSION")).strip + spec.authors = ["Brian Durand"] + spec.email = ["bbdurand@gmail.com"] spec.summary = "A simple, powerful, and very fast logging utility that can be a drop in replacement for Logger or ActiveSupport::BufferedLogger." spec.homepage = "https://github.com/bdurand/lumberjack" @@ -10,7 +10,7 @@ Gem::Specification.new do |spec| # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. - ignore_files = %w( + ignore_files = %w[ . Appraisals Gemfile @@ -18,17 +18,14 @@ Gem::Specification.new do |spec| Rakefile gemfiles/ spec/ - ) - spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do - `git ls-files -z`.split("\x0").reject{ |f| ignore_files.any?{ |path| f.start_with?(path) } } + ] + spec.files = Dir.chdir(__dir__) do + `git ls-files -z`.split("\x0").reject { |f| ignore_files.any? { |path| f.start_with?(path) } } end - spec.require_paths = ['lib'] + spec.require_paths = ["lib"] - spec.required_ruby_version = '>= 2.3.0' + spec.required_ruby_version = ">= 2.3.0" - spec.add_development_dependency("rspec", ["~> 3.0"]) - spec.add_development_dependency("timecop") - spec.add_development_dependency "rake" - spec.add_development_dependency "appraisal" + spec.add_development_dependency "bundler" end diff --git a/spec/context_spec.rb b/spec/context_spec.rb index 50f381b..6f6c178 100644 --- a/spec/context_spec.rb +++ b/spec/context_spec.rb @@ -1,7 +1,6 @@ require "spec_helper" describe Lumberjack::Context do - it "should have tags" do context = Lumberjack::Context.new expect(context.tags).to eq({}) @@ -20,5 +19,4 @@ expect(context.tags).to eq({"foo" => "other", "baz" => "boo", "stuff" => "nonsense"}) expect(parent.tags).to eq({"foo" => "bar", "baz" => "boo"}) end - end diff --git a/spec/device/date_rolling_log_file_spec.rb b/spec/device/date_rolling_log_file_spec.rb index 42c91a2..d9054da 100644 --- a/spec/device/date_rolling_log_file_spec.rb +++ b/spec/device/date_rolling_log_file_spec.rb @@ -1,7 +1,6 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Device::DateRollingLogFile do - before :all do create_tmp_dir end @@ -14,13 +13,13 @@ delete_tmp_files end - let(:one_day){ 60 * 60 * 24 } + let(:one_day) { 60 * 60 * 24 } it "should roll the file daily" do now = Time.now log_file = File.join(tmp_dir, "a#{rand(1000000000)}.log") - device = Lumberjack::Device::DateRollingLogFile.new(log_file, :roll => :daily, :template => ":message", :min_roll_check => 0) - logger = Lumberjack::Logger.new(device, :buffer_size => 2) + device = Lumberjack::Device::DateRollingLogFile.new(log_file, roll: :daily, template: ":message", min_roll_check: 0) + logger = Lumberjack::Logger.new(device, buffer_size: 2) Timecop.travel(now) do logger.error("test day one") logger.flush @@ -30,15 +29,15 @@ logger.close end - expect(File.read("#{log_file}.#{now.to_date.strftime('%Y-%m-%d')}")).to eq("test day one#{Lumberjack::LINE_SEPARATOR}") + expect(File.read("#{log_file}.#{now.to_date.strftime("%Y-%m-%d")}")).to eq("test day one#{Lumberjack::LINE_SEPARATOR}") expect(File.read(log_file)).to eq("test day two#{Lumberjack::LINE_SEPARATOR}") end it "should roll the file weekly" do now = Time.now log_file = File.join(tmp_dir, "b#{rand(1000000000)}.log") - device = Lumberjack::Device::DateRollingLogFile.new(log_file, :roll => :weekly, :template => ":message", :min_roll_check => 0) - logger = Lumberjack::Logger.new(device, :buffer_size => 2) + device = Lumberjack::Device::DateRollingLogFile.new(log_file, roll: :weekly, template: ":message", min_roll_check: 0) + logger = Lumberjack::Logger.new(device, buffer_size: 2) Timecop.freeze(now) do logger.error("test week one") logger.flush @@ -48,15 +47,15 @@ logger.close end - expect(File.read("#{log_file}.#{now.to_date.strftime('week-of-%Y-%m-%d')}")).to eq("test week one#{Lumberjack::LINE_SEPARATOR}") + expect(File.read("#{log_file}.#{now.to_date.strftime("week-of-%Y-%m-%d")}")).to eq("test week one#{Lumberjack::LINE_SEPARATOR}") expect(File.read(log_file)).to eq("test week two#{Lumberjack::LINE_SEPARATOR}") end it "should roll the file monthly" do now = Time.now log_file = File.join(tmp_dir, "c#{rand(1000000000)}.log") - device = Lumberjack::Device::DateRollingLogFile.new(log_file, :roll => :monthly, :template => ":message", :min_roll_check => 0) - logger = Lumberjack::Logger.new(device, :buffer_size => 2) + device = Lumberjack::Device::DateRollingLogFile.new(log_file, roll: :monthly, template: ":message", min_roll_check: 0) + logger = Lumberjack::Logger.new(device, buffer_size: 2) Timecop.freeze(now) do logger.error("test month one") logger.flush @@ -66,8 +65,7 @@ logger.close end - expect(File.read("#{log_file}.#{now.to_date.strftime('%Y-%m')}")).to eq("test month one#{Lumberjack::LINE_SEPARATOR}") + expect(File.read("#{log_file}.#{now.to_date.strftime("%Y-%m")}")).to eq("test month one#{Lumberjack::LINE_SEPARATOR}") expect(File.read(log_file)).to eq("test month two#{Lumberjack::LINE_SEPARATOR}") end - end diff --git a/spec/device/log_file_spec.rb b/spec/device/log_file_spec.rb index fa35525..89ca70a 100644 --- a/spec/device/log_file_spec.rb +++ b/spec/device/log_file_spec.rb @@ -1,8 +1,6 @@ -# -*- coding: utf-8 -*- -require 'spec_helper' +require "spec_helper" describe Lumberjack::Device::LogFile do - before :all do create_tmp_dir end @@ -17,11 +15,11 @@ it "should append to a file" do log_file = File.join(tmp_dir, "a#{rand(1000000000)}.log") - File.open(log_file, 'w') do |f| + File.open(log_file, "w") do |f| f.puts("Existing contents") end - device = Lumberjack::Device::LogFile.new(log_file, :template => ":message") + device = Lumberjack::Device::LogFile.new(log_file, template: ":message") device.write(Lumberjack::LogEntry.new(Time.now, 1, "New log entry", nil, $$, nil)) device.close @@ -30,7 +28,7 @@ it "properly handles messages with broken UTF-8 characters" do log_file = File.join(tmp_dir, "a#{rand(1000000000)}.log") - device = Lumberjack::Device::LogFile.new(log_file, :keep => 2, :buffer_size => 32767) + device = Lumberjack::Device::LogFile.new(log_file, keep: 2, buffer_size: 32767) message = [0xC4, 0x90, 0xE1, 0xBB].pack("c*").force_encoding "ASCII-8BIT" entry = Lumberjack::LogEntry.new(Time.now, 1, message, nil, $$, nil) @@ -47,7 +45,7 @@ it "should reopen the file" do log_file = File.join(tmp_dir, "a#{rand(1000000000)}.log") - device = Lumberjack::Device::LogFile.new(log_file, :template => ":message") + device = Lumberjack::Device::LogFile.new(log_file, template: ":message") device.write(Lumberjack::LogEntry.new(Time.now, 1, "message 1", nil, $$, nil)) device.close device.reopen diff --git a/spec/device/multi_spec.rb b/spec/device/multi_spec.rb index 8a4046c..7ab882f 100644 --- a/spec/device/multi_spec.rb +++ b/spec/device/multi_spec.rb @@ -1,12 +1,11 @@ require "spec_helper" describe Lumberjack::Device::Multi do - let(:output_1) { StringIO.new } let(:output_2) { StringIO.new } let(:device_1) { Lumberjack::Device::Writer.new(output_1, template: ":message") } let(:device_2) { Lumberjack::Device::Writer.new(output_2, template: ":severity - :message") } - let(:device) { Lumberjack::Device::Multi.new(device_1, device_2)} + let(:device) { Lumberjack::Device::Multi.new(device_1, device_2) } let(:entry) { Lumberjack::LogEntry.new(Time.now, Logger::INFO, "test", "app", 100, {}) } @@ -40,5 +39,4 @@ expect(device_1.datetime_format).to eq "%Y-%m-%d" expect(device_2.datetime_format).to eq "%Y-%m-%d" end - end diff --git a/spec/device/null_spec.rb b/spec/device/null_spec.rb index 78e39da..e505b9c 100644 --- a/spec/device/null_spec.rb +++ b/spec/device/null_spec.rb @@ -1,12 +1,10 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Device::Null do - it "should not generate any output" do device = Lumberjack::Device::Null.new device.write(Lumberjack::LogEntry.new(Time.now, 1, "New log entry", nil, $$, nil)) device.flush device.close end - end diff --git a/spec/device/rolling_log_file_spec.rb b/spec/device/rolling_log_file_spec.rb index ca7e938..3a05049 100644 --- a/spec/device/rolling_log_file_spec.rb +++ b/spec/device/rolling_log_file_spec.rb @@ -1,9 +1,8 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Device::RollingLogFile do - before :all do - Lumberjack::Device::SizeRollingLogFile #needed by jruby + Lumberjack::Device::SizeRollingLogFile # needed by jruby create_tmp_dir end @@ -15,10 +14,10 @@ delete_tmp_files end - let(:entry){ Lumberjack::LogEntry.new(Time.now, 1, "New log entry", nil, $$, nil) } + let(:entry) { Lumberjack::LogEntry.new(Time.now, 1, "New log entry", nil, $$, nil) } it "should check for rolling the log file on flush" do - device = Lumberjack::Device::RollingLogFile.new(File.join(tmp_dir, "test.log"), :buffer_size => 32767, :min_roll_check => 0) + device = Lumberjack::Device::RollingLogFile.new(File.join(tmp_dir, "test.log"), buffer_size: 32767, min_roll_check: 0) device.write(entry) expect(device).to receive(:roll_file?).twice.and_return(false) device.flush @@ -27,10 +26,10 @@ it "should roll the file by archiving the existing file and opening a new stream and calling after_roll" do log_file = File.join(tmp_dir, "test_2.log") - device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message", :buffer_size => 32767, :min_roll_check => 0) + device = Lumberjack::Device::RollingLogFile.new(log_file, template: ":message", buffer_size: 32767, min_roll_check: 0) expect(device).to receive(:roll_file?).and_return(false, true) expect(device).to receive(:after_roll) - allow(device).to receive_messages(:archive_file_suffix => "rolled") + allow(device).to receive_messages(archive_file_suffix: "rolled") device.write(entry) device.flush device.write(Lumberjack::LogEntry.new(Time.now, 1, "Another log entry", nil, $$, nil)) @@ -41,8 +40,8 @@ it "should reopen the file if the stream inode doesn't match the file path inode" do log_file = File.join(tmp_dir, "test_3.log") - device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message", :min_roll_check => 0) - allow(device).to receive_messages(:roll_file? => false) + device = Lumberjack::Device::RollingLogFile.new(log_file, template: ":message", min_roll_check: 0) + allow(device).to receive_messages(roll_file?: false) device.write(entry) device.flush File.rename(log_file, "#{log_file}.rolled") @@ -63,7 +62,7 @@ message = "This is a test message that is written to the log file to indicate what the state of the application is." logger_test = lambda do - device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => max_size, :template => ":message", :buffer_size => 32767, :min_roll_check => 0) + device = Lumberjack::Device::SizeRollingLogFile.new(log_file, max_size: max_size, template: ":message", buffer_size: 32767, min_roll_check: 0) threads = [] thread_count.times do threads << Thread.new do @@ -74,17 +73,17 @@ device.flush end end - threads.each{|thread| thread.value} + threads.each { |thread| thread.value } device.close end # Process.fork is unavailable on jruby so we need to use the java threads instead. - if RUBY_PLATFORM.match(/java/) + if RUBY_PLATFORM.match?(/java/) outer_threads = [] process_count.times do outer_threads << Thread.new(&logger_test) end - outer_threads.each{|thread| thread.value} + outer_threads.each { |thread| thread.value } else process_count.times do Process.fork(&logger_test) @@ -108,7 +107,7 @@ it "should only keep a specified number of archived log files" do log_file = File.join(tmp_dir, "test_5.log") - device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message", :keep => 2, :buffer_size => 32767, :min_roll_check => 0) + device = Lumberjack::Device::RollingLogFile.new(log_file, template: ":message", keep: 2, buffer_size: 32767, min_roll_check: 0) expect(device).to receive(:roll_file?).and_return(false, true, true, true) expect(device).to receive(:archive_file_suffix).and_return("delete", "another", "keep") t = Time.now @@ -130,9 +129,9 @@ let(:log_file) { File.join(tmp_dir, "test_6.log") } let(:device) do - device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message", :keep => 2, :buffer_size => 32767, :min_roll_check => 0) + device = Lumberjack::Device::RollingLogFile.new(log_file, template: ":message", keep: 2, buffer_size: 32767, min_roll_check: 0) allow(device).to receive(:roll_file?).and_return(true) - allow(device).to receive_messages(:archive_file_suffix => "rolled") + allow(device).to receive_messages(archive_file_suffix: "rolled") device end @@ -147,5 +146,4 @@ expect(encoding.name).to eq "ASCII-8BIT" end end - end diff --git a/spec/device/size_rolling_log_file_spec.rb b/spec/device/size_rolling_log_file_spec.rb index 174145f..1fb77a7 100644 --- a/spec/device/size_rolling_log_file_spec.rb +++ b/spec/device/size_rolling_log_file_spec.rb @@ -1,58 +1,56 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Device::SizeRollingLogFile do - before :all do create_tmp_dir end - + after :all do delete_tmp_dir end - + before :each do delete_tmp_files end it "should roll a file when it gets to a specified size" do log_file = File.join(tmp_dir, "a#{rand(1000000000)}.log") - device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => 40, :template => ":message", :min_roll_check => 0) - logger = Lumberjack::Logger.new(device, :buffer_size => 2) + device = Lumberjack::Device::SizeRollingLogFile.new(log_file, max_size: 40, template: ":message", min_roll_check: 0) + logger = Lumberjack::Logger.new(device, buffer_size: 2) 4.times do |i| logger.error("test message #{i + 1}") logger.flush end logger.close - + expect(File.read("#{log_file}.1").split(Lumberjack::LINE_SEPARATOR)).to eq(["test message 1", "test message 2", "test message 3"]) expect(File.read(log_file)).to eq("test message 4#{Lumberjack::LINE_SEPARATOR}") end - + it "should be able to specify the max size in kilobytes" do log_file = File.join(tmp_dir, "b#{rand(1000000000)}.log") - device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => "32K", :min_roll_check => 0) + device = Lumberjack::Device::SizeRollingLogFile.new(log_file, max_size: "32K", min_roll_check: 0) expect(device.max_size).to eq(32768) end - + it "should be able to specify the max size in megabytes" do log_file = File.join(tmp_dir, "c#{rand(1000000000)}.log") - device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => "100M", :min_roll_check => 0) + device = Lumberjack::Device::SizeRollingLogFile.new(log_file, max_size: "100M", min_roll_check: 0) expect(device.max_size).to eq(104_857_600) end - + it "should be able to specify the max size in gigabytes" do log_file = File.join(tmp_dir, "d#{rand(1000000000)}.log") - device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => "1G", :min_roll_check => 0) + device = Lumberjack::Device::SizeRollingLogFile.new(log_file, max_size: "1G", min_roll_check: 0) expect(device.max_size).to eq(1_073_741_824) end - + it "should figure out the next archive file name available" do log_file = File.join(tmp_dir, "filename.log") (3..11).each do |i| - File.open("#{log_file}.#{i}", 'w'){|f| f.write(i.to_s)} + File.open("#{log_file}.#{i}", "w") { |f| f.write(i.to_s) } end - device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => "100M", :min_roll_check => 0) + device = Lumberjack::Device::SizeRollingLogFile.new(log_file, max_size: "100M", min_roll_check: 0) expect(device.archive_file_suffix).to eq("12") end - end diff --git a/spec/device/writer_spec.rb b/spec/device/writer_spec.rb index 6cb6c9f..c7a34e9 100644 --- a/spec/device/writer_spec.rb +++ b/spec/device/writer_spec.rb @@ -1,14 +1,13 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Device::Writer do - - let(:time_string){ "2011-01-15T14:23:45.123" } - let(:time){ Time.parse(time_string) } - let(:stream){ StringIO.new } - let(:entry){ Lumberjack::LogEntry.new(time, Logger::INFO, "test message", "app", 12345, "unit_of_work_id" => "ABCD") } + let(:time_string) { "2011-01-15T14:23:45.123" } + let(:time) { Time.parse(time_string) } + let(:stream) { StringIO.new } + let(:entry) { Lumberjack::LogEntry.new(time, Logger::INFO, "test message", "app", 12345, "unit_of_work_id" => "ABCD") } it "should buffer output and not write directly to the stream" do - device = Lumberjack::Device::Writer.new(stream, :template => ":message", :buffer_size => 32767) + device = Lumberjack::Device::Writer.new(stream, template: ":message", buffer_size: 32767) device.write(entry) expect(stream.string).to eq("") device.flush @@ -16,7 +15,7 @@ end it "should flush the buffer when it gets to the specified size" do - device = Lumberjack::Device::Writer.new(stream, :buffer_size => 15, :template => ":message") + device = Lumberjack::Device::Writer.new(stream, buffer_size: 15, template: ":message") device.write(entry) expect(stream.string).to eq("") device.write(entry) @@ -26,15 +25,38 @@ it "should sync the stream and flush it when the device is flushed" do # Create an IO like object that require flush to be called io = Object.new - def io.init; @string = ""; @buffer = ""; @sync = false; end - def io.write(string); @buffer << string; end - def io.flush; @string << @buffer; @buffer = ""; end - def io.string; @string; end - def io.sync=(val); @sync = val; end - def io.sync; @sync; end + # rubocop:disable Style/TrivialAccessors + def io.init + @string = "" + @buffer = "" + @sync = false + end + + def io.write(string) + @buffer << string + end + + def io.flush + @string << @buffer + @buffer = "" + end + + def io.string + @string + end + + def io.sync=(val) + @sync = val + end + + def io.sync + @sync + end + # rubocop:enable Style/TrivialAccessors + io.init - device = Lumberjack::Device::Writer.new(io, :template => ":message", :buffer_size => 32767) + device = Lumberjack::Device::Writer.new(io, template: ":message", buffer_size: 32767) device.write(entry) expect(io.string).to eq("") device.flush @@ -43,7 +65,7 @@ def io.sync; @sync; end end it "should be able to set the buffer size" do - device = Lumberjack::Device::Writer.new(stream, :buffer_size => 15) + device = Lumberjack::Device::Writer.new(stream, buffer_size: 15) expect(device.buffer_size).to eq(15) device.buffer_size = 100 expect(device.buffer_size).to eq(100) @@ -62,21 +84,21 @@ def io.sync; @sync; end end it "should write entries out to the stream with a custom template" do - device = Lumberjack::Device::Writer.new(stream, :template => ":message") + device = Lumberjack::Device::Writer.new(stream, template: ":message") device.write(entry) device.flush expect(stream.string).to eq("test message#{Lumberjack::LINE_SEPARATOR}") end it "should be able to specify the time format for the template" do - device = Lumberjack::Device::Writer.new(stream, :time_format => :microseconds) + device = Lumberjack::Device::Writer.new(stream, time_format: :microseconds) device.write(entry) device.flush expect(stream.string).to eq("[2011-01-15T14:23:45.123000 INFO app(12345) #ABCD] test message#{Lumberjack::LINE_SEPARATOR}") end it "should be able to specify a block template for log entries" do - device = Lumberjack::Device::Writer.new(stream, :template => lambda{|e| e.message.upcase}) + device = Lumberjack::Device::Writer.new(stream, template: lambda { |e| e.message.upcase }) device.write(entry) device.flush expect(stream.string).to eq("TEST MESSAGE#{Lumberjack::LINE_SEPARATOR}") @@ -86,7 +108,7 @@ def io.sync; @sync; end stderr = $stderr $stderr = StringIO.new begin - device = Lumberjack::Device::Writer.new(stream, :template => ":message") + device = Lumberjack::Device::Writer.new(stream, template: ":message") expect(stream).to receive(:write).and_raise(StandardError.new("Cannot write to stream")) device.write(entry) device.flush @@ -98,8 +120,8 @@ def io.sync; @sync; end end describe "multi line messages" do - let(:message){ "line 1#{Lumberjack::LINE_SEPARATOR}line 2#{Lumberjack::LINE_SEPARATOR}line 3" } - let(:entry){ Lumberjack::LogEntry.new(time, Logger::INFO, message, "app", 12345, "unit_of_work_id" => "ABCD") } + let(:message) { "line 1#{Lumberjack::LINE_SEPARATOR}line 2#{Lumberjack::LINE_SEPARATOR}line 3" } + let(:entry) { Lumberjack::LogEntry.new(time, Logger::INFO, message, "app", 12345, "unit_of_work_id" => "ABCD") } it "should have a default template for multiline messages" do device = Lumberjack::Device::Writer.new(stream) @@ -109,7 +131,7 @@ def io.sync; @sync; end end it "should be able to specify a template for multiple line messages" do - device = Lumberjack::Device::Writer.new(stream, :additional_lines => " // :message") + device = Lumberjack::Device::Writer.new(stream, additional_lines: " // :message") device.write(entry) device.flush expect(stream.string).to eq("[2011-01-15T14:23:45.123 INFO app(12345) #ABCD] line 1 // line 2 // line 3#{Lumberjack::LINE_SEPARATOR}") diff --git a/spec/formatter/date_time_formatter_spec.rb b/spec/formatter/date_time_formatter_spec.rb index 1228aca..0898b4c 100644 --- a/spec/formatter/date_time_formatter_spec.rb +++ b/spec/formatter/date_time_formatter_spec.rb @@ -1,7 +1,6 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter::DateTimeFormatter do - it "should format a time object" do time = Time.now formatter = Lumberjack::Formatter::DateTimeFormatter.new("%Y-%m-%d %H:%M") @@ -30,5 +29,4 @@ formatter = Lumberjack::Formatter::DateTimeFormatter.new expect(formatter.call(time)).to eq time.iso8601 end - end diff --git a/spec/formatter/exception_formatter_spec.rb b/spec/formatter/exception_formatter_spec.rb index b2f06e1..95910ed 100644 --- a/spec/formatter/exception_formatter_spec.rb +++ b/spec/formatter/exception_formatter_spec.rb @@ -1,7 +1,6 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter::ExceptionFormatter do - it "should convert an exception without a backtrace to a string" do e = ArgumentError.new("not expected") formatter = Lumberjack::Formatter::ExceptionFormatter.new @@ -13,7 +12,7 @@ raise ArgumentError.new("not expected") rescue => e formatter = Lumberjack::Formatter::ExceptionFormatter.new - expect(formatter.call(e)).to eq("ArgumentError: not expected#{Lumberjack::LINE_SEPARATOR} #{e.backtrace.join(Lumberjack::LINE_SEPARATOR + ' ')}") + expect(formatter.call(e)).to eq("ArgumentError: not expected#{Lumberjack::LINE_SEPARATOR} #{e.backtrace.join(Lumberjack::LINE_SEPARATOR + " ")}") end end @@ -26,5 +25,4 @@ expect(formatter.call(e)).to eq("ArgumentError: not expected#{Lumberjack::LINE_SEPARATOR} redacted: #{e.backtrace.size}") end end - end diff --git a/spec/formatter/id_formatter_spec.rb b/spec/formatter/id_formatter_spec.rb index 928c5a4..b977b98 100644 --- a/spec/formatter/id_formatter_spec.rb +++ b/spec/formatter/id_formatter_spec.rb @@ -1,12 +1,12 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter::IdFormatter do - it "should format an object as a hash of class and id" do obj = Object.new - def obj.id; 1; end + def obj.id + 1 + end formatter = Lumberjack::Formatter::IdFormatter.new expect(formatter.call(obj)).to eq({"class" => "Object", "id" => 1}) end - end diff --git a/spec/formatter/inspect_formatter_spec.rb b/spec/formatter/inspect_formatter_spec.rb index c3c9c06..c6ea758 100644 --- a/spec/formatter/inspect_formatter_spec.rb +++ b/spec/formatter/inspect_formatter_spec.rb @@ -1,7 +1,6 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter::InspectFormatter do - it "should format objects as string by calling their inspect method" do formatter = Lumberjack::Formatter::InspectFormatter.new expect(formatter.call("abc")).to eq("\"abc\"") @@ -9,5 +8,4 @@ expect(formatter.call(1)).to eq("1") expect(formatter.call([:a, 1, "b"])).to eq([:a, 1, "b"].inspect) end - end diff --git a/spec/formatter/object_formatter_spec.rb b/spec/formatter/object_formatter_spec.rb index 6ee5b81..89744fa 100644 --- a/spec/formatter/object_formatter_spec.rb +++ b/spec/formatter/object_formatter_spec.rb @@ -1,11 +1,9 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter::ObjectFormatter do - it "should return the object itself" do formatter = Lumberjack::Formatter::ObjectFormatter.new obj = Object.new expect(formatter.call(obj).object_id).to eq obj.object_id end - end diff --git a/spec/formatter/pretty_print_formatter_spec.rb b/spec/formatter/pretty_print_formatter_spec.rb index 2f8275b..6176895 100644 --- a/spec/formatter/pretty_print_formatter_spec.rb +++ b/spec/formatter/pretty_print_formatter_spec.rb @@ -1,7 +1,6 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter::PrettyPrintFormatter do - it "should convert an object to a string using pretty print" do object = Object.new def object.pretty_print(q) @@ -10,5 +9,4 @@ def object.pretty_print(q) formatter = Lumberjack::Formatter::PrettyPrintFormatter.new expect(formatter.call(object)).to eq("woot!") end - end diff --git a/spec/formatter/string_formatter_spec.rb b/spec/formatter/string_formatter_spec.rb index 76ada1e..43da251 100644 --- a/spec/formatter/string_formatter_spec.rb +++ b/spec/formatter/string_formatter_spec.rb @@ -1,12 +1,10 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter::StringFormatter do - it "should format objects as string by calling their to_s method" do formatter = Lumberjack::Formatter::StringFormatter.new expect(formatter.call("abc")).to eq("abc") expect(formatter.call(:test)).to eq("test") expect(formatter.call(1)).to eq("1") end - end diff --git a/spec/formatter/strip_formatter_spec.rb b/spec/formatter/strip_formatter_spec.rb index b84bc5d..3b41570 100644 --- a/spec/formatter/strip_formatter_spec.rb +++ b/spec/formatter/strip_formatter_spec.rb @@ -1,12 +1,10 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter::StripFormatter do - it "should format objects as strings with leading and trailing whitespace removed" do formatter = Lumberjack::Formatter::StripFormatter.new expect(formatter.call(" abc \n")).to eq("abc") expect(formatter.call(:test)).to eq("test") expect(formatter.call(1)).to eq("1") end - end diff --git a/spec/formatter/structured_formatter_spec.rb b/spec/formatter/structured_formatter_spec.rb index 61ef98a..6069f92 100644 --- a/spec/formatter/structured_formatter_spec.rb +++ b/spec/formatter/structured_formatter_spec.rb @@ -1,42 +1,40 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter::StructuredFormatter do - it "should recursively format arrays and hashes" do formatter = Lumberjack::Formatter.new.clear formatter.add(Enumerable, Lumberjack::Formatter::StructuredFormatter.new(formatter)) - formatter.add(String) { |obj| "#{obj}?"} + formatter.add(String) { |obj| "#{obj}?" } formatter.add(Object, :object) - formatted = formatter.format({ foo: "bar", baz: [1, 2, "three", { four: "four" }] }) - expect(formatted).to eq({ "foo" => "bar?", "baz" => [1, 2, "three?", { "four" => "four?" }] }) + formatted = formatter.format({foo: "bar", baz: [1, 2, "three", {four: "four"}]}) + expect(formatted).to eq({"foo" => "bar?", "baz" => [1, 2, "three?", {"four" => "four?"}]}) end it "should not get into an infinite loop" do formatter = Lumberjack::Formatter.new.clear formatter.add(Enumerable, Lumberjack::Formatter::StructuredFormatter.new(formatter)) formatter.add(Object, :object) - object = { name: "object", children: [], v1: true, v2: true } + object = {name: "object", children: [], v1: true, v2: true} object[:parent] = object - child_1 = { name: "child_1", parent: object } - child_2 = { name: "child_2", parent: child_1 } + child_1 = {name: "child_1", parent: object} + child_2 = {name: "child_2", parent: child_1} object[:children] << child_1 object[:children] << child_2 formatted = formatter.format(object) - expect(formatted).to eq({ "name" => "object", "children" => [{ "name" => "child_1" }, { "name" => "child_2", "parent" => { "name" => "child_1" } }], "v1" => true, "v2" => true }) + expect(formatted).to eq({"name" => "object", "children" => [{"name" => "child_1"}, {"name" => "child_2", "parent" => {"name" => "child_1"}}], "v1" => true, "v2" => true}) end it "should be able to include an object multiple times if not-recursive" do formatter = Lumberjack::Formatter.new.clear formatter.add(Enumerable, Lumberjack::Formatter::StructuredFormatter.new(formatter)) formatter.add(Object, :object) - object = { name: "object", children: [] } - duplicated = { name: "duplicated" } - child_1 = { name: "child_1", parent: duplicated } - child_2 = { name: "child_2", parent: duplicated } + object = {name: "object", children: []} + duplicated = {name: "duplicated"} + child_1 = {name: "child_1", parent: duplicated} + child_2 = {name: "child_2", parent: duplicated} object[:children] << child_1 object[:children] << child_2 formatted = formatter.format(object) - expect(formatted).to eq({ "name" => "object", "children" => [{ "name" => "child_1", "parent" => { "name" => "duplicated" } }, { "name" => "child_2", "parent" => { "name" => "duplicated" } }] }) + expect(formatted).to eq({"name" => "object", "children" => [{"name" => "child_1", "parent" => {"name" => "duplicated"}}, {"name" => "child_2", "parent" => {"name" => "duplicated"}}]}) end - end diff --git a/spec/formatter_spec.rb b/spec/formatter_spec.rb index 7bc934f..3e040b1 100644 --- a/spec/formatter_spec.rb +++ b/spec/formatter_spec.rb @@ -1,8 +1,7 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Formatter do - - let(:formatter){ Lumberjack::Formatter.new } + let(:formatter) { Lumberjack::Formatter.new } it "should have a default set of formatters" do expect(formatter.format("abc")).to eq("abc") @@ -11,12 +10,12 @@ end it "should be able to add a formatter object for a class" do - formatter.add(Numeric, lambda{|obj| "number: #{obj}"}) + formatter.add(Numeric, lambda { |obj| "number: #{obj}" }) expect(formatter.format(10)).to eq("number: 10") end it "should be able to add a formatter object for a class name" do - formatter.add("Numeric", lambda{|obj| "number: #{obj}"}) + formatter.add("Numeric", lambda { |obj| "number: #{obj}" }) expect(formatter.format(10)).to eq("number: 10") end @@ -27,12 +26,12 @@ end it "should be able to add a formatter object for a module" do - formatter.add(Enumerable, lambda{|obj| "list: #{obj.inspect}"}) + formatter.add(Enumerable, lambda { |obj| "list: #{obj.inspect}" }) expect(formatter.format([1, 2])).to eq("list: [1, 2]") end it "should be able to add a formatter block for a class" do - formatter.add(Numeric){|obj| "number: #{obj}"} + formatter.add(Numeric) { |obj| "number: #{obj}" } expect(formatter.format(10)).to eq("number: 10") end @@ -57,8 +56,8 @@ end it "should format an object based on the class hierarchy" do - formatter.add(Numeric){|obj| "number: #{obj}"} - formatter.add(Integer){|obj| "fixed number: #{obj}"} + formatter.add(Numeric) { |obj| "number: #{obj}" } + formatter.add(Integer) { |obj| "fixed number: #{obj}" } expect(formatter.format(10)).to eq("fixed number: 10") expect(formatter.format(10.1)).to eq("number: 10.1") end diff --git a/spec/log_entry_spec.rb b/spec/log_entry_spec.rb index e1dc62d..ded053d 100644 --- a/spec/log_entry_spec.rb +++ b/spec/log_entry_spec.rb @@ -1,7 +1,6 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::LogEntry do - it "should have a time" do t = Time.now entry = Lumberjack::LogEntry.new(t, Logger::INFO, "test", "app", 1500, "unit_of_work_id" => "ABCD") @@ -64,11 +63,10 @@ entry.unit_of_work_id = "1234" expect(entry.unit_of_work_id).to eq("1234") end - + it "should be converted to a string" do t = Time.parse("2011-01-29T12:15:32.001") entry = Lumberjack::LogEntry.new(t, Logger::INFO, "test", "app", 1500, "unit_of_work_id" => "ABCD") expect(entry.to_s).to eq('[2011-01-29T12:15:32.001 INFO app(1500) unit_of_work_id:"ABCD"] test') end - end diff --git a/spec/logger_spec.rb b/spec/logger_spec.rb index a267e91..1c235d3 100644 --- a/spec/logger_spec.rb +++ b/spec/logger_spec.rb @@ -1,12 +1,11 @@ -require 'spec_helper' -require 'pathname' +require "spec_helper" +require "pathname" describe Lumberjack::Logger do - describe "compatibility" do it "should implement the same public API as the ::Logger class" do - logger = ::Logger.new(STDOUT) - lumberjack = Lumberjack::Logger.new(STDOUT) + logger = ::Logger.new($stdout) + lumberjack = Lumberjack::Logger.new($stdout) (logger.public_methods - Object.public_methods).each do |method_name| logger_method = logger.method(method_name) lumberjack_method = lumberjack.method(method_name) @@ -18,7 +17,6 @@ end describe "initialization" do - before :all do create_tmp_dir end @@ -55,12 +53,12 @@ end it "should set the level with a numeric" do - logger = Lumberjack::Logger.new(:null, :level => Logger::WARN) + logger = Lumberjack::Logger.new(:null, level: Logger::WARN) expect(logger.level).to eq(Logger::WARN) end it "should set the level with a level" do - logger = Lumberjack::Logger.new(:null, :level => :warn) + logger = Lumberjack::Logger.new(:null, level: :warn) expect(logger.level).to eq(Logger::WARN) end @@ -69,14 +67,14 @@ expect(logger.level).to eq(Logger::INFO) end - it "should set the progname"do - logger = Lumberjack::Logger.new(:null, :progname => "app") + it "should set the progname" do + logger = Lumberjack::Logger.new(:null, progname: "app") expect(logger.progname).to eq("app") end it "should create a thread to flush the device" do expect(Thread).to receive(:new) - logger = Lumberjack::Logger.new(:null, :flush_seconds => 10) + logger = Lumberjack::Logger.new(:null, flush_seconds: 10) end end @@ -95,7 +93,7 @@ it "should be able to silence the log in a block" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Logger::INFO, :template => ":message") + logger = Lumberjack::Logger.new(output, buffer_size: 0, level: Logger::INFO, template: ":message") logger.info("one") logger.silence do expect(logger.level).to eq(Logger::ERROR) @@ -108,7 +106,7 @@ it "should be able to customize the level of silence in a block" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Logger::INFO, :template => ":message") + logger = Lumberjack::Logger.new(output, buffer_size: 0, level: Logger::INFO, template: ":message") logger.info("one") logger.silence(Logger::FATAL) do expect(logger.level).to eq(Logger::FATAL) @@ -122,7 +120,7 @@ it "should be able to customize the level of silence in a block with a symbol" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Logger::INFO, :template => ":message") + logger = Lumberjack::Logger.new(output, buffer_size: 0, level: Logger::INFO, template: ":message") logger.info("one") logger.silence(:fatal) do expect(logger.level).to eq(Logger::FATAL) @@ -136,7 +134,7 @@ it "should not be able to silence the logger if silencing is disabled" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Logger::INFO, :template => ":message") + logger = Lumberjack::Logger.new(output, buffer_size: 0, level: Logger::INFO, template: ":message") logger.silencer = false logger.info("one") logger.silence do @@ -163,7 +161,7 @@ it "should only affect the current thread when silencing the logger" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Logger::INFO, :template => ":message") + logger = Lumberjack::Logger.new(output, buffer_size: 0, level: Logger::INFO, template: ":message") # status is used to make sure the two threads are executing at the same time status = 0 begin @@ -171,10 +169,16 @@ logger.silence do logger.info("inner") status = 1 - loop{ sleep(0.001); break if status == 2} + loop do + sleep(0.001) + break if status == 2 + end end end - loop{ sleep(0.001); break if status == 1} + loop do + sleep(0.001) + break if status == 1 + end logger.info("outer") status = 2 logger.close @@ -187,7 +191,7 @@ it "should only affect the current thread when changing the progname in a block" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :progname => "thread1", :buffer_size => 0, :level => Logger::INFO, :template => ":progname :message") + logger = Lumberjack::Logger.new(output, progname: "thread1", buffer_size: 0, level: Logger::INFO, template: ":progname :message") # status is used to make sure the two threads are executing at the same time status = 0 begin @@ -195,10 +199,16 @@ logger.set_progname("thread2") do logger.info("inner") status = 1 - loop{ sleep(0.001); break if status == 2} + loop do + sleep(0.001) + break if status == 2 + end end end - loop{ sleep(0.001); break if status == 1} + loop do + sleep(0.001) + break if status == 1 + end logger.info("outer") status = 2 logger.close @@ -213,7 +223,7 @@ describe "datetime_format" do it "should be able to set the datetime format for timestamps on the log device" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :template => ":time :message", datetime_format: "%Y-%m-%d") + logger = Lumberjack::Logger.new(output, template: ":time :message", datetime_format: "%Y-%m-%d") expect(logger.datetime_format).to eq "%Y-%m-%d" Timecop.freeze do logger.info("one") @@ -221,7 +231,7 @@ expect(logger.datetime_format).to eq "%m-%d-%Y" logger.info("two") logger.flush - expect(output.string).to eq "#{Time.now.strftime('%Y-%m-%d')} one\n#{Time.now.strftime('%m-%d-%Y')} two\n" + expect(output.string).to eq "#{Time.now.strftime("%Y-%m-%d")} one\n#{Time.now.strftime("%m-%d-%Y")} two\n" end end end @@ -229,7 +239,7 @@ describe "flushing" do it "should autoflush the buffer if it hasn't been flushed in a specified number of seconds" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :flush_seconds => 0.1, :level => Logger::INFO, :template => ":message", :buffer_size => 32767) + logger = Lumberjack::Logger.new(output, flush_seconds: 0.1, level: Logger::INFO, template: ":message", buffer_size: 32767) logger.info("message 1") logger.info("message 2") expect(output.string).to eq("") @@ -243,7 +253,7 @@ it "should write the log entries to the device on flush and update the last flushed time" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :level => Logger::INFO, :template => ":message", :buffer_size => 32767) + logger = Lumberjack::Logger.new(output, level: Logger::INFO, template: ":message", buffer_size: 32767) logger.info("message 1") expect(output.string).to eq("") last_flushed_at = logger.last_flushed_at @@ -254,7 +264,7 @@ it "should flush the buffer and close the devices" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :level => Logger::INFO, :template => ":message", :buffer_size => 32767) + logger = Lumberjack::Logger.new(output, level: Logger::INFO, template: ":message", buffer_size: 32767) logger.info("message 1") expect(output.string).to eq("") expect(logger.closed?).to eq false @@ -266,7 +276,7 @@ it "should reopen the devices" do output = StringIO.new - logger = Lumberjack::Logger.new(output, :level => Logger::INFO, :template => ":message", :buffer_size => 32767) + logger = Lumberjack::Logger.new(output, level: Logger::INFO, template: ":message", buffer_size: 32767) logger.close expect(logger.device).to receive(:reopen).and_call_original logger.reopen @@ -275,36 +285,36 @@ end describe "logging" do - let(:output){ StringIO.new } - let(:device){ Lumberjack::Device::Writer.new(output, :buffer_size => 0, template: "[:time :severity :progname(:pid)] :message :tags") } - let(:logger){ Lumberjack::Logger.new(device, :level => Logger::INFO, :progname => "app") } - let(:n){ Lumberjack::LINE_SEPARATOR } + let(:output) { StringIO.new } + let(:device) { Lumberjack::Device::Writer.new(output, buffer_size: 0, template: "[:time :severity :progname(:pid)] :message :tags") } + let(:logger) { Lumberjack::Logger.new(device, level: Logger::INFO, progname: "app") } + let(:n) { Lumberjack::LINE_SEPARATOR } describe "add" do it "should add entries with a numeric severity and a message" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.add(Logger::INFO, "test") expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 INFO app(#{$$})] test") end it "should add entries with a severity label" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.add(:info, "test") expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 INFO app(#{$$})] test") end it "should add entries with a custom progname and message" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.add(Logger::INFO, "test", "spec") expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 INFO spec(#{$$})] test") end it "should add entries with a local progname and message" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.set_progname("block") do logger.add(Logger::INFO, "test") end @@ -313,7 +323,7 @@ it "should add entries with a progname but no message or block" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.set_progname("default") do logger.add(Logger::INFO, nil, "message") end @@ -322,21 +332,21 @@ it "should add entries with a block" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.add(Logger::INFO) { "test" } expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 INFO app(#{$$})] test") end it "should log entries (::Logger compatibility)" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.log(Logger::INFO, "test") expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 INFO app(#{$$})] test") end it "should append messages with unknown severity to the log" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger << "test" expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 UNKNOWN app(#{$$})] test") end @@ -345,14 +355,14 @@ describe "add_entry" do it "should add entries with tags" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.add_entry(Logger::INFO, "test", "spec", "tag" => "ABCD") expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 INFO spec(#{$$})] test [tag:ABCD]") end it "should handle malformed tags" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.add_entry(Logger::INFO, "test", "spec", "ABCD") expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 INFO spec(#{$$})] test") end @@ -362,7 +372,7 @@ $stderr = StringIO.new begin time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) expect(device).to receive(:write).and_raise(StandardError.new("Cannot write to device")) logger.add_entry(Logger::INFO, "test") expect($stderr.string).to include("[2011-01-30T12:31:56.123 INFO app(#{$$})] test") @@ -374,15 +384,19 @@ it "should call Proc tag values" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) + allow(Time).to receive_messages(now: time) logger.add_entry(Logger::INFO, "test", "spec", tag: lambda { "foo" }) expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 INFO spec(#{$$})] test [tag:foo]") end it "should not get into infinite loops by logging entries while logging entries" do time = Time.parse("2011-01-30T12:31:56.123") - allow(Time).to receive_messages(:now => time) - logger.add_entry(Logger::INFO, "test", "spec", tag: lambda { logger.warn("inner logging"); "foo" }) + allow(Time).to receive_messages(now: time) + tag_proc = lambda do + logger.warn("inner logging") + "foo" + end + logger.add_entry(Logger::INFO, "test", "spec", tag: tag_proc) expect(output.string.chomp).to eq("[2011-01-30T12:31:56.123 INFO spec(#{$$})] test [tag:foo]") end end @@ -402,7 +416,7 @@ end end - %w(fatal error warn info debug).each do |level| + %w[fatal error warn info debug].each do |level| describe level do around :each do |example| Timecop.freeze(time) do @@ -460,7 +474,7 @@ end describe "tags" do - let(:device){ Lumberjack::Device::Writer.new(output, :buffer_size => 0, template: ":message - :count - :tags") } + let(:device) { Lumberjack::Device::Writer.new(output, buffer_size: 0, template: ":message - :count - :tags") } it "should be able to add tags to the logs" do logger.level = :debug @@ -470,11 +484,11 @@ logger.error("error", count: 4, tag: "d") logger.fatal("fatal", count: 5, tag: "e", foo: "bar") lines = output.string.split(n) - expect(lines[0]).to eq 'debug - 1 - [tag:a]' - expect(lines[1]).to eq 'info - 2 - [tag:b]' - expect(lines[2]).to eq 'warn - 3 - [tag:c]' - expect(lines[3]).to eq 'error - 4 - [tag:d]' - expect(lines[4]).to eq 'fatal - 5 - [tag:e] [foo:bar]' + expect(lines[0]).to eq "debug - 1 - [tag:a]" + expect(lines[1]).to eq "info - 2 - [tag:b]" + expect(lines[2]).to eq "warn - 3 - [tag:c]" + expect(lines[3]).to eq "error - 4 - [tag:d]" + expect(lines[4]).to eq "fatal - 5 - [tag:e] [foo:bar]" end it "should merge logger and context tags" do @@ -487,9 +501,9 @@ end end lines = output.string.split(n) - expect(lines[0]).to eq 'one - 1 - [foo:bar] [baz:boo] [tag:b]' - expect(lines[1]).to eq 'two - 2 - [foo:other] [baz:boo] [tag:c]' - expect(lines[2]).to eq 'three - 3 - [foo:bar] [baz:thing] [tag:d]' + expect(lines[0]).to eq "one - 1 - [foo:bar] [baz:boo] [tag:b]" + expect(lines[1]).to eq "two - 2 - [foo:other] [baz:boo] [tag:c]" + expect(lines[2]).to eq "three - 3 - [foo:bar] [baz:thing] [tag:d]" end it "should add and remove tags only in a tag block" do @@ -501,9 +515,9 @@ logger.info("three") lines = output.string.split(n) - expect(lines[0]).to eq 'one - 1 - [baz:boo]' - expect(lines[1]).to eq 'two - 2 - [baz:boo] [foo:bar]' - expect(lines[2]).to eq 'three - -' + expect(lines[0]).to eq "one - 1 - [baz:boo]" + expect(lines[1]).to eq "two - 2 - [baz:boo] [foo:bar]" + expect(lines[2]).to eq "three - -" end it "should add and remove tags in the global scope if there is no block" do @@ -513,8 +527,8 @@ logger.info("two") lines = output.string.split(n) - expect(lines[0]).to eq 'one - 1 - [foo:bar]' - expect(lines[1]).to eq 'two - 1 -' + expect(lines[0]).to eq "one - 1 - [foo:bar]" + expect(lines[1]).to eq "two - 1 -" end it "should apply a tag formatter to the tags" do @@ -587,7 +601,7 @@ end describe "log helper methods" do - let(:device){ Lumberjack::Device::Writer.new(output, :buffer_size => 0, :template => ":message") } + let(:device) { Lumberjack::Device::Writer.new(output, buffer_size: 0, template: ":message") } it "should only add messages whose severity is greater or equal to the logger level" do logger.add_entry(Logger::DEBUG, "debug") @@ -693,5 +707,4 @@ end end end - end diff --git a/spec/lumberjack_spec.rb b/spec/lumberjack_spec.rb index 568babe..cdc9cc2 100644 --- a/spec/lumberjack_spec.rb +++ b/spec/lumberjack_spec.rb @@ -1,7 +1,6 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack do - describe "context" do it "should create a context with tags for a block" do Lumberjack.context do @@ -85,5 +84,4 @@ expect(Lumberjack.unit_of_work_id).to eq(nil) end end - end diff --git a/spec/rack/context_spec.rb b/spec/rack/context_spec.rb index ab5f2b6..a0f1bae 100644 --- a/spec/rack/context_spec.rb +++ b/spec/rack/context_spec.rb @@ -1,9 +1,8 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Rack::Context do - it "should create a context for a request" do - app = lambda{|env| [200, {"Content-Type" => env["Content-Type"], "Context-ID" => "#{Lumberjack.context.object_id} #{Lumberjack.context.object_id}"}, ["OK"]]} + app = lambda { |env| [200, {"Content-Type" => env["Content-Type"], "Context-ID" => "#{Lumberjack.context.object_id} #{Lumberjack.context.object_id}"}, ["OK"]] } handler = Lumberjack::Rack::Context.new(app) response = handler.call("Content-Type" => "text/plain", "action_dispatch.request_id" => "0123-4567-89ab-cdef") @@ -13,5 +12,4 @@ context_ids = response[1]["Context-ID"].split expect(context_ids[0]).to eq context_ids[1] end - end diff --git a/spec/rack/request_id_spec.rb b/spec/rack/request_id_spec.rb index 7ace1e2..13306db 100644 --- a/spec/rack/request_id_spec.rb +++ b/spec/rack/request_id_spec.rb @@ -1,48 +1,46 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Rack::RequestId do - it "should use the action dispatch request id if it exists" do - app = lambda{|env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]]} + app = lambda { |env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]] } handler = Lumberjack::Rack::RequestId.new(app) - + response = handler.call("Content-Type" => "text/plain", "action_dispatch.request_id" => "0123-4567-89ab-cdef") expect(response[0]).to eq(200) expect(response[1]["Content-Type"]).to eq("text/plain") expect(response[1]["Unit-Of-Work"]).to eq("0123-4567-89ab-cdef") expect(response[2]).to eq(["OK"]) end - + it "should use an abbreviated action dispatch request id if abbreviated is true" do - app = lambda{|env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]]} + app = lambda { |env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]] } handler = Lumberjack::Rack::RequestId.new(app, true) - + response = handler.call("Content-Type" => "text/plain", "action_dispatch.request_id" => "0123-4567-89ab-cdef") expect(response[0]).to eq(200) expect(response[1]["Content-Type"]).to eq("text/plain") expect(response[1]["Unit-Of-Work"]).to eq("0123") expect(response[2]).to eq(["OK"]) end - + it "should create a unit of work in a middleware stack if the request id doesn't exist" do - app = lambda{|env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]]} + app = lambda { |env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]] } handler = Lumberjack::Rack::RequestId.new(app) - + response = handler.call("Content-Type" => "text/plain") expect(response[0]).to eq(200) expect(response[1]["Content-Type"]).to eq("text/plain") unit_of_work_1 = response[1]["Unit-Of-Work"] expect(response[2]).to eq(["OK"]) - + response = handler.call("Content-Type" => "text/html") expect(response[0]).to eq(200) expect(response[1]["Content-Type"]).to eq("text/html") unit_of_work_2 = response[1]["Unit-Of-Work"] expect(response[2]).to eq(["OK"]) - + expect(unit_of_work_1).not_to eq(nil) expect(unit_of_work_2).not_to eq(nil) expect(unit_of_work_1).not_to eq(unit_of_work_2) end - end diff --git a/spec/rack/unit_of_work_spec.rb b/spec/rack/unit_of_work_spec.rb index 10bbfc9..1925b7a 100644 --- a/spec/rack/unit_of_work_spec.rb +++ b/spec/rack/unit_of_work_spec.rb @@ -1,26 +1,24 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Rack::UnitOfWork do - it "should create a unit of work in a middleware stack" do - app = lambda{|env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]]} + app = lambda { |env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]] } handler = Lumberjack::Rack::UnitOfWork.new(app) - + response = handler.call("Content-Type" => "text/plain") expect(response[0]).to eq(200) expect(response[1]["Content-Type"]).to eq("text/plain") unit_of_work_1 = response[1]["Unit-Of-Work"] expect(response[2]).to eq(["OK"]) - + response = handler.call("Content-Type" => "text/html") expect(response[0]).to eq(200) expect(response[1]["Content-Type"]).to eq("text/html") unit_of_work_2 = response[1]["Unit-Of-Work"] expect(response[2]).to eq(["OK"]) - + expect(unit_of_work_1).not_to eq(nil) expect(unit_of_work_2).not_to eq(nil) expect(unit_of_work_1).not_to eq(unit_of_work_2) end - end diff --git a/spec/severity_spec.rb b/spec/severity_spec.rb index ccb1257..d749ed3 100644 --- a/spec/severity_spec.rb +++ b/spec/severity_spec.rb @@ -1,7 +1,6 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Severity do - it "should convert a level to a label" do expect(Lumberjack::Severity.level_to_label(Logger::DEBUG)).to eq("DEBUG") expect(Lumberjack::Severity.level_to_label(Logger::INFO)).to eq("INFO") @@ -19,5 +18,4 @@ expect(Lumberjack::Severity.label_to_level("FATAL")).to eq(Logger::FATAL) expect(Lumberjack::Severity.label_to_level("???")).to eq(Logger::UNKNOWN) end - end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6be9393..340d65b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,13 +1,13 @@ # ActiveSupport is only available on some Appraisal runs. begin - require 'active_support/all' + require "active_support/all" rescue LoadError => e end require File.expand_path("../../lib/lumberjack.rb", __FILE__) -require 'stringio' -require 'fileutils' -require 'timecop' +require "stringio" +require "fileutils" +require "timecop" RSpec.configure do |config| config.expect_with :rspec do |c| diff --git a/spec/tag_formatter_spec.rb b/spec/tag_formatter_spec.rb index dcc3343..72142b6 100644 --- a/spec/tag_formatter_spec.rb +++ b/spec/tag_formatter_spec.rb @@ -1,7 +1,6 @@ require "spec_helper" describe Lumberjack::TagFormatter do - let(:tags) { {"foo" => "bar", "baz" => "boo", "count" => 1} } it "should do nothing by default" do @@ -15,12 +14,12 @@ expect(tag_formatter.format(tags)).to eq({"foo" => '"bar"', "baz" => '"boo"', "count" => 1}) end - it "should be able to add tag name specific formatters" do + it "should be able to add tag name specific formatters" do formatter = Lumberjack::Formatter.new.clear.add(String, :inspect) tag_formatter = Lumberjack::TagFormatter.new.add(:foo, formatter) expect(tag_formatter.format(tags)).to eq({"foo" => '"bar"', "baz" => "boo", "count" => 1}) - tag_formatter.remove(:foo).add(["baz", "count"]) { |val| "#{val}!"} + tag_formatter.remove(:foo).add(["baz", "count"]) { |val| "#{val}!" } expect(tag_formatter.format(tags)).to eq({"foo" => "bar", "baz" => "boo!", "count" => "1!"}) tag_formatter.remove(:foo).add("foo", :inspect) @@ -38,5 +37,4 @@ tag_formatter = Lumberjack::TagFormatter.new.add(:other, &:reverse) expect(tag_formatter.format(tags).object_id).to eq tags.object_id end - end diff --git a/spec/tagged_logger_support_spec.rb b/spec/tagged_logger_support_spec.rb index b9d0765..bd6b8d3 100644 --- a/spec/tagged_logger_support_spec.rb +++ b/spec/tagged_logger_support_spec.rb @@ -1,9 +1,9 @@ require "spec_helper" describe Lumberjack::TaggedLoggerSupport do - let(:output){ StringIO.new } - let(:device){ Lumberjack::Device::Writer.new(output, :buffer_size => 0, template: ":message - :count - :tags") } - let(:logger){ Lumberjack::Logger.new(device).tagged_logger! } + let(:output) { StringIO.new } + let(:device) { Lumberjack::Device::Writer.new(output, buffer_size: 0, template: ":message - :count - :tags") } + let(:logger) { Lumberjack::Logger.new(device).tagged_logger! } describe "logger" do describe "tagged" do @@ -20,7 +20,7 @@ logger.info("message") end line = output.string.chomp - expect(line).to eq 'message - - [tagged:[15]]' + expect(line).to eq "message - - [tagged:[15]]" end it "should be nestable" do diff --git a/spec/tagged_logging_spec.rb b/spec/tagged_logging_spec.rb index ef522d8..319cad6 100644 --- a/spec/tagged_logging_spec.rb +++ b/spec/tagged_logging_spec.rb @@ -27,7 +27,7 @@ logger = ::Logger.new(output) tagged_logger = ActiveSupport::TaggedLogging.new(logger) tagged_logger.tagged("foo", "bar") { tagged_logger.info("test") } - expect(output.string.chomp).to eq '[foo] [bar] test' + expect(output.string.chomp).to eq "[foo] [bar] test" end end -end \ No newline at end of file +end diff --git a/spec/tags_spec.rb b/spec/tags_spec.rb index 5db2681..0fbedae 100644 --- a/spec/tags_spec.rb +++ b/spec/tags_spec.rb @@ -1,7 +1,6 @@ require "spec_helper" describe Lumberjack::Tags do - describe "stringify_keys" do it "transforms hash keys to strings" do hash = {foo: 1, bar: 2} @@ -39,5 +38,4 @@ expect(Lumberjack::Tags.expand_runtime_values(hash)).to eq({"foo" => 1, "bar" => "stuff", "baz" => p2}) end end - end diff --git a/spec/template_spec.rb b/spec/template_spec.rb index 212e791..e30c26c 100644 --- a/spec/template_spec.rb +++ b/spec/template_spec.rb @@ -1,10 +1,9 @@ -require 'spec_helper' +require "spec_helper" describe Lumberjack::Template do - - let(:time_string){ "2011-01-15T14:23:45.123" } - let(:time){ Time.parse(time_string) } - let(:entry){ Lumberjack::LogEntry.new(time, Logger::INFO, "line 1#{Lumberjack::LINE_SEPARATOR}line 2#{Lumberjack::LINE_SEPARATOR}line 3", "app", 12345, "unit_of_work_id" => "ABCD", "foo" => "bar") } + let(:time_string) { "2011-01-15T14:23:45.123" } + let(:time) { Time.parse(time_string) } + let(:entry) { Lumberjack::LogEntry.new(time, Logger::INFO, "line 1#{Lumberjack::LINE_SEPARATOR}line 2#{Lumberjack::LINE_SEPARATOR}line 3", "app", 12345, "unit_of_work_id" => "ABCD", "foo" => "bar") } describe "format" do it "should format a log entry with a template string" do @@ -13,24 +12,24 @@ end it "should be able to specify a template for additional lines in a message" do - template = Lumberjack::Template.new(":message (:time)", :additional_lines => " // :message") + template = Lumberjack::Template.new(":message (:time)", additional_lines: " // :message") expect(template.call(entry)).to eq("line 1 (2011-01-15T14:23:45.123) // line 2 // line 3") end end describe "timestamp format" do it "should be able to specify the time format for log entries as microseconds" do - template = Lumberjack::Template.new(":message (:time)", :time_format => :microseconds) + template = Lumberjack::Template.new(":message (:time)", time_format: :microseconds) expect(template.call(entry)).to eq("line 1 (2011-01-15T14:23:45.123000)#{Lumberjack::LINE_SEPARATOR}line 2#{Lumberjack::LINE_SEPARATOR}line 3") end it "should be able to specify the time format for log entries as milliseconds" do - template = Lumberjack::Template.new(":message (:time)", :time_format => :milliseconds) + template = Lumberjack::Template.new(":message (:time)", time_format: :milliseconds) expect(template.call(entry)).to eq("line 1 (2011-01-15T14:23:45.123)#{Lumberjack::LINE_SEPARATOR}line 2#{Lumberjack::LINE_SEPARATOR}line 3") end it "should be able to specify the time format for log entries with a custom format" do - template = Lumberjack::Template.new(":message (:time)", :time_format => "%m/%d/%Y, %I:%M:%S %p") + template = Lumberjack::Template.new(":message (:time)", time_format: "%m/%d/%Y, %I:%M:%S %p") expect(template.call(entry)).to eq("line 1 (01/15/2011, 02:23:45 PM)#{Lumberjack::LINE_SEPARATOR}line 2#{Lumberjack::LINE_SEPARATOR}line 3") end end @@ -39,25 +38,25 @@ it "should format named tags in the template and not in the :tags placement" do template = Lumberjack::Template.new(":message - :foo - :tags") entry = Lumberjack::LogEntry.new(time, Logger::INFO, "here", "app", 12345, "foo" => "bar", "tag" => "a") - expect(template.call(entry)).to eq('here - bar - [tag:a]') + expect(template.call(entry)).to eq("here - bar - [tag:a]") end it "should put nothing in place of missing named tags" do template = Lumberjack::Template.new(":message - :foo - :tags") entry = Lumberjack::LogEntry.new(time, Logger::INFO, "here", "app", 12345, "tag" => "a") - expect(template.call(entry)).to eq('here - - [tag:a]') + expect(template.call(entry)).to eq("here - - [tag:a]") end it "should remove line separators in tags" do template = Lumberjack::Template.new(":message - :foo - :tags") entry = Lumberjack::LogEntry.new(time, Logger::INFO, "here", "app", 12345, "tag" => "a#{Lumberjack::LINE_SEPARATOR}b") - expect(template.call(entry)).to eq('here - - [tag:a b]') + expect(template.call(entry)).to eq("here - - [tag:a b]") end it "should handle tags with special characters by surrounding with brackets" do template = Lumberjack::Template.new(":message - :{foo.bar} - :{@baz!} - :tags") entry = Lumberjack::LogEntry.new(time, Logger::INFO, "here", "app", 12345, "foo.bar" => "test", "@baz!" => 1, "tag" => "a") - expect(template.call(entry)).to eq('here - test - 1 - [tag:a]') + expect(template.call(entry)).to eq("here - test - 1 - [tag:a]") end end end From f6865478aafe1803f536db5d6aa020fe1b57745b Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 21:45:26 -0700 Subject: [PATCH 03/12] github actions --- .github/dependabot.yml | 12 ++++ .github/workflows/continuous_integration.yml | 59 ++++++++++++++++++++ .travis.yml | 40 ------------- 3 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/continuous_integration.yml delete mode 100644 .travis.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..f626269 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +# Dependabot update strategy +version: 2 +updates: + - package-ecosystem: bundler + directory: "/" + schedule: + interval: daily + allow: + # Automatically keep all runtime dependencies updated + - dependency-name: "*" + dependency-type: "production" + versioning-strategy: lockfile-only diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml new file mode 100644 index 0000000..8bcc346 --- /dev/null +++ b/.github/workflows/continuous_integration.yml @@ -0,0 +1,59 @@ +name: Continuous Integration +on: + push: + branches: + - master + - actions-* + tags: + - v* + pull_request: +env: + BUNDLE_CLEAN: "true" + BUNDLE_PATH: vendor/bundle + BUNDLE_JOBS: 3 + BUNDLE_RETRY: 3 +jobs: + specs: + name: ${{ matrix.job }} ruby-${{ matrix.ruby }} ${{ matrix.appraisal }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + ruby: [ 2.3, 2.4, 2.5, 2.6, 2.7, "jruby" ] + job: [ rspec ] + include: + - ruby: 2.3 + appraisal: activesupport_4 + job: rspec + - ruby: 2.5 + appraisal: activesupport_5 + job: rspec + - ruby: 2.6 + appraisal: activesupport + job: rspec + - ruby: 2.6 + appraisal: logger + job: rspec + - ruby: 2.7 + job: standardrb + steps: + - name: checkout + uses: actions/checkout@v2 + - name: set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + - name: inject appraisal ${{ matrix.apprasial }} + if: matrix.apprasial + run: | # inject a specific appraisal Gemfile + bundle update + bundle exec appraisal generate + bundle config set gemfile "gemfiles/${{ matrix.apprasial }}.gemfile" + - name: install dependencies + run: bundle install + - name: specs + if: matrix.job == 'rspec' + run: bundle exec rake spec + - name: standardrb + if: matrix.job == 'standardrb' + run: bundle exec rake standard diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e30a97e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,40 +0,0 @@ -language: ruby - -rvm: - - "2.7" - -gemfile: - - Gemfile - -matrix: - include: - - rvm: "2.7" - gemfile: Gemfile - - - rvm: "2.6" - gemfile: gemfiles/logger.gemfile - - - rvm: "2.6" - gemfile: gemfiles/activesupport.gemfile - - - rvm: "2.5" - gemfile: Gemfile - - - rvm: "2.5" - gemfile: gemfiles/activesupport_5.gemfile - - - rvm: "2.4" - gemfile: Gemfile - - - rvm: "2.3" - gemfile: Gemfile - - - rvm: "2.3" - gemfile: gemfiles/activesupport_4.gemfile - - - rvm: "jruby" - gemfile: Gemfile - -script: - - gem list bundler - - bundle exec rake spec From c4499ecaee835ea9249ebe330f73d9bcc74f3a95 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 22:10:20 -0700 Subject: [PATCH 04/12] fix appraisal --- .github/workflows/continuous_integration.yml | 7 +++++-- Gemfile | 4 +++- gemfiles/activesupport.gemfile | 19 ++++++++++++++++++- gemfiles/activesupport_4.gemfile | 19 ++++++++++++++++++- gemfiles/activesupport_5.gemfile | 19 ++++++++++++++++++- gemfiles/logger.gemfile | 19 ++++++++++++++++++- 6 files changed, 80 insertions(+), 7 deletions(-) diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index 8bcc346..f86e64e 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -12,6 +12,7 @@ env: BUNDLE_PATH: vendor/bundle BUNDLE_JOBS: 3 BUNDLE_RETRY: 3 + BUNDLE_WITHOUT: standardrb yarn jobs: specs: name: ${{ matrix.job }} ruby-${{ matrix.ruby }} ${{ matrix.appraisal }} @@ -36,6 +37,8 @@ jobs: job: rspec - ruby: 2.7 job: standardrb + env: + BUNDLE_WITHOUT: "yarn" steps: - name: checkout uses: actions/checkout@v2 @@ -50,10 +53,10 @@ jobs: bundle exec appraisal generate bundle config set gemfile "gemfiles/${{ matrix.apprasial }}.gemfile" - name: install dependencies - run: bundle install + run: bundle install {{ matrix.bundle_without }} - name: specs if: matrix.job == 'rspec' run: bundle exec rake spec - name: standardrb if: matrix.job == 'standardrb' - run: bundle exec rake standard + run: bundle exec standardrb diff --git a/Gemfile b/Gemfile index 20a9020..f4ca874 100644 --- a/Gemfile +++ b/Gemfile @@ -9,9 +9,11 @@ group :development, :test do gem "rspec", "~> 3.9" gem "timecop" gem "appraisal" +end +group :standardrb do # Lock standard to a particular version, esp. cause it's still 0.x.x according to Semver - gem "standard", "0.5.2" + gem "standard", "0.5.2", require: false end group :doc do diff --git a/gemfiles/activesupport.gemfile b/gemfiles/activesupport.gemfile index d4bc70f..8c394e0 100644 --- a/gemfiles/activesupport.gemfile +++ b/gemfiles/activesupport.gemfile @@ -4,4 +4,21 @@ source "https://rubygems.org" gem "activesupport", require: "activesupport/all" -gemspec path: "../" +group :runtime do + gemspec path: "../" +end + +group :development, :test do + gem "rake" + gem "rspec", "~> 3.9" + gem "timecop" + gem "appraisal" +end + +group :standardrb do + gem "standard", "0.5.2", require: false +end + +group :doc do + gem "yard" +end diff --git a/gemfiles/activesupport_4.gemfile b/gemfiles/activesupport_4.gemfile index 526314c..717ec19 100644 --- a/gemfiles/activesupport_4.gemfile +++ b/gemfiles/activesupport_4.gemfile @@ -4,4 +4,21 @@ source "https://rubygems.org" gem "activesupport", "~> 4.0", require: "activesupport/all" -gemspec path: "../" +group :runtime do + gemspec path: "../" +end + +group :development, :test do + gem "rake" + gem "rspec", "~> 3.9" + gem "timecop" + gem "appraisal" +end + +group :standardrb do + gem "standard", "0.5.2", require: false +end + +group :doc do + gem "yard" +end diff --git a/gemfiles/activesupport_5.gemfile b/gemfiles/activesupport_5.gemfile index 4a6b2e6..377c60e 100644 --- a/gemfiles/activesupport_5.gemfile +++ b/gemfiles/activesupport_5.gemfile @@ -4,4 +4,21 @@ source "https://rubygems.org" gem "activesupport", "~> 5.0", require: "activesupport/all" -gemspec path: "../" +group :runtime do + gemspec path: "../" +end + +group :development, :test do + gem "rake" + gem "rspec", "~> 3.9" + gem "timecop" + gem "appraisal" +end + +group :standardrb do + gem "standard", "0.5.2", require: false +end + +group :doc do + gem "yard" +end diff --git a/gemfiles/logger.gemfile b/gemfiles/logger.gemfile index e6ef878..42b724c 100644 --- a/gemfiles/logger.gemfile +++ b/gemfiles/logger.gemfile @@ -4,4 +4,21 @@ source "https://rubygems.org" gem "logger" -gemspec path: "../" +group :runtime do + gemspec path: "../" +end + +group :development, :test do + gem "rake" + gem "rspec", "~> 3.9" + gem "timecop" + gem "appraisal" +end + +group :standardrb do + gem "standard", "0.5.2", require: false +end + +group :doc do + gem "yard" +end From a7d9aed42bb5c508f29c6845491efa0e140fa137 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 22:10:45 -0700 Subject: [PATCH 05/12] fix appraisal --- .github/workflows/continuous_integration.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index f86e64e..89bc475 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -53,7 +53,7 @@ jobs: bundle exec appraisal generate bundle config set gemfile "gemfiles/${{ matrix.apprasial }}.gemfile" - name: install dependencies - run: bundle install {{ matrix.bundle_without }} + run: bundle install - name: specs if: matrix.job == 'rspec' run: bundle exec rake spec From d1f46af4c0c615f7fbabf10d19729794c08a647c Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 22:19:48 -0700 Subject: [PATCH 06/12] fix appraisal --- .github/workflows/continuous_integration.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index 89bc475..00cf842 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -12,7 +12,7 @@ env: BUNDLE_PATH: vendor/bundle BUNDLE_JOBS: 3 BUNDLE_RETRY: 3 - BUNDLE_WITHOUT: standardrb yarn + BUNDLE_WITHOUT: standard yard jobs: specs: name: ${{ matrix.job }} ruby-${{ matrix.ruby }} ${{ matrix.appraisal }} @@ -38,7 +38,7 @@ jobs: - ruby: 2.7 job: standardrb env: - BUNDLE_WITHOUT: "yarn" + BUNDLE_WITHOUT: "yard" steps: - name: checkout uses: actions/checkout@v2 @@ -47,7 +47,7 @@ jobs: with: ruby-version: ${{ matrix.ruby }} - name: inject appraisal ${{ matrix.apprasial }} - if: matrix.apprasial + if: matrix.appraisal && matrix.appraisal != '' run: | # inject a specific appraisal Gemfile bundle update bundle exec appraisal generate From 90871653a02df945ab89326a55d020bf8c341071 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 22:25:38 -0700 Subject: [PATCH 07/12] fix appraisal --- .github/workflows/continuous_integration.yml | 5 ++--- README.md | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index 00cf842..2116113 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -46,12 +46,11 @@ jobs: uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} - - name: inject appraisal ${{ matrix.apprasial }} + - name: inject appraisal ${{ matrix.appraisal }} if: matrix.appraisal && matrix.appraisal != '' run: | # inject a specific appraisal Gemfile - bundle update bundle exec appraisal generate - bundle config set gemfile "gemfiles/${{ matrix.apprasial }}.gemfile" + bundle config set gemfile "gemfiles/${{ matrix.appraisal }}.gemfile" - name: install dependencies run: bundle install - name: specs diff --git a/README.md b/README.md index 15039cc..b12c067 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +![Continuous Integration](https://github.com/bdurand/lumberjack/workflows/Continuous%20Integration/badge.svg) +[![Maintainability](https://api.codeclimate.com/v1/badges/a0abc03721fff9b0cde1/maintainability)](https://codeclimate.com/github/bdurand/lumberjack/maintainability) +[![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard) + +# Is It Broken? # Lumberjack [![Build Status](https://travis-ci.org/bdurand/lumberjack.svg?branch=master)](https://travis-ci.org/bdurand/lumberjack) From 3eb19f552ee97851ba3e2cbe42844de3c5acaae3 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 22:27:10 -0700 Subject: [PATCH 08/12] fix appraisal --- .github/workflows/continuous_integration.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index 2116113..638dce7 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -49,6 +49,7 @@ jobs: - name: inject appraisal ${{ matrix.appraisal }} if: matrix.appraisal && matrix.appraisal != '' run: | # inject a specific appraisal Gemfile + bundle install bundle exec appraisal generate bundle config set gemfile "gemfiles/${{ matrix.appraisal }}.gemfile" - name: install dependencies From 13536c7bb003acd452557a79530b117844ff9b66 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 22:33:18 -0700 Subject: [PATCH 09/12] fix appraisal --- .github/workflows/continuous_integration.yml | 8 ++++---- Gemfile | 2 -- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index 638dce7..d57f755 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -12,7 +12,6 @@ env: BUNDLE_PATH: vendor/bundle BUNDLE_JOBS: 3 BUNDLE_RETRY: 3 - BUNDLE_WITHOUT: standard yard jobs: specs: name: ${{ matrix.job }} ruby-${{ matrix.ruby }} ${{ matrix.appraisal }} @@ -37,8 +36,6 @@ jobs: job: rspec - ruby: 2.7 job: standardrb - env: - BUNDLE_WITHOUT: "yard" steps: - name: checkout uses: actions/checkout@v2 @@ -46,10 +43,13 @@ jobs: uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} + - name: remove standardrb + if: matrix.appraisal != 'activesupport_4' + run: sed -i 's/gem "standard"/# gem "standard"' Gemfile - name: inject appraisal ${{ matrix.appraisal }} if: matrix.appraisal && matrix.appraisal != '' run: | # inject a specific appraisal Gemfile - bundle install + bundle update bundle exec appraisal generate bundle config set gemfile "gemfiles/${{ matrix.appraisal }}.gemfile" - name: install dependencies diff --git a/Gemfile b/Gemfile index f4ca874..ac92db3 100644 --- a/Gemfile +++ b/Gemfile @@ -9,9 +9,7 @@ group :development, :test do gem "rspec", "~> 3.9" gem "timecop" gem "appraisal" -end -group :standardrb do # Lock standard to a particular version, esp. cause it's still 0.x.x according to Semver gem "standard", "0.5.2", require: false end From 2f5980a079a99c725d92eb54a21aa08a0604f6af Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 22:35:02 -0700 Subject: [PATCH 10/12] fix appraisal --- .github/workflows/continuous_integration.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index d57f755..ac1ceb8 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -44,8 +44,8 @@ jobs: with: ruby-version: ${{ matrix.ruby }} - name: remove standardrb - if: matrix.appraisal != 'activesupport_4' - run: sed -i 's/gem "standard"/# gem "standard"' Gemfile + if: matrix.appraisal == 'activesupport_4' + run: sed -i 's/gem "standard"/# gem "standard"/' Gemfile - name: inject appraisal ${{ matrix.appraisal }} if: matrix.appraisal && matrix.appraisal != '' run: | # inject a specific appraisal Gemfile From d1a4a9c764a93bb358bdb196424855c1adf8f441 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 22:42:56 -0700 Subject: [PATCH 11/12] fix appraisal --- lib/lumberjack.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/lumberjack.rb b/lib/lumberjack.rb index 066d18d..6bb412f 100644 --- a/lib/lumberjack.rb +++ b/lib/lumberjack.rb @@ -6,7 +6,9 @@ require "logger" module Lumberjack - LINE_SEPARATOR = (/mswin/i.match?(RbConfig::CONFIG["host_os"]) ? "\r\n" : "\n") + # rubocop:disable Performance/RegexpMatch + LINE_SEPARATOR = (RbConfig::CONFIG["host_os"] =~ /mswin/i ? "\r\n" : "\n") + # rubocop:enable Performance/RegexpMatch require_relative "lumberjack/severity" require_relative "lumberjack/formatter" From dfc4b663ff3b7d640b0c3d91466df3419a184619 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 26 Aug 2020 22:55:40 -0700 Subject: [PATCH 12/12] fix appraisal --- .standard.yml | 1 + lib/lumberjack.rb | 2 -- lib/lumberjack/device/size_rolling_log_file.rb | 2 +- spec/device/rolling_log_file_spec.rb | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.standard.yml b/.standard.yml index 6c9ccb2..c50a30f 100644 --- a/.standard.yml +++ b/.standard.yml @@ -7,6 +7,7 @@ format: progress ignore: - '**/*': - Standard/SemanticBlocks + - Performance/RegexpMatch - 'spec/**/*': - Lint/UselessAssignment - Lint/Void diff --git a/lib/lumberjack.rb b/lib/lumberjack.rb index 6bb412f..093c4a7 100644 --- a/lib/lumberjack.rb +++ b/lib/lumberjack.rb @@ -6,9 +6,7 @@ require "logger" module Lumberjack - # rubocop:disable Performance/RegexpMatch LINE_SEPARATOR = (RbConfig::CONFIG["host_os"] =~ /mswin/i ? "\r\n" : "\n") - # rubocop:enable Performance/RegexpMatch require_relative "lumberjack/severity" require_relative "lumberjack/formatter" diff --git a/lib/lumberjack/device/size_rolling_log_file.rb b/lib/lumberjack/device/size_rolling_log_file.rb index 699ade5..b0562e7 100644 --- a/lib/lumberjack/device/size_rolling_log_file.rb +++ b/lib/lumberjack/device/size_rolling_log_file.rb @@ -51,7 +51,7 @@ def roll_file? def next_archive_number # :nodoc: max = 0 Dir.glob("#{path}.*").each do |filename| - if /\.\d+$/.match?(filename) + if /\.\d+\z/ =~ filename suffix = filename.split(".").last.to_i max = suffix if suffix > max end diff --git a/spec/device/rolling_log_file_spec.rb b/spec/device/rolling_log_file_spec.rb index 3a05049..3930e05 100644 --- a/spec/device/rolling_log_file_spec.rb +++ b/spec/device/rolling_log_file_spec.rb @@ -78,7 +78,7 @@ end # Process.fork is unavailable on jruby so we need to use the java threads instead. - if RUBY_PLATFORM.match?(/java/) + if RUBY_PLATFORM =~ /java/ outer_threads = [] process_count.times do outer_threads << Thread.new(&logger_test)