l2meter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d9d24762c770d4796433243c9a0e79fdab77a3a9
4
+ data.tar.gz: 4132e41611d13554df9caffe440764ef5d9c915d
5
+ SHA512:
6
+ metadata.gz: e97d987cf5098a46759a41ea4102d96c5c25ea8c82b4c0555b6aec077958a5af78ea64d0e972611a882f0ad11ff90497d0672f972d5dba27cb1acebc51df7b14
7
+ data.tar.gz: aabc3a2a2861904bab33896aab903a922d103d45d8e6c503fb9065911e3abda36fcc41a1198783a0a6c0ea8c86a5f7f7e5d5e416926ae77a2b7ff1bf1439b36f
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pavel Pravosud
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,35 @@
1
+ # L2meter
2
+
3
+ L2meter is a little gem that helps you build loggers that outputs things in l2met format.
4
+
5
+ ### Usage
6
+
7
+ ```ruby
8
+ Metrics = L2meter.build do |config|
9
+ # sort output tokens, false by default
10
+ config.sort = true
11
+
12
+ # default context
13
+ config.context = { name: "my-app-name" }
14
+
15
+ # ... or dynamic context
16
+ config.context do
17
+ { random_thing: SecureRandom.uuid }
18
+ end
19
+
20
+ # $stdout by default
21
+ config.output = StringIO.new
22
+ end
23
+
24
+ Metrics.log "Hello world" # => hello-world
25
+
26
+ Metrics.log :foo, :bar, fizz: :buzz # => foo bar fizz=buzz
27
+
28
+ Metrics.log :doing_work do # => doing-work at=start
29
+ do_some_work #
30
+ end # => doing-work at=finish elapsed=3.1234s
31
+
32
+ Metrics.log :deez_nutz do # => deez-nutz at=start
33
+ raise ArgumentError, "error here" #
34
+ end # => deez-nutz at=exception exception=ArgumentError message="error here" elapsed=1.2345s
35
+ ```
@@ -0,0 +1,51 @@
1
+ module L2meter
2
+ class Configuration
3
+ attr_writer :output
4
+
5
+ def output
6
+ @output ||= $stdout
7
+ end
8
+
9
+ def key_formatter
10
+ @key_formatter ||= ->(key) do
11
+ key.to_s.strip.downcase.gsub(/[^-a-z\d]+/, ?-)
12
+ end
13
+ end
14
+
15
+ def format_keys(&block)
16
+ @key_formatter = block
17
+ end
18
+
19
+ def value_formatter
20
+ @value_formatter ||= ->(value) do
21
+ value = value.to_s
22
+ value =~ /\s/ ? value.inspect : value
23
+ end
24
+ end
25
+
26
+ def format_values(&block)
27
+ @value_formatter = block
28
+ end
29
+
30
+ def sort?
31
+ defined?(@apply_sort) ? @apply_sort : false
32
+ end
33
+
34
+ def sort=(value)
35
+ @apply_sort = !!value
36
+ end
37
+
38
+ def context(&block)
39
+ @context_block = block
40
+ end
41
+
42
+ def context=(value)
43
+ @static_context = value
44
+ end
45
+
46
+ def get_context
47
+ return @static_context if @static_context
48
+ @context_block ? @context_block.call.to_h : {}
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,55 @@
1
+ module L2meter
2
+ class Emitter
3
+ attr_reader :configuration
4
+
5
+ def initialize(configuration: Configuration.new)
6
+ @configuration = configuration
7
+ end
8
+
9
+ def log(*args, **params)
10
+ args = args.map { |key| [ key, true ] }.to_h
11
+ params = args.merge(params)
12
+ params = context.merge(params)
13
+
14
+ if block_given?
15
+ wrap params, &Proc.new
16
+ else
17
+ write params
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def context
24
+ configuration.get_context
25
+ end
26
+
27
+ def write(params)
28
+ tokens = params.map do |key, value|
29
+ key = configuration.key_formatter.call(key)
30
+ next key if value == true
31
+ value = configuration.value_formatter.call(value)
32
+ [ key, value ] * ?=
33
+ end
34
+
35
+ tokens.sort! if configuration.sort?
36
+
37
+ configuration.output.puts tokens.join(" ")
38
+ end
39
+
40
+ def wrap(params)
41
+ time_at_start = Time.now
42
+ write params.merge(at: :start)
43
+ yield
44
+ rescue => error
45
+ status = { at: :exception, exception: error.class.to_s, message: error.message.strip }
46
+ raise
47
+ else
48
+ status = { at: :finish }
49
+ ensure
50
+ elapsed = Time.now - time_at_start
51
+ status.merge! elapsed: "%.4fs" % elapsed
52
+ write params.merge(status)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module L2meter
2
+ VERSION = "0.0.1".freeze
3
+ end
data/lib/l2meter.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "l2meter/version"
2
+
3
+ module L2meter
4
+ extend self
5
+
6
+ autoload :Configuration, "l2meter/configuration"
7
+ autoload :Emitter, "l2meter/emitter"
8
+
9
+ def build
10
+ Emitter.new.tap do |emitter|
11
+ yield emitter.configuration if block_given?
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: l2meter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Pravosud
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - pavel@heroku.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - lib/l2meter.rb
23
+ - lib/l2meter/configuration.rb
24
+ - lib/l2meter/emitter.rb
25
+ - lib/l2meter/version.rb
26
+ homepage: https://github.com/heroku/l2meter
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.4.8
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: L2met friendly log formatter
50
+ test_files: []