mixlib-log 2.0.4 → 3.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f38162b462a52fdf0dd4465f19065171321ac07e34a5a65f9ab577f77b22dcf
4
- data.tar.gz: dafa77b839b7ea7f94ee5614d29a054756d2d0115100fa7f3b44f2afcb23624a
3
+ metadata.gz: 95493c0814c7d9168427a11a611f5e55eb9b104635267bb2f62fae5c29b5d005
4
+ data.tar.gz: 3a64b2405bbe57c2cb2bfb72b815c67c3470b38c47dd6835c2dba61d5195f18d
5
5
  SHA512:
6
- metadata.gz: e4b4141f0fcbe6f14ac1ff949f7771179cb617af4fc0f7d1f934d7e3db67e70dfd9dc443886f64ceda4a1531b946c945567bea69e82405cef3ea746bb28bf9ff
7
- data.tar.gz: 6c2a02ccc1d4967241d5a4439562cff07ef0620c750ee29809ecb172c81a24e7b7b746ebcc4da8fdf6a3f2f1d89210f7cc3fee5efbe96eac460aa082c720ecdf
6
+ metadata.gz: cde4bc246b2e9878fe2ce87026c6be03cf6c9d7d031f3fe29c73eb1d162076f8b30820ad8cccfa125f012787da33de5e681c25bc15f4ac1413e45b3dc8cd6b07
7
+ data.tar.gz: 6f88ebc29a7e9989f1d391d44a105c1583573e7b7f70a4c8cb6853202c52417614dece03a48b3bdb9210e9a2f1a9e42fc24540ce3ad921406c6d2041ba30fdbd
@@ -17,26 +17,27 @@
17
17
  # limitations under the License.
18
18
 
19
19
  require "logger"
20
- require "mixlib/log/version"
21
- require "mixlib/log/formatter"
22
- require "mixlib/log/child"
23
- require "mixlib/log/logging"
24
- require "mixlib/log/logger"
20
+ require_relative "log/version"
21
+ require_relative "log/formatter"
22
+ require_relative "log/child"
23
+ require_relative "log/logging"
24
+ require_relative "log/logger"
25
25
 
26
26
  module Mixlib
27
27
  module Log
28
28
 
29
29
  include Logging
30
- @logger, @loggers = nil
31
30
 
32
31
  def reset!
32
+ @logger ||= nil
33
+ @loggers ||= []
33
34
  close!
34
- @logger, @loggers = nil, nil
35
+ @logger = @loggers = nil
35
36
  @metadata = {}
36
37
  end
37
38
 
38
39
  # An Array of log devices that will be logged to. Defaults to just the default
39
- # @logger log device, but you can push to this array to add more devices.
40
+ # \@logger log device, but you can push to this array to add more devices.
40
41
  def loggers
41
42
  @loggers ||= [logger]
42
43
  end
@@ -46,7 +47,7 @@ module Mixlib
46
47
  # and creates a new one if it doesn't yet exist
47
48
  ##
48
49
  def logger
49
- @logger || init
50
+ @logger ||= init
50
51
  end
51
52
 
52
53
  # Sets the log device to +new_log_device+. Any additional loggers
@@ -60,7 +61,7 @@ module Mixlib
60
61
  if other.respond_to?(:loggers) && other.respond_to?(:logger)
61
62
  @loggers = other.loggers
62
63
  @logger = other.logger
63
- elsif other.kind_of?(Array)
64
+ elsif other.is_a?(Array)
64
65
  @loggers = other
65
66
  @logger = other.first
66
67
  else
@@ -81,7 +82,7 @@ module Mixlib
81
82
  def init(*opts)
82
83
  reset!
83
84
  @logger = logger_for(*opts)
84
- @logger.formatter = Mixlib::Log::Formatter.new() if @logger.respond_to?(:formatter=)
85
+ @logger.formatter = Mixlib::Log::Formatter.new if @logger.respond_to?(:formatter=)
85
86
  @logger.level = Logger::WARN
86
87
  @configured = true
87
88
  @parent = nil
@@ -109,6 +110,7 @@ module Mixlib
109
110
  def level=(new_level)
110
111
  level_int = LEVEL_NAMES.key?(new_level) ? new_level : LEVELS[new_level]
111
112
  raise ArgumentError, "Log level must be one of :trace, :debug, :info, :warn, :error, or :fatal" if level_int.nil?
113
+
112
114
  loggers.each { |l| l.level = level_int }
113
115
  end
114
116
 
@@ -124,7 +126,7 @@ module Mixlib
124
126
  # Note that we *only* query the default logger (@logger) and not any other
125
127
  # loggers that may have been added, even though it is possible to configure
126
128
  # two (or more) loggers at different log levels.
127
- [:trace?, :debug?, :info?, :warn?, :error?, :fatal?].each do |method_name|
129
+ %i{trace? debug? info? warn? error? fatal?}.each do |method_name|
128
130
  define_method(method_name) do
129
131
  logger.send(method_name)
130
132
  end
@@ -136,7 +138,7 @@ module Mixlib
136
138
 
137
139
  def add(severity, message = nil, progname = nil, data: {}, &block)
138
140
  message, progname, data = yield if block_given?
139
- data = metadata.merge(data) if metadata.kind_of?(Hash) && data.kind_of?(Hash)
141
+ data = metadata.merge(data) if metadata.is_a?(Hash) && data.is_a?(Hash)
140
142
  loggers.each do |l|
141
143
  # if we don't have any metadata, let's not do the potentially expensive
142
144
  # merging and managing that this call requires
@@ -170,7 +172,7 @@ module Mixlib
170
172
 
171
173
  def logger_for(*opts)
172
174
  if opts.empty?
173
- Mixlib::Log::Logger.new(STDOUT)
175
+ Mixlib::Log::Logger.new($stdout)
174
176
  elsif LEVELS.keys.inject(true) { |quacks, level| quacks && opts.first.respond_to?(level) }
175
177
  opts.first
176
178
  else
@@ -190,7 +192,9 @@ module Mixlib
190
192
  # via public API. In order to reduce amount of impact and
191
193
  # handle only File type log devices I had to use this method
192
194
  # to get access to it.
195
+ next unless logger.instance_variable_defined?(:"@logdev")
193
196
  next unless (logdev = logger.instance_variable_get(:"@logdev"))
197
+
194
198
  loggers_to_close << logger if logdev.filename
195
199
  end
196
200
  loggers_to_close
@@ -14,7 +14,7 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- require "mixlib/log/logging"
17
+ require_relative "logging"
18
18
 
19
19
  module Mixlib
20
20
  module Log
@@ -36,14 +36,14 @@ module Mixlib
36
36
  # Note that we *only* query the default logger (@logger) and not any other
37
37
  # loggers that may have been added, even though it is possible to configure
38
38
  # two (or more) loggers at different log levels.
39
- [:trace?, :debug?, :info?, :warn?, :error?, :fatal?].each do |method_name|
39
+ %i{trace? debug? info? warn? error? fatal?}.each do |method_name|
40
40
  define_method(method_name) do
41
41
  parent.send(method_name)
42
42
  end
43
43
  end
44
44
 
45
45
  def add(severity, message = nil, progname = nil, data: {}, &block)
46
- data = metadata.merge(data) if data.kind_of?(Hash)
46
+ data = metadata.merge(data) if data.is_a?(Hash)
47
47
  parent.send(:pass, severity, message, progname, data: data, &block)
48
48
  end
49
49
 
@@ -16,7 +16,7 @@
16
16
  # limitations under the License.
17
17
 
18
18
  require "logger"
19
- require "time"
19
+ require "time" unless defined?(Time.zone_offset)
20
20
 
21
21
  module Mixlib
22
22
  module Log
@@ -31,7 +31,7 @@ module Mixlib
31
31
  # Otherwise, doesn't print the time.
32
32
  def call(severity, time, progname, msg)
33
33
  if @@show_time
34
- sprintf("[%s] %s: %s\n", time.iso8601(), severity, msg2str(msg))
34
+ sprintf("[%s] %s: %s\n", time.iso8601, severity, msg2str(msg))
35
35
  else
36
36
  sprintf("%s: %s\n", severity, msg2str(msg))
37
37
  end
@@ -1,5 +1,5 @@
1
1
  require "logger"
2
- require "mixlib/log/logging"
2
+ require_relative "logging"
3
3
 
4
4
  # A subclass of Ruby's stdlib Logger with all the mutex and logrotation stuff
5
5
  # ripped out, and metadata added in.
@@ -21,7 +21,7 @@ module Mixlib
21
21
  #
22
22
  # +logdev+::
23
23
  # The log device. This is a filename (String) or IO object (typically
24
- # +STDOUT+, +STDERR+, or an open file).
24
+ # +$stdout+, +$stderr+, or an open file).
25
25
  # +shift_age+::
26
26
  # Number of old log files to keep, *or* frequency of rotation (+daily+,
27
27
  # +weekly+ or +monthly+).
@@ -45,14 +45,16 @@ module Mixlib
45
45
 
46
46
  def add_data(severity, message, progname, data: {})
47
47
  return true if @logdev.nil? || severity < @level
48
+
48
49
  data ||= {}
49
- if message.kind_of?(::Exception)
50
+ if message.is_a?(::Exception)
50
51
  data[:err] = message
51
52
  else
52
53
  data[:msg] = message
53
54
  end
54
55
  @logdev.write(
55
- format_message(to_label(severity), Time.now, progname, data))
56
+ format_message(to_label(severity), Time.now, progname, data)
57
+ )
56
58
  true
57
59
  end
58
60
  alias_method :add, :add_data
@@ -29,7 +29,7 @@ module Mixlib
29
29
  SEV_LABEL[sev + 1] || -"ANY"
30
30
  end
31
31
 
32
- LEVELS = { :trace => TRACE, :debug => DEBUG, :info => INFO, :warn => WARN, :error => ERROR, :fatal => FATAL }.freeze
32
+ LEVELS = { trace: TRACE, debug: DEBUG, info: INFO, warn: WARN, error: ERROR, fatal: FATAL }.freeze
33
33
  LEVEL_NAMES = LEVELS.invert.freeze
34
34
 
35
35
  attr_accessor :metadata
@@ -41,7 +41,7 @@ module Mixlib
41
41
 
42
42
  # Define the standard logger methods on this class programmatically.
43
43
  # No need to incur method_missing overhead on every log call.
44
- [:trace, :debug, :info, :warn, :error, :fatal].each do |method_name|
44
+ %i{trace debug info warn error fatal}.each do |method_name|
45
45
  level = LEVELS[method_name]
46
46
  define_method(method_name) do |msg = nil, data: {}, &block|
47
47
  pass(level, msg, data: data, &block)
@@ -1,5 +1,5 @@
1
1
  module Mixlib
2
2
  module Log
3
- VERSION = "2.0.4"
3
+ VERSION = "3.0.9".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,112 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixlib-log
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 3.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef Software, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-12 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rake
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '3.7'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '3.7'
41
- - !ruby/object:Gem::Dependency
42
- name: chefstyle
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: cucumber
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: github_changelog_generator
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: 1.11.3
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: 1.11.3
11
+ date: 2020-08-21 00:00:00.000000000 Z
12
+ dependencies: []
83
13
  description:
84
14
  email: info@chef.io
85
15
  executables: []
86
16
  extensions: []
87
- extra_rdoc_files:
88
- - README.md
89
- - LICENSE
90
- - NOTICE
17
+ extra_rdoc_files: []
91
18
  files:
92
- - ".gemtest"
93
- - Gemfile
94
19
  - LICENSE
95
- - NOTICE
96
- - README.md
97
- - Rakefile
98
20
  - lib/mixlib/log.rb
99
21
  - lib/mixlib/log/child.rb
100
22
  - lib/mixlib/log/formatter.rb
101
23
  - lib/mixlib/log/logger.rb
102
24
  - lib/mixlib/log/logging.rb
103
25
  - lib/mixlib/log/version.rb
104
- - mixlib-log.gemspec
105
- - spec/mixlib/log/child_spec.rb
106
- - spec/mixlib/log/formatter_spec.rb
107
- - spec/mixlib/log_spec.rb
108
- - spec/spec_helper.rb
109
- homepage: https://www.chef.io
26
+ homepage: https://github.com/chef/mixlib-log
110
27
  licenses:
111
28
  - Apache-2.0
112
29
  metadata: {}
@@ -118,15 +35,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
35
  requirements:
119
36
  - - ">="
120
37
  - !ruby/object:Gem::Version
121
- version: '2.2'
38
+ version: '2.3'
122
39
  required_rubygems_version: !ruby/object:Gem::Requirement
123
40
  requirements:
124
41
  - - ">="
125
42
  - !ruby/object:Gem::Version
126
43
  version: '0'
127
44
  requirements: []
128
- rubyforge_project:
129
- rubygems_version: 2.7.6
45
+ rubygems_version: 3.0.3
130
46
  signing_key:
131
47
  specification_version: 4
132
48
  summary: A gem that provides a simple mixin for log functionality
data/.gemtest DELETED
File without changes
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gemspec
4
-
5
- group :development do
6
- gem "rdoc"
7
- gem "bundler"
8
- end
data/NOTICE DELETED
@@ -1,28 +0,0 @@
1
- ============
2
- Mixin::Log Notices
3
- ============
4
-
5
- Developed at Chef (http://www.chef.io).
6
-
7
-
8
- * Copyright 2009-2016, Chef Software, Inc. <legal@chef.io>
9
-
10
- Mixin::Log incorporates code from Chef. The Chef notice file follows:
11
-
12
- ============
13
- Chef Notices
14
- ============
15
-
16
- Developed at Chef (http://www.chef.io).
17
-
18
- Contributors and Copyright holders:
19
-
20
- * Copyright 2008, Adam Jacob <adam@chef.io>
21
- * Copyright 2008, Arjuna Christensen <aj@hjksolutions.com>
22
- * Copyright 2008, Bryan McLellan <btm@loftninjas.org>
23
- * Copyright 2008, Ezra Zygmuntowicz <ezra@engineyard.com>
24
- * Copyright 2009, Sean Cribbs <seancribbs@gmail.com>
25
- * Copyright 2009, Christopher Brown <cb@chef.io>
26
- * Copyright 2009, Thom May <thom@clearairturbulence.org>
27
-
28
- Chef incorporates code modified from Open4 (http://www.codeforpeople.com/lib/ruby/open4/), which was written by Ara T. Howard.
data/README.md DELETED
@@ -1,55 +0,0 @@
1
- # Mixlib::Log
2
-
3
- [![Build Status Master](https://travis-ci.org/chef/mixlib-log.svg?branch=master)](https://travis-ci.org/chef/mixlib-log) [![Gem Version](https://badge.fury.io/rb/mixlib-log.svg)](https://badge.fury.io/rb/mixlib-log)
4
-
5
- Mixlib::Log provides a mixin for enabling a class based logger object, a-la Merb, Chef, and Nanite. To use it:
6
-
7
- ```ruby
8
- require 'mixlib/log'
9
-
10
- class Log
11
- extend Mixlib::Log
12
- end
13
- ```
14
-
15
- You can then do:
16
-
17
- ```ruby
18
- Log.debug('foo')
19
- Log.info('bar')
20
- Log.warn('baz')
21
- Log.error('baz')
22
- Log.fatal('wewt')
23
- ```
24
-
25
- By default, `Mixlib::Logger` logs to STDOUT. To alter this, you should call `Log.init`, passing any arguments to the standard Ruby Logger. For example:
26
-
27
- ```ruby
28
- Log.init('/tmp/logfile') # log to /tmp/logfile
29
- Log.init('/tmp/logfile', 7) # log to /tmp/logfile, rotate every day
30
- ```
31
-
32
- Enjoy!
33
-
34
- ## Contributing
35
-
36
- For information on contributing to this project see <https://github.com/chef/chef/blob/master/CONTRIBUTING.md>
37
-
38
- ## License
39
-
40
- - Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
41
- - License:: Apache License, Version 2.0
42
-
43
- ```text
44
- Licensed under the Apache License, Version 2.0 (the "License");
45
- you may not use this file except in compliance with the License.
46
- You may obtain a copy of the License at
47
-
48
- http://www.apache.org/licenses/LICENSE-2.0
49
-
50
- Unless required by applicable law or agreed to in writing, software
51
- distributed under the License is distributed on an "AS IS" BASIS,
52
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53
- See the License for the specific language governing permissions and
54
- limitations under the License.
55
- ```
data/Rakefile DELETED
@@ -1,43 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rdoc/task"
3
- require "rspec/core/rake_task"
4
- require "cucumber/rake/task"
5
-
6
- RSpec::Core::RakeTask.new(:spec) do |spec|
7
- spec.pattern = "spec/**/*_spec.rb"
8
- end
9
-
10
- task default: [:style, :spec, :features]
11
-
12
- # For rubygems-test
13
- task :test => :spec
14
-
15
- RDoc::Task.new do |rdoc|
16
- rdoc.rdoc_dir = "rdoc"
17
- rdoc.title = "mixlib-log #{Mixlib::Log::VERSION}"
18
- rdoc.rdoc_files.include("README*")
19
- rdoc.rdoc_files.include("lib/**/*.rb")
20
- end
21
-
22
- Cucumber::Rake::Task.new(:features) do |t|
23
- t.cucumber_opts = "--format pretty"
24
- end
25
-
26
- begin
27
- require "chefstyle"
28
- require "rubocop/rake_task"
29
- RuboCop::RakeTask.new(:style) do |task|
30
- task.options += ["--display-cop-names", "--no-color"]
31
- end
32
- rescue LoadError
33
- puts "chefstyle/rubocop is not available. gem install chefstyle to do style checking."
34
- end
35
-
36
- require "github_changelog_generator/task"
37
-
38
- GitHubChangelogGenerator::RakeTask.new :changelog do |config|
39
- config.future_release = Mixlib::Log::VERSION
40
- config.enhancement_labels = "enhancement,Enhancement,New Feature,Feature".split(",")
41
- config.bug_labels = "bug,Bug,Improvement,Upstream Bug".split(",")
42
- config.exclude_labels = "duplicate,question,invalid,wontfix,no_changelog,Exclude From Changelog,Question,Discussion".split(",")
43
- end
@@ -1,22 +0,0 @@
1
- $:.unshift File.expand_path("../lib", __FILE__)
2
- require "mixlib/log/version"
3
-
4
- Gem::Specification.new do |gem|
5
- gem.name = "mixlib-log"
6
- gem.version = Mixlib::Log::VERSION
7
- gem.platform = Gem::Platform::RUBY
8
- gem.summary = "A gem that provides a simple mixin for log functionality"
9
- gem.email = "info@chef.io"
10
- gem.homepage = "https://www.chef.io"
11
- gem.license = "Apache-2.0"
12
- gem.authors = ["Chef Software, Inc."]
13
- gem.has_rdoc = true
14
- gem.extra_rdoc_files = ["README.md", "LICENSE", "NOTICE"]
15
- gem.files = Dir["lib/**/*"] + Dir["spec/**/*"] + ["Gemfile", "Rakefile", ".gemtest", "mixlib-log.gemspec"]
16
- gem.required_ruby_version = ">= 2.2"
17
- gem.add_development_dependency "rake"
18
- gem.add_development_dependency "rspec", "~> 3.7"
19
- gem.add_development_dependency "chefstyle"
20
- gem.add_development_dependency "cucumber"
21
- gem.add_development_dependency "github_changelog_generator", ">= 1.11.3"
22
- end
@@ -1,99 +0,0 @@
1
- #
2
- # Copyright:: Copyright (c) 2018 Chef Software, Inc.
3
- # License:: Apache License, Version 2.0
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- require "tempfile"
19
- require "stringio"
20
- require "spec_helper"
21
-
22
- RSpec.describe Mixlib::Log::Child do
23
- before do
24
- Logit.reset!
25
- Logit.init(io)
26
- Logit.level = :warn
27
- end
28
-
29
- let(:io) { StringIO.new }
30
-
31
- let(:child) { Logit.with_child }
32
-
33
- it "has a parent" do
34
- expect(child.parent).to be(Logit)
35
- end
36
-
37
- it "accepts a message" do
38
- Logit.with_child { |l| l.add(Logger::WARN, "a message") }
39
- expect(io.string).to match(/a message$/)
40
- end
41
-
42
- context "with structured data" do
43
- it "can be created with metadata" do
44
- expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true" })
45
- Logit.with_child({ child: "true" }) { |l| l.warn("a message") }
46
- end
47
-
48
- it "a message can be logged" do
49
- expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true" })
50
- Logit.with_child { |l| l.warn("a message", data: { child: "true" }) }
51
- end
52
-
53
- context "merges properly" do
54
- it "in the simple case" do
55
- expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true", meta: "data" })
56
- Logit.with_child(meta: "data") { |l| l.warn("a message", data: { child: "true" }) }
57
- end
58
-
59
- it "when overwriting" do
60
- expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true", meta: "overwritten" })
61
- Logit.with_child(meta: "data") { |l| l.warn("a message", data: { child: "true", meta: "overwritten" }) }
62
- end
63
- end
64
-
65
- context "when receiving a message from a child" do
66
- it "passes data on" do
67
- expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true", parent: "first" })
68
- child.metadata = { parent: "first" }
69
- child.with_child { |l| l.warn("a message", data: { child: "true" }) }
70
- end
71
-
72
- it "merges its own data" do
73
- expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true", parent: "second" })
74
- child.metadata = { parent: "first" }
75
- child.with_child { |l| l.warn("a message", data: { child: "true", parent: "second" }) }
76
- end
77
- end
78
- end
79
-
80
- context "sends a message to the parent" do
81
- %i{ debug info warn error fatal }.each do |level|
82
- it "at #{level}" do
83
- expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[level], "a #{level} message", nil, data: {})
84
- Logit.level = level
85
- child.send(level, "a #{level} message")
86
- end
87
- end
88
- end
89
-
90
- context "can query the parent's level" do
91
- %i{ debug info warn error fatal }.each do |level|
92
- it "at #{level}" do
93
- query = "#{level}?".to_sym
94
- Logit.level = level
95
- expect(child.send(query)).to be(true)
96
- end
97
- end
98
- end
99
- end
@@ -1,64 +0,0 @@
1
- #
2
- # Author:: Adam Jacob (<adam@chef.io>)
3
- # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require "time"
20
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
21
-
22
- RSpec.describe Mixlib::Log::Formatter do
23
- before(:each) do
24
- @formatter = Mixlib::Log::Formatter.new
25
- end
26
-
27
- it "should print raw strings with msg2str(string)" do
28
- expect(@formatter.msg2str("nuthin new")).to eq("nuthin new")
29
- end
30
-
31
- it "should format exceptions properly with msg2str(e)" do
32
- e = IOError.new("legendary roots crew")
33
- expect(@formatter.msg2str(e)).to eq("legendary roots crew (IOError)\n")
34
- end
35
-
36
- it "should format random objects via inspect with msg2str(Object)" do
37
- expect(@formatter.msg2str([ "black thought", "?uestlove" ])).to eq('["black thought", "?uestlove"]')
38
- end
39
-
40
- it "should return a formatted string with call" do
41
- time = Time.new
42
- Mixlib::Log::Formatter.show_time = true
43
- expect(@formatter.call("monkey", time, "test", "mos def")).to eq("[#{time.iso8601}] monkey: mos def\n")
44
- end
45
-
46
- it "should allow you to turn the time on and off in the output" do
47
- Mixlib::Log::Formatter.show_time = false
48
- expect(@formatter.call("monkey", Time.new, "test", "mos def")).to eq("monkey: mos def\n")
49
- end
50
-
51
- context "with structured data" do
52
- let(:data) { {} }
53
-
54
- it "should format a message" do
55
- data[:msg] = "nuthin new"
56
- expect(@formatter.msg2str(data)).to eq("nuthin new")
57
- end
58
-
59
- it "should format an exception" do
60
- data[:err] = IOError.new("legendary roots crew")
61
- expect(@formatter.msg2str(data)).to eq("legendary roots crew (IOError)\n")
62
- end
63
- end
64
- end
@@ -1,234 +0,0 @@
1
- #
2
- # Author:: Adam Jacob (<adam@chef.io>)
3
- # Author:: Christopher Brown (<cb@chef.io>)
4
- # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
5
- # License:: Apache License, Version 2.0
6
- #
7
- # Licensed under the Apache License, Version 2.0 (the "License");
8
- # you may not use this file except in compliance with the License.
9
- # You may obtain a copy of the License at
10
- #
11
- # http://www.apache.org/licenses/LICENSE-2.0
12
- #
13
- # Unless required by applicable law or agreed to in writing, software
14
- # distributed under the License is distributed on an "AS IS" BASIS,
15
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
-
20
- require "tempfile"
21
- require "stringio"
22
- require "spec_helper"
23
-
24
- class LoggerLike
25
- attr_accessor :level
26
- attr_reader :messages, :data
27
- def initialize
28
- @messages = ""
29
- @data = []
30
- end
31
-
32
- def add_data(severity, message = nil, progname = nil, data: {})
33
- @messages << message
34
- @data << data
35
- end
36
-
37
- def add(severity, message = nil, progname = nil, data: {})
38
- @messages << message
39
- end
40
-
41
- [:trace, :debug, :info, :warn, :error, :fatal].each do |method_name|
42
- class_eval(<<-E)
43
- def #{method_name}(message)
44
- @messages << message
45
- end
46
- E
47
- end
48
- end
49
-
50
- RSpec.describe Mixlib::Log do
51
-
52
- # Since we are testing class behaviour for an instance variable
53
- # that gets set once, we need to reset it prior to each example [cb]
54
- before(:each) do
55
- Logit.reset!
56
- end
57
-
58
- it "creates a logger using an IO object" do
59
- io = StringIO.new
60
- Logit.init(io)
61
- Logit << "foo"
62
- expect(io.string).to match(/foo/)
63
- end
64
-
65
- it "creates a logger with a file name" do
66
- Tempfile.open("chef-test-log") do |tempfile|
67
- Logit.init(tempfile.path)
68
- Logit << "bar"
69
- tempfile.rewind
70
- expect(tempfile.read).to match(/bar/)
71
- end
72
- end
73
-
74
- it "uses the logger provided when initialized with a logger like object" do
75
- logger = LoggerLike.new
76
- Logit.init(logger)
77
- Logit.level = :debug
78
- Logit.debug "qux"
79
- expect(logger.messages).to match(/qux/)
80
- end
81
-
82
- it "should re-initialize the logger if init is called again" do
83
- first_logdev, second_logdev = StringIO.new, StringIO.new
84
- Logit.init(first_logdev)
85
- Logit.fatal "FIRST"
86
- expect(first_logdev.string).to match(/FIRST/)
87
- Logit.init(second_logdev)
88
- Logit.fatal "SECOND"
89
- expect(first_logdev.string).to_not match(/SECOND/)
90
- expect(second_logdev.string).to match(/SECOND/)
91
- end
92
-
93
- it "knows that it's been configured" do
94
- Logit.init
95
- expect(Logit.configured?).to be true
96
- end
97
-
98
- it "should set the log level using the binding form, with :trace, :debug, :info, :warn, :error, or :fatal" do
99
- levels = {
100
- :trace => Mixlib::Log::TRACE,
101
- :debug => Mixlib::Log::DEBUG,
102
- :info => Mixlib::Log::INFO,
103
- :warn => Mixlib::Log::WARN,
104
- :error => Mixlib::Log::ERROR,
105
- :fatal => Mixlib::Log::FATAL,
106
- }
107
- levels.each do |symbol, constant|
108
- Logit.level = symbol
109
- expect(Logit.logger.level).to eq(constant)
110
- expect(Logit.level).to eq(symbol)
111
- end
112
- end
113
-
114
- it "passes blocks to the underlying logger object" do
115
- logdev = StringIO.new
116
- Logit.init(logdev)
117
- Logit.fatal { "the_message" }
118
- expect(logdev.string).to match(/the_message/)
119
- end
120
-
121
- it "should set the log level using the method form, with :trace, :debug, :info, :warn, :error, or :fatal" do
122
- levels = {
123
- :trace => Mixlib::Log::TRACE,
124
- :debug => Mixlib::Log::DEBUG,
125
- :info => Mixlib::Log::INFO,
126
- :warn => Mixlib::Log::WARN,
127
- :error => Mixlib::Log::ERROR,
128
- :fatal => Mixlib::Log::FATAL,
129
- }
130
- levels.each do |symbol, constant|
131
- Logit.level(symbol)
132
- expect(Logit.logger.level).to eq(constant)
133
- end
134
- end
135
-
136
- it "should raise an ArgumentError if you try and set the level to something strange using the binding form" do
137
- expect(lambda { Logit.level = :the_roots }).to raise_error(ArgumentError)
138
- end
139
-
140
- it "should raise an ArgumentError if you try and set the level to something strange using the method form" do
141
- expect(lambda { Logit.level(:the_roots) }).to raise_error(ArgumentError)
142
- end
143
-
144
- it "should pass other method calls directly to logger" do
145
- Logit.level = :debug
146
- expect(Logit).to be_debug
147
- expect(lambda { Logit.debug("Gimme some sugar!") }).to_not raise_error
148
- end
149
-
150
- it "should pass add method calls directly to logger" do
151
- logdev = StringIO.new
152
- Logit.init(logdev)
153
- Logit.level = :debug
154
- expect(Logit).to be_debug
155
- expect(lambda { Logit.add(Logger::DEBUG, "Gimme some sugar!") }).to_not raise_error
156
- expect(logdev.string).to match(/Gimme some sugar/)
157
- end
158
-
159
- it "should default to STDOUT if init is called with no arguments" do
160
- logger_mock = Struct.new(:formatter, :level).new
161
- expect(Logger).to receive(:new).with(STDOUT).and_return(logger_mock)
162
- Logit.init
163
- end
164
-
165
- it "should have by default a base log level of warn" do
166
- logger_mock = Struct.new(:formatter, :level).new
167
- expect(Logger).to receive(:new).and_return(logger_mock)
168
- Logit.init
169
- expect(Logit.level).to eq(:warn)
170
- end
171
-
172
- it "should close File logger" do
173
- opened_files_count_before = 0
174
- ObjectSpace.each_object(File) do |f|
175
- opened_files_count_before += 1 unless f.closed?
176
- end
177
- name = File.join(Dir.tmpdir, "logger.log")
178
- Logit.init(name)
179
- Logit.init(name)
180
- Logit.init(name)
181
- opened_files_count_after = 0
182
- ObjectSpace.each_object(File) do |f|
183
- opened_files_count_after += 1 unless f.closed?
184
- end
185
- expect(opened_files_count_after).to eq(opened_files_count_before + 1)
186
- end
187
-
188
- it "should not close IO logger" do
189
- opened_files_count_before = 0
190
- ObjectSpace.each_object(File) do |f|
191
- opened_files_count_before += 1 unless f.closed?
192
- end
193
- Tempfile.open("chef-test-log") do |file|
194
- Logit.init(file)
195
- Logit.init(file)
196
- Logit.init(file)
197
- opened_files_count_after = 0
198
- ObjectSpace.each_object(File) do |f|
199
- opened_files_count_after += 1 unless f.closed?
200
- end
201
- expect(opened_files_count_after).to eq(opened_files_count_before + 1)
202
- end
203
- end
204
-
205
- it "should return nil from its logging methods" do
206
- expect(Logger).to receive(:new).with(STDOUT) { double("a-quiet-logger").as_null_object }
207
- Logit.init
208
-
209
- aggregate_failures "returns nil from logging method" do
210
- expect(Logit.trace("hello")).to be_nil
211
- expect(Logit.debug("hello")).to be_nil
212
- expect(Logit.info("hello")).to be_nil
213
- expect(Logit.warn("hello")).to be_nil
214
- expect(Logit.error("hello")).to be_nil
215
- expect(Logit.fatal("hello")).to be_nil
216
- end
217
- end
218
-
219
- it "should set metadata correctly" do
220
- Logit.metadata = { test: "data" }
221
- expect(Logit.metadata).to eql({ test: "data" })
222
- end
223
-
224
- it "should format :trace level messages with TRACE: label" do
225
- logdev = StringIO.new
226
- Logit.init(logdev)
227
- Logit.level = :trace
228
- Logit.trace("this is a log message")
229
- aggregate_failures do
230
- expect(logdev.string).to_not match(/ANY:/)
231
- expect(logdev.string).to match(/TRACE:/)
232
- end
233
- end
234
- end
@@ -1,33 +0,0 @@
1
- #
2
- # Author:: Adam Jacob (<adam@chef.io>)
3
- # Author:: Christopher Brown (<cb@chef.io>)
4
- # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
5
- # License:: Apache License, Version 2.0
6
- #
7
- # Licensed under the Apache License, Version 2.0 (the "License");
8
- # you may not use this file except in compliance with the License.
9
- # You may obtain a copy of the License at
10
- #
11
- # http://www.apache.org/licenses/LICENSE-2.0
12
- #
13
- # Unless required by applicable law or agreed to in writing, software
14
- # distributed under the License is distributed on an "AS IS" BASIS,
15
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
-
20
- $TESTING = true
21
- $:.push File.join(File.dirname(__FILE__), "..", "lib")
22
-
23
- require "rspec"
24
- require "mixlib/log"
25
- require "mixlib/log/formatter"
26
-
27
- RSpec.configure do |config|
28
- config.disable_monkey_patching!
29
- end
30
-
31
- class Logit
32
- extend Mixlib::Log
33
- end