Skip to content

Commit

Permalink
Merge pull request #44 from bdurand/logger-compatibility
Browse files Browse the repository at this point in the history
Logger compatibility
  • Loading branch information
bdurand authored Nov 13, 2023
2 parents b7487e8 + 99f7756 commit fc6fbbc
Show file tree
Hide file tree
Showing 35 changed files with 123 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- ruby: "ruby"
appraisal: logger_gem
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby ${{ matrix.ruby }}
uses: ruby/setup-ruby@v1
with:
Expand Down
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.2.10

### Added

- Added `with_level` method for compatibility with the latest standard library logger gem.

### Fixed

- Fixed typo in magic frozen string literal comments. (thanks @andyw8 and @steveclarke)

## 1.2.9

### Added
Expand Down Expand Up @@ -134,8 +144,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix Ruby 2.4 deprecation warning on Fixnum (thanks koic).
- Fix gemspec files to be flat array (thanks e2).
- Fix Ruby 2.4 deprecation warning on Fixnum (thanks @koic).
- Fix gemspec files to be flat array (thanks @e2).

## 1.0.10

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ gem "rspec", "~> 3.12"
gem "timecop"
gem "appraisal"
gem "standard", require: false
gem "simplecov", require: false
gem "yard"
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
![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)

# Lumberjack

[![Continuous Integration](https://github.com/bdurand/lumberjack/actions/workflows/continuous_integration.yml/badge.svg)](https://github.com/bdurand/lumberjack/actions/workflows/continuous_integration.yml)
[![Regression Test](https://github.com/bdurand/lumberjack/actions/workflows/regression_test.yml/badge.svg)](https://github.com/bdurand/lumberjack/actions/workflows/regression_test.yml)
[![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
[![Gem Version](https://badge.fury.io/rb/lumberjack.svg)](https://badge.fury.io/rb/lumberjack)

Lumberjack is a simple, powerful, and fast logging implementation in Ruby. It uses nearly the same API as the Logger class in the Ruby standard library and as ActiveSupport::BufferedLogger in Rails.

## Usage
Expand Down Expand Up @@ -276,3 +277,31 @@ To send log messages to syslog instead of to a file, you could use this (require
```ruby
config.logger = Lumberjack::Logger.new(Lumberjack::SyslogDevice.new)
```

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'lumberjack'
```

And then execute:
```bash
$ bundle
```

Or install it yourself as:
```bash
$ gem install lumberjack
```

## Contributing

Open a pull request on GitHub.

Please use the [standardrb](https://github.com/testdouble/standard) syntax and lint your code with `standardrb --fix` before submitting.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.9
1.2.10
3 changes: 2 additions & 1 deletion lib/lumberjack.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literals: true
# frozen_string_literal: true

require "rbconfig"
require "time"
require "securerandom"
require "logger"
require "fiber"

module Lumberjack
LINE_SEPARATOR = ((RbConfig::CONFIG["host_os"] =~ /mswin/i) ? "\r\n" : "\n")
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/device.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
# This is an abstract class for logging devices. Subclasses must implement the +write+ method and
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/device/date_rolling_log_file.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

require "date"

Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/device/log_file.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

require "fileutils"

Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/device/multi.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Device
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/device/null.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Device
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/device/rolling_log_file.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Device
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/device/size_rolling_log_file.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Device
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/device/writer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Device
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
# This class controls the conversion of log entry messages into a loggable format. This allows you
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter/date_time_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Formatter
Expand Down
4 changes: 2 additions & 2 deletions lib/lumberjack/formatter/exception_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Formatter
Expand All @@ -16,7 +16,7 @@ def initialize(backtrace_cleaner = nil)
end

def call(exception)
message = "#{exception.class.name}: #{exception.message}"
message = +"#{exception.class.name}: #{exception.message}"
trace = exception.backtrace
if trace
trace = clean_backtrace(trace)
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter/id_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Formatter
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter/inspect_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Formatter
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter/object_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Formatter
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter/pretty_print_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

require "pp"
require "stringio"
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter/string_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Formatter
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter/strip_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Formatter
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter/structured_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

require "set"

Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/formatter/truncate_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
class Formatter
Expand Down
4 changes: 2 additions & 2 deletions lib/lumberjack/log_entry.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
# An entry in a log is a data structure that captures the log message as well as
Expand Down Expand Up @@ -66,7 +66,7 @@ def tag(name)
private

def tags_to_s
tags_string = ""
tags_string = +""
tags&.each { |name, value| tags_string << " #{name}:#{value.inspect}" }
tags_string
end
Expand Down
16 changes: 10 additions & 6 deletions lib/lumberjack/logger.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
# Logger is a thread safe logging object. It has a compatible API with the Ruby
Expand Down Expand Up @@ -118,15 +118,19 @@ def level
# @param [Integer, Symbol, String] value The severity level.
# @return [void]
def level=(value)
@level = if value.is_a?(Integer)
value
else
Severity.label_to_level(value)
end
@level = Severity.coerce(value)
end

alias_method :sev_threshold=, :level=

# Adjust the log level during the block execution for the current Fiber only.
#
# @param [Integer, Symbol, String] severity The severity level.
# @return [Object] The result of the block.
def with_level(severity, &block)
push_thread_local_value(:lumberjack_logger_level, Severity.coerce(severity), &block)
end

# Set the Lumberjack::Formatter used to format objects for logging as messages.
#
# @param [Lumberjack::Formatter, Object] value The formatter to use.
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/rack.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
module Rack
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/rack/context.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
module Rack
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/rack/request_id.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
module Rack
Expand Down
2 changes: 1 addition & 1 deletion lib/lumberjack/rack/unit_of_work.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
module Rack
Expand Down
14 changes: 13 additions & 1 deletion lib/lumberjack/severity.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
# The standard severity levels for logging messages.
Expand Down Expand Up @@ -29,6 +29,18 @@ def level_to_label(severity)
def label_to_level(label)
SEVERITY_LABELS.index(label.to_s.upcase) || UNKNOWN
end

# Coerce a value to a severity level.
#
# @param [Integer, String, Symbol] value The value to coerce.
# @return [Integer] The severity level.
def coerce(value)
if value.is_a?(Integer)
value
else
label_to_level(value)
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/lumberjack/template.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literals: true
# frozen_string_literal: true

module Lumberjack
# A template converts entries to strings. Templates can contain the following place holders to
Expand Down Expand Up @@ -98,7 +98,7 @@ def call(entry)
def tag_args(tags, tag_vars)
return [nil] * (tag_vars.size + 1) if tags.nil? || tags.size == 0

tags_string = ""
tags_string = +""
tags.each do |name, value|
unless value.nil? || tag_vars.include?(name)
value = value.to_s
Expand Down
10 changes: 10 additions & 0 deletions spec/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
expect(logger.level).to eq(Logger::INFO)
end

it "should selt the level within a block" do
logger = Lumberjack::Logger.new(:null, level: :warn)
retval = logger.with_level(:info) do
expect(logger.level).to eq(Logger::INFO)
:foo
end
expect(retval).to eq(:foo)
expect(logger.level).to eq(Logger::WARN)
end

it "should set the progname" do
logger = Lumberjack::Logger.new(:null, progname: "app")
expect(logger.progname).to eq("app")
Expand Down
19 changes: 12 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
rescue LoadError => e
end

require File.expand_path("../../lib/lumberjack.rb", __FILE__)
require "stringio"
require "fileutils"
require "timecop"

RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = [:should, :expect]
end
config.mock_with :rspec do |c|
c.syntax = [:should, :expect]
begin
require "simplecov"
SimpleCov.start do
add_filter ["/spec/"]
end
rescue LoadError
end

require File.expand_path("../../lib/lumberjack.rb", __FILE__)

RSpec.configure do |config|
config.warnings = true
config.order = :random
end

def tmp_dir
Expand Down

0 comments on commit fc6fbbc

Please sign in to comment.