yog 0.2.0 → 0.3.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
  SHA256:
3
- metadata.gz: 75d9f8013927b9e382cb7b01d4007167d0ade47f1a018f3be60e2042530e29f1
4
- data.tar.gz: 5a02f9b446a8b614bd406afbdfd4fedadbd3214fabacce26eb9861e0a71b2609
3
+ metadata.gz: db8903f2744b37ff04bc5dd36f44edb790b4878d176fd77a900ff5cef858680b
4
+ data.tar.gz: 03a96abc9076220df30f0b55be41befb92eee172dc88d0224be0cdad8ce3f732
5
5
  SHA512:
6
- metadata.gz: dff04a9a278360f3b82b8cfa21f90469fc0401d59373e193ebf5b021d342ff017044973578df2e5978e760b1b7a6e01b936ff190ef9b4644513b7ae42693ed42
7
- data.tar.gz: b2b11557f4b030a64a722ef966b9bfcad0d2924058ae0c555358b673ed7843965950d4f9689775e0184f5e37448601253000a3b5008d06d77912f0ba35f44068
6
+ metadata.gz: 46fbe2aa15a5fab09ba295a39fdb9673154a949dbaec4506322ee3d0b36a5b2b89ef337e594386f6c3dffe659c611f6319cc3662bf9caf7033faa716d738a825
7
+ data.tar.gz: c18237cde2eaf11e02e8cc6ba567709464972e3730b974a29e01c1cdbfda6ed5e3001af87785d15cab5d5b56f39cf372c92555fa883fce0da318699b571ca569
data/lib/yog.rb CHANGED
@@ -15,9 +15,9 @@ class Yog
15
15
  end
16
16
 
17
17
  # Log a message. Logs any given block's duration as `elapsed=float` ms.
18
- def write(msg, **fields)
18
+ def info(msg, **fields)
19
19
  if @output.nil?
20
- return block_given? && yield
20
+ return (yield if block_given?)
21
21
  end
22
22
 
23
23
  if msg.is_a?(Hash)
@@ -37,7 +37,7 @@ class Yog
37
37
  prefix[:duration] = (Time.now - start) * 1000
38
38
  end
39
39
 
40
- combined = Yog.merge(prefix, fields, @context)
40
+ combined = prefix.merge(@context).merge(fields)
41
41
  @output.puts(@generator.generate(combined))
42
42
 
43
43
  result
@@ -45,30 +45,30 @@ class Yog
45
45
 
46
46
  # Log an error.
47
47
  def error(ex, **fields)
48
- write(ex.message, at: "error", type: ex.class, **fields)
48
+ info(error: ex.message, type: ex.class, **fields)
49
49
  end
50
50
 
51
51
  # Update the context.
52
- def push(**fields)
53
- self.tap { @context.merge!(fields) }
52
+ def set(**fields)
53
+ tap { @context.merge!(fields) }
54
54
  end
55
55
 
56
56
  # Create a new log with more context.
57
57
  def with(**fields)
58
58
  self.class.new \
59
- context: Yog.merge(@context, fields),
59
+ context: @context.merge(fields),
60
60
  generator: @generator,
61
61
  output: @output
62
62
  end
63
63
 
64
64
  ### Helpers for the default log
65
65
 
66
- # The log for Yog(), Yog.push(), and Yog.with().
67
- def self.current
66
+ # The log for Yog(), Yog.set(), and Yog.with().
67
+ def self.default
68
68
  logs.first
69
69
  end
70
70
 
71
- # A stack of receivers with the current log first.
71
+ # A stack of receivers with the default log first.
72
72
  def self.logs
73
73
  Thread.current[:logs] ||= [new]
74
74
  end
@@ -80,38 +80,33 @@ class Yog
80
80
  logs.shift
81
81
  end
82
82
 
83
- # Update the context of the current log.
84
- def self.push(**fields)
85
- current.push(fields)
83
+ # Update the context of the default log.
84
+ def self.set(**fields)
85
+ default.set(fields)
86
86
  end
87
87
 
88
- # Create a new log by extending the context of the current log.
88
+ # Create a new log by extending the context of the default log.
89
89
  def self.with(**fields)
90
- current.with(fields)
90
+ default.with(fields)
91
91
  end
92
92
 
93
- # Write a line to the current log.
94
- def self.write(msg, **fields, &block)
95
- current.write(msg, fields, &block)
93
+ # Write a line to the default log.
94
+ def self.info(msg, **fields, &block)
95
+ default.info(msg, fields, &block)
96
96
  end
97
97
 
98
- # Log an error to the current log.
98
+ # Log an error to the default log.
99
99
  def self.error(ex, **fields)
100
- current.error(ex, fields)
101
- end
102
-
103
- # Internal: Combine a list of contexts.
104
- def self.merge(*contexts)
105
- contexts.inject { |a, b| a.merge(b) }
100
+ default.error(ex, fields)
106
101
  end
107
102
  end
108
103
 
109
- # Write a line to the current log.
104
+ # Write a line to the default log.
110
105
  def Yog(msg, **fields, &block)
111
106
  case msg
112
107
  when Exception
113
108
  Yog.error(msg, fields)
114
109
  else
115
- Yog.write(msg, fields, &block)
110
+ Yog.info(msg, fields, &block)
116
111
  end
117
112
  end
@@ -21,7 +21,7 @@ class Yog
21
21
  when FalseClass
22
22
  "false"
23
23
  when DateTime, Time
24
- v.iso8601(2)
24
+ v.iso8601(3)
25
25
  when Float
26
26
  sprintf("%.2f", v)
27
27
  when Numeric
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack/request"
4
+ require "rack/utils"
5
+ require "securerandom"
6
+ require "yog"
7
+
8
+ class Yog
9
+ class Rack
10
+ def initialize(app, **ctx)
11
+ @app = app
12
+ @ctx = ctx.merge(svc: "rack")
13
+ end
14
+
15
+ def call(env)
16
+ now = Time.now
17
+ req = ::Rack::Request.new(env)
18
+ rid = (env["HTTP_X_REQUEST_ID"] ||= SecureRandom.uuid)
19
+
20
+ Yog.context **@ctx, rid: rid do
21
+ begin
22
+ res = @app.call(env)
23
+ ensure
24
+ duration = (Time.now - now) * 1000
25
+ status, headers, * = res
26
+ headers["X-Request-ID"] ||= rid
27
+
28
+ Yog ::Rack::Utils::HTTP_STATUS_CODES[status],
29
+ at: "finish",
30
+ method: req.request_method,
31
+ path: req.fullpath,
32
+ status: status,
33
+ duration: duration
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "yog"
3
- spec.version = "0.2.0"
3
+ spec.version = "0.3.0"
4
4
  spec.authors = ["John Barnette"]
5
5
  spec.email = ["john@jbarnette.com"]
6
6
 
7
- spec.summary = "A lightweight Ruby library for emitting logfmt style log lines."
7
+ spec.summary = "A Ruby library for emitting logfmt style log lines."
8
8
  spec.homepage = "https://github.com/jbarnette/yog"
9
9
  spec.license = "MIT"
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Barnette
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-14 00:00:00.000000000 Z
11
+ date: 2018-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,6 +101,7 @@ files:
101
101
  - docs/CODE_OF_CONDUCT.md
102
102
  - lib/yog.rb
103
103
  - lib/yog/logfmt.rb
104
+ - lib/yog/rack.rb
104
105
  - yog.gemspec
105
106
  homepage: https://github.com/jbarnette/yog
106
107
  licenses:
@@ -125,5 +126,5 @@ rubyforge_project:
125
126
  rubygems_version: 2.7.3
126
127
  signing_key:
127
128
  specification_version: 4
128
- summary: A lightweight Ruby library for emitting logfmt style log lines.
129
+ summary: A Ruby library for emitting logfmt style log lines.
129
130
  test_files: []