logging_factory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTI3NTEyMWVlMGYyMjY3OWVkNWUwNTEzMWM4NThkYzZmMDQ0MmU0Mw==
5
+ data.tar.gz: !binary |-
6
+ NzFiNTBjNjgzMzc1ZjY1OTNlMTg0NDljNjViNmFmOTliNjQwNWI2ZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YTY1NzFhMWZlOGQxOGI5ZDdkMzhjZGVjYTVhOWQwMDY4ZWM3NzQxMzFiNzVh
10
+ OWYxMzYxMTk0MzUwMjM0MGNhNzNiZTE4NDNjMzI0NDExZjZkNGVmNDU4NTgw
11
+ MWQ5OGVkNTkyMTU2NzY5MzMxMjM1MWJiMWI5YjQyNTJmOGQyMjY=
12
+ data.tar.gz: !binary |-
13
+ YzljMGJiNzE0OWQyMzYxMDM5NThlZjI1Y2U4Mzg0ZWM2YTc2MjY1NGQ0ZDlk
14
+ YTc1ODRjOGYxOTRiZTQ1ZTQ2MTQyYzlmODNlNDliODFkNjU2MmM5ZGFlODg4
15
+ ZWYwODMyMDMyNjkxYzUwOTc5ZjViOWMxZDI0NzRlNDMwYTJiZjU=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in logging_factory.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nayyara Samuel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # LoggingFactory
2
+
3
+ A wrapper around the [`logging` gem](https://github.com/TwP/logging) that makes instantiating logs easier. Also has a number of presets for file and standard output logging.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'logging_factory'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install logging_factory
18
+
19
+ ## Usage
20
+ By default all output is written to standard output. To use in your project:
21
+
22
+ public class MyClass
23
+ def initialize()
24
+ log = LoggingFactory::DEFAULT_FACTORY.log(self.class)
25
+ end
26
+ end
27
+
28
+ This will instantiate a log with the default configuration, specified by `LoggingFactory::DEFAULT_LOG_CONFIGURATION`:
29
+
30
+ {
31
+ output: 'STDOUT',
32
+ truncate: false,
33
+ level: :debug,
34
+ format: '[%d] %-5l [%c]: %m\n'
35
+ }
36
+
37
+ To override this configuration you can modify a log instance like this:
38
+
39
+ public class MyClass
40
+ def initialize()
41
+ log = LoggingFactory::DEFAULT_FACTORY.log(self.class, output: 'myproject.log')
42
+ end
43
+ end
44
+
45
+ Or create a new factory instant for use in your project:
46
+
47
+
48
+ MY_LOG_FACTORY = LoggingFactory.create (
49
+ output: 'myproject.log'
50
+ )
51
+
52
+
53
+ This will create an factory that will use the `myproject.log` for logging. Other options which are no supplied will fall back to the default unless explicitly overridden.
54
+
55
+
56
+ ## Examples
57
+ See `test/log_example.rb` for an example.
58
+
59
+ Standard output:
60
+
61
+ <img src='https://github.va.opower.it/opower/logging_factory/blob/master/img/stdout.png?raw=true' height='130'>
62
+
63
+ Log file:
64
+
65
+ [2013-09-19 23:12:18] DEBUG [LogExample] This is a debug message
66
+ [2013-09-19 23:12:18] INFO [LogExample] This is an informational message
67
+ [2013-09-19 23:12:18] WARN [LogExample] This is an warning message
68
+ [2013-09-19 23:12:18] ERROR [LogExample] This is an error message
69
+ [2013-09-19 23:12:18] FATAL [LogExample] This is a fatal message
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ require 'opower/nexus/deploy_task'
data/img/stdout.png ADDED
Binary file
@@ -0,0 +1,108 @@
1
+ require 'version'
2
+ require 'preconditions'
3
+ require 'logging'
4
+
5
+ #
6
+ # This class that contains code for initializing logs.
7
+ # @author: Nayyara Samuel (mailto: nayyara.samuel@opower.com)
8
+ #
9
+ # Patch so that certain checks can be gotten on a class using `include Preconditions`
10
+ module Preconditions
11
+ def self.included(receiver)
12
+ receiver.send :include, PreconditionMixinMethods
13
+ receiver.send :include, PreconditionModuleClassMethods
14
+ end
15
+ end
16
+
17
+ # Logging factory for logs
18
+ module LoggingFactory
19
+
20
+ DEFAULT_LOG_CONFIGURATION = {
21
+ output: 'STDOUT',
22
+ truncate: false,
23
+ level: :debug,
24
+ format: '[%d] %-5l [%c] %m\n'
25
+ }
26
+
27
+ # Create a new logging factory
28
+ def self.create(*params)
29
+ Logger.new(*params)
30
+ end
31
+
32
+ # Main logger class
33
+ class Logger
34
+ include Preconditions
35
+
36
+ attr_reader :log_configuration
37
+
38
+ # Initializes this logger instance with teh format specified
39
+ def initialize(config={})
40
+ @log_configuration = DEFAULT_LOG_CONFIGURATION.merge(config)
41
+ end
42
+
43
+ # Returns a new log for the calling class with the supplied configuration
44
+ def log(caller, config={})
45
+ log_configuration = @log_configuration.merge(config)
46
+ log = Logging.logger[caller]
47
+ add_appenders(log, log_configuration)
48
+ log
49
+ end
50
+
51
+ private
52
+ def add_appenders(log, log_configuration)
53
+ destination = log_configuration[:output]
54
+ truncate = log_configuration[:truncate]
55
+ level = log_configuration[:level].to_sym
56
+ fmt = log_configuration[:format]
57
+
58
+ if (destination == 'STDOUT')
59
+ append_stdout_format(log, fmt)
60
+ else
61
+ append_file_format(log, destination, fmt, truncate)
62
+ end
63
+
64
+ valid_levels = [:debug, :error, :fatal, :warn, :info]
65
+ check(level) do
66
+ satisfies("Logging level must be one of {#{valid_levels.join(',')}}") do
67
+ valid_levels.include?(level)
68
+ end
69
+ end
70
+ log.level = level
71
+ log
72
+ end
73
+
74
+ def append_file_format(log, destination, fmt, truncate=false)
75
+ Logging.appenders.file(destination,
76
+ {
77
+ file_name: destination,
78
+ truncate: truncate ? true : false,
79
+ layout: Logging.layouts.pattern(pattern: fmt)
80
+ })
81
+ log.add_appenders(destination)
82
+ end
83
+
84
+ def append_stdout_format(log, fmt)
85
+ Logging.color_scheme('bright',
86
+ levels: {
87
+ debug: :magenta,
88
+ info: :green,
89
+ warn: :yellow,
90
+ error: :red, fatal: [:white, :on_red]
91
+ },
92
+ date: :blue, logger: :cyan)
93
+ Logging.appenders.stdout(
94
+ 'stdout',
95
+ layout: Logging.layouts.pattern(
96
+ pattern: fmt,
97
+ color_scheme: 'bright'))
98
+ log.add_appenders 'stdout'
99
+ end
100
+ end
101
+
102
+ DEFAULT_FACTORY = LoggingFactory.create
103
+ end
104
+
105
+
106
+
107
+
108
+
data/lib/version.rb ADDED
@@ -0,0 +1,4 @@
1
+ module LoggingFactory
2
+ VERSION = '0.0.1'
3
+ end
4
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'logging_factory'
8
+ gem.version = LoggingFactory::VERSION
9
+ gem.authors = ['Nayyara Samuel']
10
+ gem.email = ['nayyara.samuel@opower.com']
11
+ gem.description = %q{A wrapper around logging gem}
12
+ gem.summary = %q{A wrapper around logging gem}
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(spec|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency 'logging'
20
+ gem.add_dependency 'preconditions'
21
+ end
@@ -0,0 +1,20 @@
1
+ require_relative './lib/logging_factory'
2
+
3
+
4
+ describe LoggingFactory::Logger do
5
+
6
+ let(:log) { LoggingFactory::DEFAULT_FACTORY.log('LogExample') }
7
+ let(:debug_message) { 'This is a debug message' }
8
+ let(:info_message) { 'This is an info message' }
9
+ let(:warn_message) { 'This is a warn message' }
10
+ let(:error_message) { 'This is an error message' }
11
+ let(:fatal_message) { 'This is a fatal message' }
12
+
13
+ context 'stdout' do
14
+ debug_output = capture_stdout { log.debug(debug_message) }
15
+ puts debug_output
16
+ end
17
+
18
+ end
19
+
20
+
@@ -0,0 +1,20 @@
1
+ require 'rspec'
2
+
3
+
4
+ # To capture stdout contents
5
+ module StdOutCapture
6
+ def capture_stdout(&block)
7
+ original_stdout = $stdout
8
+ $stdout = fake = StringIO.new
9
+ begin
10
+ yield
11
+ ensure
12
+ $stdout = original_stdout
13
+ end
14
+ fake.string
15
+ end
16
+ end
17
+
18
+ RSpec.configure do |config|
19
+ config.include StdOutCapture
20
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logging_factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nayyara Samuel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logging
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: preconditions
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A wrapper around logging gem
42
+ email:
43
+ - nayyara.samuel@opower.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - img/stdout.png
54
+ - lib/logging_factory.rb
55
+ - lib/version.rb
56
+ - logging_factory.gemspec
57
+ - spec/log_stdout_spec.rb
58
+ - spec/spec_helper.rb
59
+ homepage:
60
+ licenses: []
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: A wrapper around logging gem
82
+ test_files:
83
+ - spec/log_stdout_spec.rb
84
+ - spec/spec_helper.rb
85
+ has_rdoc: