alephant-logger 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9a5bd35b04aa6d2617caf7b3981ed99bc04805e
4
- data.tar.gz: bfe2dc0e7ea1d273ebd7d6d6b22c95aa5ec1f28a
3
+ metadata.gz: 2d605d1b63ad9aba8fa73757565282ab99da4315
4
+ data.tar.gz: 8c5aaba175a66c483b007996b565e1bd33b47eac
5
5
  SHA512:
6
- metadata.gz: 63912a965c633660e323df8bf9d3898439b65580ee9a72a44218a307453629c52cb4f39549c52df4e51a7d93887f743e8d0e56ca439808425c79b64da7c4732d
7
- data.tar.gz: 7e3460616caf70dcf6c95da656a3baf08884f4021df7f485ec22f3ba2f185cdf3182113318fe79f8a1755c963e7643af1dc3c373d5c5c98e683b4d5fb7d14d28
6
+ metadata.gz: c32c78d2569da9febbf690ede022a1f455bd06d6a58300985d94c2210008b89b170166752df5366a1a8ac9a8ea2134d5263476041635fc8bc245bb2ef3a506a5
7
+ data.tar.gz: b481bd868cd2a352f5e020412120d4254c399bfa7879e6a76c220eea464fd65e0d65b8feed6e273a5f24708a2914e3245cc2c0c9a4f99a140298ce316f705758
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ jruby-1.7.9
data/README.md CHANGED
@@ -44,12 +44,53 @@ class SetsLogger
44
44
  logger.warn "SUCH CUSTOM. MUCH LOG."
45
45
  end
46
46
  end
47
+ ```
48
+
49
+ ### AWS CloudWatch Metrics
50
+
51
+ ```ruby
52
+ require "aws-sdk"
53
+ require "alephant/logger"
54
+ require "alephant/logger/cloudwatch_decorator"
55
+
56
+ AWS.config(
57
+ :region => "eu-west-1",
58
+ :access_key_id => "ACCESS_KEY_ID",
59
+ :secret_access_key => "SECRET_ACCESS_KEY",
60
+ :session_token => "SESSION_TOKEN"
61
+ )
62
+
63
+ LOGGER = Alephant::Logger::CloudWatchDecorator.new(
64
+ Logger.new("app.log"), "SomeCloudWatchMetricNameSpace"
65
+ )
66
+
67
+ Alephant::Logger.set_logger LOGGER
68
+
69
+ class Foo
70
+ include Alephant::Logger
71
+
72
+ def initialize
73
+ logger.info "some info"
74
+ logger.debug "much debug"
75
+ logger.error "great error"
76
+ logger.warn "so warn"
77
+ logger.fatal "ooh fatal"
78
+
79
+ # We set the value of the metric to 123
80
+ logger.metric({:name => "SomeMetricName", :unit => "Count", :value => 123})
81
+
82
+ # Notice that no :value key is specified, meaning we'll increment the last seen value
83
+ # If this metric hasn't been set within the last hour then we'll reset the value back to zero
84
+ logger.metric({:name => "SomeMetricName", :unit => "Count"})
85
+ end
86
+ end
47
87
 
88
+ Foo.new
48
89
  ```
49
90
 
50
91
  ## Contributing
51
92
 
52
- 1. Fork it ( http://github.com/<my-github-username>/alephant-logger/fork )
93
+ 1. Fork it ( http://github.com/BBC-News/alephant-logger/fork )
53
94
  2. Create your feature branch (`git checkout -b my-new-feature`)
54
95
  3. Commit your changes (`git commit -am 'Add some feature'`)
55
96
  4. Push to the branch (`git push origin my-new-feature`)
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.add_runtime_dependency "aws-sdk"
21
+
20
22
  spec.add_development_dependency "rspec"
21
23
  spec.add_development_dependency "rspec-nc"
22
24
  spec.add_development_dependency "guard"
@@ -0,0 +1,90 @@
1
+ require "aws-sdk"
2
+
3
+ module Alephant
4
+ module Logger
5
+ class CloudWatchDecorator
6
+ ONE_HOUR = 3600
7
+
8
+ def initialize(logger, namespace)
9
+ @logger = logger
10
+ @namespace = namespace
11
+ @cloudwatch = AWS::CloudWatch.new
12
+ end
13
+
14
+ def metric(opts)
15
+ name, value, unit, dimensions = opts.values_at(:name, :value, :unit, :dimensions)
16
+
17
+ cloudwatch.put_metric_data(
18
+ :namespace => namespace,
19
+ :metric_data => [{
20
+ :metric_name => name,
21
+ :value => determine(name, value),
22
+ :unit => unit || "None",
23
+ :dimensions => parse(dimensions || {})
24
+ }]
25
+ )
26
+ end
27
+
28
+ # Ruby's Kernel implements a `warn` method
29
+ def warn(*args)
30
+ logger.warn(*args)
31
+ end
32
+
33
+ def method_missing(name, *args)
34
+ logger.send(name, *args)
35
+ end
36
+
37
+ def respond_to?(name)
38
+ logger.respond_to?(name) || super
39
+ end
40
+
41
+ private
42
+
43
+ attr_reader :logger, :cloudwatch, :namespace
44
+
45
+ def parse(dimensions)
46
+ dimensions.map do |name, value|
47
+ {
48
+ :name => name,
49
+ :value => value
50
+ }
51
+ end
52
+ end
53
+
54
+ def determine(name, value)
55
+ value ? value : increment_metric_value(name)
56
+ end
57
+
58
+ def increment_metric_value(name)
59
+ stats = get_stats name
60
+ stats.datapoints.size == 0 ? 0 : stats.datapoints.last[:sum] += 1
61
+ end
62
+
63
+ def get_stats(name)
64
+ stats_for(filter_metric name).tap do |stats|
65
+ sort stats.datapoints if stats.datapoints.size > 0
66
+ end
67
+ end
68
+
69
+ def filter_metric(name)
70
+ filter_namespace.filter("metric_name", name)
71
+ end
72
+
73
+ def filter_namespace
74
+ cloudwatch.metrics.filter("namespace", namespace)
75
+ end
76
+
77
+ def stats_for(metric)
78
+ metric.first.statistics({
79
+ :statistics => ["Sum"],
80
+ :start_time => Time.now - ONE_HOUR,
81
+ :end_time => Time.now
82
+ })
83
+ end
84
+
85
+ def sort(stats)
86
+ stats.sort! { |a, b| a[:timestamp] <=> b[:timestamp] }
87
+ end
88
+ end
89
+ end
90
+ end
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Logger
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -2,6 +2,22 @@ require 'alephant/logger/version'
2
2
  require 'logger'
3
3
 
4
4
  module Alephant
5
+ class DelegatingLogger
6
+ attr_reader :logger
7
+
8
+ def initialize(logger)
9
+ @logger = logger
10
+ end
11
+
12
+ def method_missing(name, *args)
13
+ logger.send(name, *args) if logger.respond_to? name
14
+ end
15
+
16
+ def respond_to?(name)
17
+ logger.respond_to? name || super
18
+ end
19
+ end
20
+
5
21
  module Logger
6
22
  @@logger = nil
7
23
 
@@ -10,12 +26,11 @@ module Alephant
10
26
  end
11
27
 
12
28
  def self.get_logger
13
- @@logger ||= ::Logger.new(STDOUT)
29
+ @@logger ||= Alephant::DelegatingLogger.new ::Logger.new(STDOUT)
14
30
  end
15
31
 
16
32
  def self.set_logger(value)
17
- @@logger = value
33
+ @@logger = Alephant::DelegatingLogger.new value
18
34
  end
19
35
  end
20
36
  end
21
-
data/spec/logger_spec.rb CHANGED
@@ -4,9 +4,9 @@ describe Alephant::Logger do
4
4
  describe ".get_logger" do
5
5
 
6
6
  context "no logger set" do
7
- it "returns standard Logger" do
8
- expect(subject.get_logger).to be_a Logger
9
- end
7
+ specify {
8
+ expect(subject.get_logger.logger).to be_a Logger
9
+ }
10
10
  end
11
11
 
12
12
  context "subject.set_logger(:foo)" do
@@ -20,7 +20,7 @@ describe Alephant::Logger do
20
20
  describe ".set_logger(:foo)" do
21
21
  it "sets foo as @@logger" do
22
22
  subject.set_logger(:bar)
23
- expect(subject.class_variable_get(:@@logger)).to eq(:bar)
23
+ expect(subject.class_variable_get(:@@logger).logger).to eq :bar
24
24
  end
25
25
  end
26
26
 
@@ -37,7 +37,7 @@ describe Alephant::Logger do
37
37
  context "called via '#logger'" do
38
38
  it "returns the value of @@logger" do
39
39
  subject.class_variable_set(:@@logger, :baz)
40
- expect(IncludesLog.new.calls_logger).to eq(:baz)
40
+ expect(IncludesLog.new.calls_logger).to eq :baz
41
41
  end
42
42
  end
43
43
 
metadata CHANGED
@@ -1,142 +1,156 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Kenny
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-13 00:00:00.000000000 Z
11
+ date: 2014-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
15
22
  version_requirements: !ruby/object:Gem::Requirement
16
23
  requirements:
17
24
  - - '>='
18
25
  - !ruby/object:Gem::Version
19
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
20
29
  requirement: !ruby/object:Gem::Requirement
21
30
  requirements:
22
31
  - - '>='
23
32
  - !ruby/object:Gem::Version
24
33
  version: '0'
25
- prerelease: false
26
34
  type: :development
27
- - !ruby/object:Gem::Dependency
28
- name: rspec-nc
35
+ prerelease: false
29
36
  version_requirements: !ruby/object:Gem::Requirement
30
37
  requirements:
31
38
  - - '>='
32
39
  - !ruby/object:Gem::Version
33
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-nc
34
43
  requirement: !ruby/object:Gem::Requirement
35
44
  requirements:
36
45
  - - '>='
37
46
  - !ruby/object:Gem::Version
38
47
  version: '0'
39
- prerelease: false
40
48
  type: :development
41
- - !ruby/object:Gem::Dependency
42
- name: guard
49
+ prerelease: false
43
50
  version_requirements: !ruby/object:Gem::Requirement
44
51
  requirements:
45
52
  - - '>='
46
53
  - !ruby/object:Gem::Version
47
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
48
57
  requirement: !ruby/object:Gem::Requirement
49
58
  requirements:
50
59
  - - '>='
51
60
  - !ruby/object:Gem::Version
52
61
  version: '0'
53
- prerelease: false
54
62
  type: :development
55
- - !ruby/object:Gem::Dependency
56
- name: guard-rspec
63
+ prerelease: false
57
64
  version_requirements: !ruby/object:Gem::Requirement
58
65
  requirements:
59
66
  - - '>='
60
67
  - !ruby/object:Gem::Version
61
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
62
71
  requirement: !ruby/object:Gem::Requirement
63
72
  requirements:
64
73
  - - '>='
65
74
  - !ruby/object:Gem::Version
66
75
  version: '0'
67
- prerelease: false
68
76
  type: :development
69
- - !ruby/object:Gem::Dependency
70
- name: pry
77
+ prerelease: false
71
78
  version_requirements: !ruby/object:Gem::Requirement
72
79
  requirements:
73
80
  - - '>='
74
81
  - !ruby/object:Gem::Version
75
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
76
85
  requirement: !ruby/object:Gem::Requirement
77
86
  requirements:
78
87
  - - '>='
79
88
  - !ruby/object:Gem::Version
80
89
  version: '0'
81
- prerelease: false
82
90
  type: :development
83
- - !ruby/object:Gem::Dependency
84
- name: pry-remote
91
+ prerelease: false
85
92
  version_requirements: !ruby/object:Gem::Requirement
86
93
  requirements:
87
94
  - - '>='
88
95
  - !ruby/object:Gem::Version
89
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-remote
90
99
  requirement: !ruby/object:Gem::Requirement
91
100
  requirements:
92
101
  - - '>='
93
102
  - !ruby/object:Gem::Version
94
103
  version: '0'
95
- prerelease: false
96
104
  type: :development
97
- - !ruby/object:Gem::Dependency
98
- name: pry-nav
105
+ prerelease: false
99
106
  version_requirements: !ruby/object:Gem::Requirement
100
107
  requirements:
101
108
  - - '>='
102
109
  - !ruby/object:Gem::Version
103
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-nav
104
113
  requirement: !ruby/object:Gem::Requirement
105
114
  requirements:
106
115
  - - '>='
107
116
  - !ruby/object:Gem::Version
108
117
  version: '0'
109
- prerelease: false
110
118
  type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: bundler
113
- version_requirements: !ruby/object:Gem::Requirement
127
+ requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
129
  - - ~>
116
130
  - !ruby/object:Gem::Version
117
131
  version: '1.5'
118
- requirement: !ruby/object:Gem::Requirement
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
119
135
  requirements:
120
136
  - - ~>
121
137
  - !ruby/object:Gem::Version
122
138
  version: '1.5'
123
- prerelease: false
124
- type: :development
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: rake
127
- version_requirements: !ruby/object:Gem::Requirement
141
+ requirement: !ruby/object:Gem::Requirement
128
142
  requirements:
129
143
  - - '>='
130
144
  - !ruby/object:Gem::Version
131
145
  version: '0'
132
- requirement: !ruby/object:Gem::Requirement
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
133
149
  requirements:
134
150
  - - '>='
135
151
  - !ruby/object:Gem::Version
136
152
  version: '0'
137
- prerelease: false
138
- type: :development
139
- description:
153
+ description:
140
154
  email:
141
155
  - kenoir@gmail.com
142
156
  executables: []
@@ -144,6 +158,7 @@ extensions: []
144
158
  extra_rdoc_files: []
145
159
  files:
146
160
  - .gitignore
161
+ - .ruby-version
147
162
  - .travis.yml
148
163
  - Gemfile
149
164
  - Guardfile
@@ -152,6 +167,7 @@ files:
152
167
  - Rakefile
153
168
  - alephant-logger.gemspec
154
169
  - lib/alephant/logger.rb
170
+ - lib/alephant/logger/cloudwatch_decorator.rb
155
171
  - lib/alephant/logger/version.rb
156
172
  - spec/logger_spec.rb
157
173
  - spec/spec_helper.rb
@@ -159,7 +175,7 @@ homepage: ''
159
175
  licenses:
160
176
  - MIT
161
177
  metadata: {}
162
- post_install_message:
178
+ post_install_message:
163
179
  rdoc_options: []
164
180
  require_paths:
165
181
  - lib
@@ -174,9 +190,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
190
  - !ruby/object:Gem::Version
175
191
  version: '0'
176
192
  requirements: []
177
- rubyforge_project:
178
- rubygems_version: 2.1.9
179
- signing_key:
193
+ rubyforge_project:
194
+ rubygems_version: 2.0.14
195
+ signing_key:
180
196
  specification_version: 4
181
197
  summary: Logger functionality for Alephant
182
198
  test_files: