log_decorator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ba107a1dc8b72623b1856a7313aa34b531a8b83d
4
+ data.tar.gz: e50d21dce4fdb6ea705b54e8af6734fd8f734829
5
+ SHA512:
6
+ metadata.gz: d2cc774af0caacf463887e802425d9fef36aba9b0735ee1e5f5f9819fa497084c1293f4d53c59924fdc1555700a0887dcadf0fe5d6ad745adce08cdf9cc22e4a
7
+ data.tar.gz: 4b90098ffec9fadb8a72a9fccf6d318fef4b8e5770d18abe6f637e3d47e296df512b80ab3be3c1ff7935767d81b34c92edb00616c9a2d90d26c0f167d80189d0
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in log_decorator.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 ManageIQ
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,80 @@
1
+ # LogDecorator
2
+
3
+ Acts as a proxy to a configurable underlying logging mechanism,
4
+ permitting the code to be executed in absence of the logger.
5
+
6
+ Provides hooks to permit the "decoration" of log messages. By default,
7
+ the class and method name of the caller are added to the log message.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'log_decorator'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install log_decorator
24
+
25
+ ## Usage
26
+
27
+ ### Initialization
28
+
29
+ ```ruby
30
+ logger = Logger.new # Instantiate an instance of the logger of your choice
31
+ logger.level = desired_log_level # Set up the logger as needed
32
+ LogDecorator.logger = logger # Tell LogDecorator to use the logger
33
+ ```
34
+
35
+ ### Use
36
+
37
+ Given:
38
+
39
+ ```ruby
40
+ class MyClass
41
+ include LogDecorator
42
+
43
+ def self.method_1
44
+ _log.debug "Called"
45
+ end
46
+
47
+ def method_2
48
+ _log.debug "Called"
49
+ end
50
+ end
51
+ ```
52
+
53
+ Then:
54
+
55
+ ```ruby
56
+ MyClass.method_1
57
+ ```
58
+
59
+ Logs:
60
+
61
+ ```
62
+ MyClass.method_1 Called
63
+ ```
64
+
65
+ and
66
+
67
+ ```ruby
68
+ MyClass.new.method_2
69
+ ```
70
+
71
+ Logs:
72
+
73
+ ```
74
+ MyClass#method_2 Called
75
+ ```
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "log_decorator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ require "log_decorator/version"
2
+ require "log_decorator/log_proxy"
3
+ require "log_decorator/logging"
4
+
5
+ module LogDecorator
6
+ DEFAULT_PREFIX = lambda do |klass, separator, location|
7
+ location = location.first if location.is_a?(Array)
8
+ meth = location.base_label
9
+ meth ? "#{klass}#{separator}#{meth}" : klass
10
+ end
11
+
12
+ DEFAULT_LOG_LEVELS = [:debug, :info, :warn, :error]
13
+
14
+ class << self
15
+ attr_writer :prefix, :log_levels
16
+ attr_accessor :logger
17
+
18
+ def prefix
19
+ @prefix ||= DEFAULT_PREFIX
20
+ end
21
+
22
+ def log_levels
23
+ @log_levels ||= DEFAULT_LOG_LEVELS
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,50 @@
1
+ module LogDecorator
2
+ class LogProxy
3
+ attr_accessor :prefix, :logger
4
+
5
+ def self.initialize_log_levels # rubocop:disable AbcSize
6
+ return if @log_levels_initialized
7
+ @log_levels_initialized = true
8
+
9
+ LogDecorator.log_levels.each do |level|
10
+ define_method(level) do |msg = nil, &blk|
11
+ return unless logger # rubocop:disable Lint/NonLocalExitFromIterator
12
+ location = caller_locations(1, 1)
13
+ if blk
14
+ logger.send(level) do
15
+ "#{@prefix.call(@klass, @separator, location)} #{blk.call}"
16
+ end
17
+ else
18
+ logger.send(level, "#{@prefix.call(@klass, @separator, location)} #{msg}")
19
+ end
20
+ end
21
+
22
+ level_bool = :"#{level}?"
23
+ define_method(level_bool) do
24
+ return unless logger # rubocop:disable Lint/NonLocalExitFromIterator
25
+ logger.send(level_bool)
26
+ end
27
+ end
28
+ end
29
+
30
+ def initialize(klass, separator)
31
+ self.class.initialize_log_levels
32
+ @klass = klass
33
+ @separator = separator
34
+ @prefix = LogDecorator.prefix
35
+ @logger = LogDecorator.logger
36
+ end
37
+
38
+ def level
39
+ logger.level if logger
40
+ end
41
+
42
+ def level=(level)
43
+ logger.level = level if logger
44
+ end
45
+
46
+ def log_backtrace
47
+ logger.log_backtrace if logger
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,33 @@
1
+ module LogDecorator
2
+ module Logging
3
+ module ClassMethods
4
+ def instance_logger
5
+ @instance_logger ||= LogProxy.new(name, '#')
6
+ end
7
+
8
+ def _log
9
+ @_log ||= LogProxy.new(name, '.')
10
+ end
11
+
12
+ def _logger=(logger)
13
+ _log.logger = logger
14
+ instance_logger.logger = logger
15
+ end
16
+
17
+ def _log_prefix=(prefix)
18
+ _log.prefix = prefix
19
+ instance_logger.prefix = prefix
20
+ end
21
+ end
22
+
23
+ def self.included(host_class)
24
+ host_class.extend(ClassMethods) unless host_class.respond_to?(:_log)
25
+ host_class.instance_logger
26
+ host_class._log
27
+ end
28
+
29
+ def _log
30
+ self.class.instance_logger
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module LogDecorator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'log_decorator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "log_decorator"
8
+ spec.version = LogDecorator::VERSION
9
+ spec.authors = ["ManageIQ Authors"]
10
+
11
+ spec.summary = "Add caller location information to log messages - class and method names."
12
+ spec.description = %q(
13
+ Acts as a proxy to a configurable underlying logging mechanism,
14
+ permitting the code to be executed in absence of the logger.
15
+
16
+ Provides hooks to permit the "decoration" of log messages. By default,
17
+ the class and method name of the caller are added to the log message.
18
+ )
19
+ spec.homepage = "https://github.com/ManageIQ/log_decorator"
20
+ spec.license = "MIT"
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.10"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "log4r"
31
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log_decorator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ManageIQ Authors
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: log4r
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
+ description: "\n Acts as a proxy to a configurable underlying logging mechanism,\n
70
+ \ permitting the code to be executed in absence of the logger.\n\n Provides
71
+ hooks to permit the \"decoration\" of log messages. By default,\n the class and
72
+ method name of the caller are added to the log message.\n "
73
+ email:
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/log_decorator.rb
88
+ - lib/log_decorator/log_proxy.rb
89
+ - lib/log_decorator/logging.rb
90
+ - lib/log_decorator/version.rb
91
+ - log_decorator.gemspec
92
+ homepage: https://github.com/ManageIQ/log_decorator
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.11
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Add caller location information to log messages - class and method names.
116
+ test_files: []