cute_logger 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7bb49f4e102b7d0827d84b93296911db889deabb
4
+ data.tar.gz: 9c5c17882b1d330543ca825de39e016ad4bb19af
5
+ SHA512:
6
+ metadata.gz: e89de94763e3843f36119cfa5b18af5ab4a7b98a36a794eeb2a109a1b934e8ee31e0afd32553f9f4ba74e59316227df842437ca13c50134af508ce93f1630197
7
+ data.tar.gz: d5f125d22a65492b02e24179c9749b4e87ac52e0745bef2e5e4e6f13392ed949822eeff1c8bc6a51cfedbb6bf3d96d7c277e7795ae5cf8190b42adee0ff5001f
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cute_logger.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 newint33h
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Cute Logger
2
+
3
+ This gem provides methods to log events in an easy way and accessible from anywhere.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cute_logger'
11
+ ```
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install cute_logger
16
+
17
+ ## Usage
18
+
19
+ To use this gem, require it into your application and optionally call the setup function to specify how the information should be stored.
20
+
21
+ ```
22
+ CuteLogger.setup(
23
+ filename: 'application.log',
24
+ severity: 'INFO',
25
+ shift_age: 7, # 7 days
26
+ shift_size: 1024 * 1024 * 1024 # On gigabyte
27
+ )
28
+ ```
29
+
30
+ To log something you could use the following formats:
31
+
32
+ ```
33
+ log_info('Some text')
34
+ log_info(status: 'Working', value: '123')
35
+ log_debug(my_hash)
36
+ log_debug { 'Delayed evaluation' }
37
+ log_info('MyAppName') { 'Something to log' }
38
+ log_error('Error X', my_exception)
39
+ ```
40
+
41
+ There is an utility script that helps to visualize the contents of the log. For example:
42
+
43
+ ```
44
+ $ cat application.log | cute_log
45
+ $ tail -F application.log | cute_log
46
+ $ tail -F application.log | cute_log --json
47
+ $ grep "ERROR" application.log | cute_log --awesome
48
+ ```
49
+
50
+ ## Development
51
+
52
+ Run `rake test` to run the tests.
53
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/bin/cute_log ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+ require 'colorize'
3
+ require 'json'
4
+ require 'awesome_print'
5
+ require 'cute_logger'
6
+
7
+ awesome = true
8
+
9
+ until ARGV.empty?
10
+ option = ARGV.shift
11
+ case option
12
+ when '--awesome', '-a'
13
+ awesome = true
14
+ when '--json', '-j'
15
+ awesome = false
16
+ when '--help', '-h'
17
+ $stderr.puts 'CuteLogger - cute_log'
18
+ $stderr.puts 'Usage: cat application.log | cute_log [-j|--json|-a|--awesome]'
19
+ $stderr.puts ' -j, --json Prints the log using JSON pretty print'
20
+ $stderr.puts ' -a, --awesome Prints the log using Awesome print'
21
+ exit
22
+ else
23
+ $stderr.puts "Invalid option: #{option}"
24
+ exit(-1)
25
+ end
26
+ end
27
+
28
+ def format_severity(severity)
29
+ severity.magenta if severity == 'DEBUG'
30
+ severity.white if severity == 'INFO'
31
+ severity.light_yellow if severity == 'WARN'
32
+ severity.light_red
33
+ end
34
+
35
+ ARGF.each do |line|
36
+ matches = line.to_utf8.match(/^(.*?),(.*?),(.*?),(.*?),(.*?),(.+)$/)
37
+ if matches
38
+ print "#{matches[1]}".white
39
+ print " #{format_severity(matches[2])}"
40
+ print " #{matches[3].to_i(16)}-#{matches[4]}".white
41
+ print " (#{matches[5]})".blue
42
+ print "\n"
43
+ begin
44
+ json_data = JSON.parse("[#{matches[6]}]").first
45
+ if json_data.is_a?(String)
46
+ puts json_data.yellow
47
+ elsif awesome
48
+ ap(json_data)
49
+ else
50
+ puts JSON.pretty_generate(json_data).yellow
51
+ end
52
+ rescue
53
+ puts matches[6].to_s.red
54
+ end
55
+ else
56
+ puts line.red
57
+ end
58
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cute_logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cute_logger'
8
+ spec.version = CuteLogger::VERSION
9
+ spec.authors = ['Jorge Del Rio']
10
+ spec.email = ['jdelrios@gmail.com']
11
+
12
+ spec.summary = 'Gem to simplify and centralize the logging process'
13
+ spec.description = 'This gem provides methods to log events to an unique place'
14
+ spec.homepage = 'https://github.com/newint33h/cute_logger'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'awesome_print', '~> 1'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.10'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'minitest', '~> 5'
27
+ end
@@ -0,0 +1,114 @@
1
+ require 'cute_logger/version'
2
+ require 'logger'
3
+ require 'json'
4
+ require 'awesome_print'
5
+ require_relative 'utf8_enforcer'
6
+
7
+ # This module defines the functionality the the CuteLogger gem
8
+ module CuteLogger
9
+ SEVERITY = {
10
+ 'DEBUG' => Logger::DEBUG,
11
+ 'INFO' => Logger::INFO,
12
+ 'WARN' => Logger::WARN,
13
+ 'ERROR' => Logger::ERROR,
14
+ 'FATAL' => Logger::FATAL
15
+ }
16
+
17
+ def self.setup(settings = {})
18
+ @logger = Logger.new(
19
+ settings[:filename] || 'application.log',
20
+ settings[:shift_age] || 7,
21
+ settings[:shift_size] || 1024 * 1024 * 1024 # One gigabyte
22
+ )
23
+ @logger.sev_threshold = severity(settings[:severity])
24
+ @logger.datetime_format = '%Y-%m-%d %H:%M:%S'
25
+ @logger.formatter = proc do |severity, datetime, progname, msg|
26
+ "#{datetime},#{severity},#{Process.pid.to_s(16)},#{Thread.current.object_id.to_s(16)}" \
27
+ ",#{progname},#{msg}\n"
28
+ end
29
+ end
30
+
31
+ def self.severity=(text)
32
+ @logger.sev_threshold = severity(text)
33
+ end
34
+
35
+ def self.severity(text)
36
+ return Logger::INFO unless text
37
+ fail("Unknown logger severity: #{text}") unless SEVERITY[text.upcase]
38
+ SEVERITY[text.upcase]
39
+ end
40
+
41
+ def self.format_message(message)
42
+ if message.is_a?(Array) && message.count == 1
43
+ message.first.to_log_format.to_json
44
+ else
45
+ message.to_log_format.to_json
46
+ end
47
+ end
48
+
49
+ def self.log(severity, classname, args, &block)
50
+ setup unless defined?(@logger)
51
+ if block_given?
52
+ @logger.add(severity, nil, (args.first || classname)) { format_message(block.call) }
53
+ else
54
+ @logger.add(severity, format_message(args), classname)
55
+ end
56
+ end
57
+
58
+ def self.logger
59
+ @logger
60
+ end
61
+
62
+ # Methods to be included as part of the Object class
63
+ module GeneralMethods
64
+ def log_debug(*args, &block)
65
+ CuteLogger.log(Logger::DEBUG, self.class, args, &block)
66
+ end
67
+
68
+ def log_info(*args, &block)
69
+ CuteLogger.log(Logger::INFO, self.class, args, &block)
70
+ end
71
+
72
+ def log_warn(*args, &block)
73
+ CuteLogger.log(Logger::WARN, self.class, args, &block)
74
+ end
75
+
76
+ def log_error(*args, &block)
77
+ CuteLogger.log(Logger::ERROR, self.class, args, &block)
78
+ end
79
+
80
+ def log_fatal(*args, &block)
81
+ CuteLogger.log(Logger::FATAL, self.class, args, &block)
82
+ end
83
+
84
+ def to_log_format
85
+ to_s.to_utf8
86
+ end
87
+ end
88
+ end
89
+
90
+ # Add global logging methods to all scopes
91
+ class Object
92
+ include CuteLogger::GeneralMethods
93
+ end
94
+
95
+ # Specify and special way of logging exceptions
96
+ class Exception
97
+ def to_log_format
98
+ { class: self.class, message: message, backtrace: backtrace }
99
+ end
100
+ end
101
+
102
+ # Specify that the arrays must traverse all the children to format the log
103
+ class Array
104
+ def to_log_format
105
+ map(&:to_log_format)
106
+ end
107
+ end
108
+
109
+ # Specify that the hash must traverse all the children to format the log
110
+ class Hash
111
+ def to_log_format
112
+ Hash[map { |key, value| [key.to_log_format, value.to_log_format] }]
113
+ end
114
+ end
@@ -0,0 +1,5 @@
1
+
2
+ # Partial module to define the version of the gem
3
+ module CuteLogger
4
+ VERSION = '0.1.5'
5
+ end
@@ -0,0 +1,27 @@
1
+ # String extensions to convert any encoding to utf8
2
+ class String
3
+ def try_convert_from_encoding_to_utf8!(encoding)
4
+ original_encoding = self.encoding
5
+ begin
6
+ force_encoding(encoding).encode!(Encoding::UTF_8)
7
+ true
8
+ rescue
9
+ force_encoding(original_encoding)
10
+ false
11
+ end
12
+ end
13
+
14
+ def to_utf8!
15
+ if force_encoding(Encoding::UTF_8).valid_encoding?
16
+ return encode!(Encoding::UTF_8, invalid: :replace, replace: '?')
17
+ end
18
+ return self if try_convert_from_encoding_to_utf8!(Encoding::ISO_8859_1)
19
+ return self if try_convert_from_encoding_to_utf8!(Encoding::Windows_1252)
20
+ return self if try_convert_from_encoding_to_utf8!(Encoding::Windows_1252)
21
+ encode!(Encoding::UTF_8, invalid: :replace, replace: '?')
22
+ end
23
+
24
+ def to_utf8
25
+ dup.to_utf8!
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cute_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Jorge Del Rio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: awesome_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5'
69
+ description: This gem provides methods to log events to an unique place
70
+ email:
71
+ - jdelrios@gmail.com
72
+ executables:
73
+ - cute_log
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - bin/cute_log
82
+ - cute_logger.gemspec
83
+ - lib/cute_logger.rb
84
+ - lib/cute_logger/version.rb
85
+ - lib/utf8_enforcer.rb
86
+ homepage: https://github.com/newint33h/cute_logger
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.5.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Gem to simplify and centralize the logging process
110
+ test_files: []