fiva 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
+ SHA256:
3
+ metadata.gz: 457621ec4d3b3fa14f01dabce896377e76fde859e171ce471d64b65ca2ed4b53
4
+ data.tar.gz: c692ac9532b0d5e28158c6be38618a8166319d6af39676e93034857065165b59
5
+ SHA512:
6
+ metadata.gz: 9857ec3b63ba1de23b9ec6cd6709e875f83da139d2c90aaa98271a6956345c370ac485c5d9ff30a17d769d944ae535ea8524fe3b0ae8bd323ec05555f762d13d
7
+ data.tar.gz: 630eaa295e23b016cd9f10fc4c43e9e6a0c06ab0d4546633b07cb6407c2e77d9134be622a0ca5d2609d08d3651f70c4136b52554f233a343cd470d7c4dd2c2d6
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fiva.gemspec
6
+ gemspec
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fiva (0.1.0)
5
+ pastel (~> 0.7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ equatable (0.5.0)
11
+ pastel (0.7.2)
12
+ equatable (~> 0.5.0)
13
+ tty-color (~> 0.4.0)
14
+ rake (10.5.0)
15
+ tty-color (0.4.3)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.17)
22
+ fiva!
23
+ rake (~> 10.0)
24
+
25
+ BUNDLED WITH
26
+ 1.17.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Nickolay Ilyushin
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,50 @@
1
+ # Fiva
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'fiva'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fiva
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'fiva'
23
+ require 'json'
24
+
25
+ Fiva::Logger.new
26
+ .add_handler(Fiva::puts_handler(level: Fiva::DEBUG))
27
+ .add_handler(Fiva::putsr_handler(level: Fiva::DEBUG, func: ->(msg) { msg.to_json }))
28
+ .write(Fiva::DEBUG, 'Hello!', foo: 'bar')
29
+ .debug('Debug', tv: Fiva::TextValue.new('Foo'))
30
+ .io('A message', text: 'Hello, World!', payload: "foo\n asdf")
31
+ .info('Info')
32
+ .important('Important')
33
+ .warning('Warning')
34
+ .error('Error')
35
+ .fatal('Fatal')
36
+ ```
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/handicraftsman/fiva.
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fiva"
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,30 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fiva/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fiva'
8
+ spec.version = Fiva::VERSION
9
+ spec.authors = ['Nickolay (handicraftsman) Ilyushin']
10
+ spec.email = ['nickolay02@inbox.ru']
11
+
12
+ spec.summary = 'A structured logging implementation for Ruby'
13
+ spec.description = 'A structured logging implementation for Ruby'
14
+ spec.homepage = 'https://github.com/handicraftsman/fiva'
15
+ spec.license = 'MIT'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.17'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+
29
+ spec.add_dependency 'pastel', '~> 0.7'
30
+ end
@@ -0,0 +1,25 @@
1
+ require 'date'
2
+
3
+ require 'pastel'
4
+
5
+ require 'fiva/version'
6
+
7
+ module Fiva
8
+ class Error < StandardError; end
9
+
10
+ FATAL = 6
11
+ ERROR = 5
12
+ WARNING = 4
13
+ IMPORTANT = 3
14
+ INFO = 2
15
+ IO = 1
16
+ DEBUG = 0
17
+
18
+ LEVELS = ['DBG', ' IO', 'INF', 'IMP', 'WRN', 'ERR', 'FTL']
19
+ FULL_LEVELS = ['DEBUG', 'IO', 'INFO', 'IMPORTANT', 'WARNING', 'ERROR', 'FATAL']
20
+ end
21
+
22
+ require 'fiva/text_value'
23
+ require 'fiva/message'
24
+ require 'fiva/handlers'
25
+ require 'fiva/logger'
@@ -0,0 +1,65 @@
1
+ module Fiva
2
+ PLE = ['data', 'text', 'body', 'info', 'payload']
3
+
4
+ def self.puts_handler(**opts)
5
+ level = opts.fetch :level, Fiva::INFO
6
+ padding = opts.fetch :padding, 32
7
+ ple = opts.fetch :ple, Fiva::PLE
8
+ target = opts.fetch :target, STDERR
9
+ colored = opts.fetch :colored, true
10
+
11
+ pastel = Pastel.new
12
+
13
+ lc = [
14
+ pastel.green.detach,
15
+ pastel.cyan.detach,
16
+ pastel.blue.detach,
17
+ pastel.white.detach,
18
+ pastel.yellow.detach,
19
+ pastel.red.detach,
20
+ pastel.magenta.detach
21
+ ]
22
+
23
+ gl = ->(l) {
24
+ return (Fiva::LEVELS[l] || "UNK") unless colored
25
+ (lc[l] || pastel.clear.detach).(Fiva::LEVELS[l] || "UNK")
26
+ }
27
+
28
+ cl = ->(l, t) {
29
+ return t unless colored
30
+ (lc[l] || pastel.clear.detach).(t)
31
+ }
32
+
33
+ return ->(logger, msg) {
34
+ return if msg.level < level
35
+
36
+ data = msg.data
37
+ .filter { |k, v| not Fiva::PLE.include? k.to_s }
38
+ .map { |k, v| "#{cl.(msg.level, k)}: #{v.inspect}" }
39
+ .join(' ')
40
+
41
+ str = "#{msg.timestamp.strftime} " +
42
+ "#{gl.(msg.level)} " +
43
+ "#{"%-#{padding}s" % msg.msg.to_s} " +
44
+ "#{data}"
45
+
46
+ msg.data
47
+ .filter { |k, v| Fiva::PLE.include? k.to_s }
48
+ .each_pair { |k, v| str << "\n#{cl.(msg.level, k)}:\n #{v.to_s.gsub(/\n/, "\n ")}" }
49
+
50
+ target.puts(str)
51
+ }
52
+ end
53
+
54
+ def self.putsr_handler(**opts)
55
+ level = opts.fetch :level, Fiva::INFO
56
+ target = opts.fetch :target, STDERR
57
+ func = opts.fetch :func, ->(msg) { msg.msg.to_s }
58
+
59
+ return ->(logger, msg) {
60
+ return if msg.level < level
61
+
62
+ target.puts func.(msg)
63
+ }
64
+ end
65
+ end
@@ -0,0 +1,59 @@
1
+ module Fiva
2
+ class Logger
3
+ def initialize
4
+ @handlers = []
5
+ end
6
+
7
+ def add_handler(h)
8
+ @handlers << h
9
+ self
10
+ end
11
+
12
+ def write_m(msg)
13
+ @handlers.each do |h|
14
+ h.(self, msg)
15
+ end
16
+ self
17
+ end
18
+
19
+ def write(level, msg, **data)
20
+ self.write_m(Message.new(level, msg, **data))
21
+ self
22
+ end
23
+
24
+ def debug(msg, **data)
25
+ self.write(Fiva::DEBUG, msg, **data)
26
+ self
27
+ end
28
+
29
+ def io(msg, **data)
30
+ self.write(Fiva::IO, msg, **data)
31
+ self
32
+ end
33
+
34
+ def info(msg, **data)
35
+ self.write(Fiva::INFO, msg, **data)
36
+ self
37
+ end
38
+
39
+ def important(msg, **data)
40
+ self.write(Fiva::IMPORTANT, msg, **data)
41
+ self
42
+ end
43
+
44
+ def warning(msg, **data)
45
+ self.write(Fiva::WARNING, msg, **data)
46
+ self
47
+ end
48
+
49
+ def error(msg, **data)
50
+ self.write(Fiva::ERROR, msg, **data)
51
+ self
52
+ end
53
+
54
+ def fatal(msg, **data)
55
+ self.write(Fiva::FATAL, msg, **data)
56
+ self
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,25 @@
1
+ module Fiva
2
+ class Message
3
+ attr_accessor :timestamp, :level, :msg, :data
4
+
5
+ def initialize(level, msg, **data)
6
+ @timestamp = DateTime.now
7
+ @level = level
8
+ @msg = msg
9
+ @data = data
10
+ end
11
+
12
+ def to_hash
13
+ {
14
+ timestamp: @timestamp,
15
+ level: @level,
16
+ msg: @msg,
17
+ data: @data
18
+ }
19
+ end
20
+
21
+ def to_json
22
+ self.to_hash.to_json
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module Fiva
2
+ class TextValue < String
3
+ def inspect
4
+ self.to_s
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Fiva
2
+ VERSION = "0.1.0"
3
+ end
data/test.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'fiva'
2
+ require 'json'
3
+
4
+ Fiva::Logger.new
5
+ .add_handler(Fiva::puts_handler(level: Fiva::DEBUG))
6
+ .add_handler(Fiva::putsr_handler(level: Fiva::DEBUG, func: ->(msg) { msg.to_json }))
7
+ .write(Fiva::DEBUG, 'Hello!', foo: 'bar')
8
+ .debug('Debug', tv: Fiva::TextValue.new('Foo'))
9
+ .io('A message', text: 'Hello, World!', payload: "foo\n asdf")
10
+ .info('Info')
11
+ .important('Important')
12
+ .warning('Warning')
13
+ .error('Error')
14
+ .fatal('Fatal')
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fiva
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nickolay (handicraftsman) Ilyushin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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: pastel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ description: A structured logging implementation for Ruby
56
+ email:
57
+ - nickolay02@inbox.ru
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - fiva.gemspec
71
+ - lib/fiva.rb
72
+ - lib/fiva/handlers.rb
73
+ - lib/fiva/logger.rb
74
+ - lib/fiva/message.rb
75
+ - lib/fiva/text_value.rb
76
+ - lib/fiva/version.rb
77
+ - test.rb
78
+ homepage: https://github.com/handicraftsman/fiva
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A structured logging implementation for Ruby
101
+ test_files: []