flnt 0.1.0

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: 9598b2d9ec5edeb6b7bb130e680583098930677d
4
+ data.tar.gz: 63dbc73fe1f99ce3e4727d43932828f0a7431e5a
5
+ SHA512:
6
+ metadata.gz: c3af8c77dfd7b650dcf05c5c09e71f3d5d996e9d7cbd520129ea21e51b04aae9d52311c0743d108d900b53a917671b7b27bba2a7953745f87ba069fbecd1e0c7
7
+ data.tar.gz: b5e50e8c86511c7cdc39a2be85f7e6a5c6580215403e57ce5b412e5185cc8e2ac62b135bd4f655d7d08107e89c9f11b6b9183ec46f0e2af5185eb01429a98385
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flnt.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Uchio KONDO
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,62 @@
1
+ # Flnt
2
+
3
+ Gentle post-to-fluentd log solution.
4
+
5
+ ## Powered by wercker
6
+
7
+ [![wercker status](https://app.wercker.com/status/4c5b18d2fe3debeee48b7f0fab81eb12/m/ "wercker status")](https://app.wercker.com/project/bykey/4c5b18d2fe3debeee48b7f0fab81eb12)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'flnt'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install flnt
22
+
23
+ ## Usage
24
+
25
+ ### Just post log to fluent
26
+
27
+ * Setting up fluentd on your localhost:24224
28
+ * Call the methods below:
29
+ ```ruby
30
+ Flnt.app.payment.info "Payment done"
31
+ ```
32
+ * Then, your fluentd receives json `{message: "Payment done"}` with tag `app.payment.info`
33
+
34
+ `Flnt` compiles method chaining into a tag, and when the method is called with an argument,
35
+ it emits the information to fluentd.
36
+
37
+ NOTE: You cannot use methods such as `foo?` or `foo!` for tag suffix. only `/[a-zA-Z0-9_]/` are OK.
38
+
39
+ ### Configuration of fluentd
40
+
41
+ ```ruby
42
+ Flnt::Configuration.configure do |c|
43
+ c.prefix = "foobar"
44
+ c.host = 'fluentd.example.jp'
45
+ c.port = 12345
46
+ end
47
+ ```
48
+
49
+ ## License
50
+
51
+ see `LICENSE.txt`.
52
+
53
+
54
+ ## Contributing
55
+
56
+ The usual github way.
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.pattern = "spec/**/*_spec.rb"
6
+ end
data/flnt.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'flnt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flnt"
8
+ spec.version = Flnt::VERSION
9
+ spec.authors = ["Uchio KONDO"]
10
+ spec.email = ["udzura@udzura.jp"]
11
+ spec.description = %q{Gentle post-to-fluentd log solution}
12
+ spec.summary = %q{Gentle post-to-fluentd log solution}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "fluent-logger"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "pry"
26
+
27
+ spec.add_development_dependency "rspec"
28
+ end
@@ -0,0 +1,33 @@
1
+ module Flnt
2
+ module Configuration
3
+ class << self
4
+ def host
5
+ @host ||= 'localhost'
6
+ end
7
+
8
+ def port
9
+ @port ||= 24224
10
+ end
11
+ attr_writer :host, :port
12
+ attr_accessor :prefix
13
+
14
+ def to_fluentd_config
15
+ [
16
+ prefix,
17
+ {
18
+ host: host,
19
+ port: port,
20
+ }
21
+ ]
22
+ end
23
+
24
+ def configure &b
25
+ b.call(self)
26
+ end
27
+ end
28
+ end
29
+
30
+ def self.Configuration
31
+ Configuration.to_fluentd_config
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ require 'fluent/logger'
2
+
3
+ module Flnt
4
+ class Logger < BasicObject
5
+ def initialize(init_tag)
6
+ @tag = init_tag
7
+ end
8
+
9
+ def method_missing(name, *args)
10
+ return super if name.to_s =~ /(!|\?)$/
11
+
12
+ @tag = [@tag, name.to_s].join('.')
13
+ unless args.empty?
14
+ emit! args.first
15
+ end
16
+
17
+ return self
18
+ end
19
+
20
+ def emit!(arg)
21
+ info = {}
22
+ case arg
23
+ when ::Hash
24
+ info.merge! arg
25
+ when ::String
26
+ info[:message] = arg
27
+ when ::Exception
28
+ info[:error_class] = arg.class
29
+ info[:message] = arg.message
30
+ info[:backtrace] = arg.backtrace if arg.backtrace
31
+ else
32
+ info[:info] = arg
33
+ end
34
+ ::Fluent::Logger.post @tag, info
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Flnt
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,3 @@
1
+ module Flnt
2
+ VERSION = "0.1.0"
3
+ end
data/lib/flnt.rb ADDED
@@ -0,0 +1,35 @@
1
+ require "flnt/version"
2
+ require "flnt/logger"
3
+ require "flnt/configuration"
4
+
5
+ module Flnt
6
+ class << self
7
+ # Flnt should be a Module because she is also a namespace,
8
+ # but should behaves like a BasicObject
9
+ ((Module.instance_methods + Module.private_instance_methods) -
10
+ (BasicObject.instance_methods + BasicObject.private_instance_methods)).
11
+ delete_if {|method_name| method_name.to_s =~ /([\-\[\]\/~=+*&|%<>!?])/}.
12
+ each do |target_method|
13
+ if target_method.to_sym != :object_id
14
+ eval %Q(undef #{target_method})
15
+ end
16
+ end
17
+
18
+ def method_missing(name, *args)
19
+ return super if name.to_s =~ /(!|\?)$/
20
+ initialize!
21
+ Flnt::Logger.new(name.to_s)
22
+ end
23
+
24
+ def initialized?
25
+ !! @initialized
26
+ end
27
+
28
+ def initialize!
29
+ unless initialized?
30
+ @initialized = true
31
+ Fluent::Logger::FluentLogger.open(*Flnt::Configuration())
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe "Flnt::Configuration" do
4
+ it "should have default values" do
5
+ expect(Flnt::Configuration()).to eq(
6
+ [
7
+ nil,
8
+ {
9
+ host: 'localhost',
10
+ port: 24224,
11
+ }
12
+ ]
13
+ )
14
+ end
15
+
16
+ it "should be customized" do
17
+ Flnt::Configuration.configure do |c|
18
+ c.prefix = "foobar"
19
+ c.host = 'fluentd.example.jp'
20
+ c.port = 12345
21
+ end
22
+
23
+ expect(Flnt::Configuration()).to eq(
24
+ [
25
+ "foobar",
26
+ {
27
+ host: 'fluentd.example.jp',
28
+ port: 12345,
29
+ }
30
+ ]
31
+ )
32
+ end
33
+
34
+ after do
35
+ Flnt::Configuration.instance_eval do
36
+ @host = nil
37
+ @port = nil
38
+ @prefix = nil
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,62 @@
1
+ require "spec_helper"
2
+
3
+ describe "Flnt::Logger" do
4
+ before do
5
+ allow(Fluent::Logger::FluentLogger).to receive(:open).with(nil, an_instance_of(Hash))
6
+ end
7
+
8
+ def tag_of(flnt_logger)
9
+ return flnt_logger.instance_eval { @tag }
10
+ end
11
+
12
+ it "should chain the method call appending a tag" do
13
+ expect(tag_of Flnt.sample.foo).to eq "sample.foo"
14
+ expect(tag_of Flnt._sample._foo2).to eq "_sample._foo2"
15
+ expect(tag_of Flnt.sample.foo.bar).to eq "sample.foo.bar"
16
+ end
17
+
18
+ it "should raise when chained with ! or ? methods" do
19
+ expect {
20
+ Flnt.sample.warn!
21
+ }.to raise_error NoMethodError
22
+
23
+ expect {
24
+ Flnt.sample.info?
25
+ }.to raise_error NoMethodError
26
+ end
27
+
28
+ describe "should send messages when called with arg" do
29
+ context "when String" do
30
+ specify do
31
+ expect(Fluent::Logger).to receive(:post).with("sample.info", {message: "Hello Info"})
32
+ Flnt.sample.info "Hello Info"
33
+ end
34
+ end
35
+
36
+ context "when Hash" do
37
+ specify do
38
+ expect(Fluent::Logger).to receive(:post).with("sample.info", {info1: "Info", info2: 1234})
39
+ Flnt.sample.info({info1: "Info", info2: 1234})
40
+ end
41
+ end
42
+
43
+ context "when Error" do
44
+ let(:error) { StandardError.new "Error Info" }
45
+
46
+ specify do
47
+ expect(Fluent::Logger).to receive(:post)
48
+ .with("sample.info", {error_class: StandardError, message: "Error Info"})
49
+ Flnt.sample.info(error)
50
+ end
51
+ end
52
+
53
+ context "else" do
54
+ let(:obj) { Object.new }
55
+
56
+ specify do
57
+ expect(Fluent::Logger).to receive(:post).with("sample.info", {info: obj})
58
+ Flnt.sample.info(obj)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe "Flnt" do
4
+ it "should be a Module" do
5
+ expect(Flnt).to be_a_kind_of Module
6
+ end
7
+
8
+ it "should initialize fluentd connection" do
9
+ allow(Flnt).to receive(:initialized?).and_return(false)
10
+
11
+ expect(Fluent::Logger::FluentLogger).to receive(:open).with(*Flnt::Configuration())
12
+ Flnt.init_foo
13
+ end
14
+
15
+ it "should initialize fluentd connection just once" do
16
+ allow(Flnt).to receive(:initialized?).and_return(true)
17
+
18
+ expect(Fluent::Logger::FluentLogger).not_to receive(:open)
19
+ expect(Flnt).not_to receive(:Configuration)
20
+ Flnt.init_foo
21
+ end
22
+
23
+ it "should return Flnt::Logger tagged with called method name" do
24
+ ret = Flnt.init_foo
25
+ expect(ret.instance_eval { @tag }).to eq "init_foo"
26
+
27
+ expect { ret.chain_bar }.not_to raise_error
28
+ expect { ret.respond_to? }.to raise_error NoMethodError
29
+ end
30
+
31
+ it "should create a new logger for each call" do
32
+ logger1 = Flnt.foo_tag
33
+ logger2 = Flnt.foo_tag
34
+ expect(logger1.__id__).not_to eq logger2.__id__
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'flnt'
8
+
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus if ENV['FOCUS']
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
data/wercker.yml ADDED
@@ -0,0 +1,23 @@
1
+ box: wercker/rvm
2
+ build:
3
+ steps:
4
+ - rvm-use:
5
+ version: ruby-2.1.0
6
+ - bundle-install
7
+ - script:
8
+ name: run rspec
9
+ code: bundle exec rake spec
10
+
11
+ - rvm-use:
12
+ version: ruby-2.0.0-p353
13
+ - bundle-install
14
+ - script:
15
+ name: run rspec
16
+ code: bundle exec rake spec
17
+
18
+ - rvm-use:
19
+ version: ruby-1.9.3-p545
20
+ - bundle-install
21
+ - script:
22
+ name: run rspec
23
+ code: bundle exec rake spec
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flnt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Uchio KONDO
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluent-logger
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: pry
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: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Gentle post-to-fluentd log solution
84
+ email:
85
+ - udzura@udzura.jp
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - flnt.gemspec
97
+ - lib/flnt.rb
98
+ - lib/flnt/configuration.rb
99
+ - lib/flnt/logger.rb
100
+ - lib/flnt/version.rb
101
+ - lib/flnt/version_flymake.rb
102
+ - spec/lib/flnt/configuration_spec.rb
103
+ - spec/lib/flnt/logger_spec.rb
104
+ - spec/lib/flnt_spec.rb
105
+ - spec/spec_helper.rb
106
+ - wercker.yml
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.0.14
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Gentle post-to-fluentd log solution
131
+ test_files:
132
+ - spec/lib/flnt/configuration_spec.rb
133
+ - spec/lib/flnt/logger_spec.rb
134
+ - spec/lib/flnt_spec.rb
135
+ - spec/spec_helper.rb