yog 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: 7a368faf2b40b6f7874d643db6cdf6d92f888f11d11e6d9592564f99e9f347c8
4
+ data.tar.gz: 9326d66736f140ab0ca623efb54596e3dd90dba49e7256107a10679b83f8436c
5
+ SHA512:
6
+ metadata.gz: 7a76c80aeabb756397ec4aa6a5311dee0d781590a4fbcff68177f7b580bf02d5c8b9118cb288e65005208a2b801beb121319b4b0f077b8152a1386a85fa7757b
7
+ data.tar.gz: fcb89cc14d962bed269ac576503861a99e82c61c0da22ea34415199210310139bc05e01438f1a9c44229b47db2eecf115245ac76aa0da6630d4179e7060a96fa
@@ -0,0 +1,2 @@
1
+ .ruby-version
2
+ *.gem
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ script: bin/test
3
+
4
+ rvm:
5
+ - 2.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ ruby "2.5"
2
+ source "https://rubygems.org"
3
+
4
+ gemspec
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yog (0.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ ffi (1.9.21)
11
+ formatador (0.2.5)
12
+ guard (2.14.2)
13
+ formatador (>= 0.2.4)
14
+ listen (>= 2.7, < 4.0)
15
+ lumberjack (>= 1.0.12, < 2.0)
16
+ nenv (~> 0.1)
17
+ notiffany (~> 0.0)
18
+ pry (>= 0.9.12)
19
+ shellany (~> 0.0)
20
+ thor (>= 0.18.1)
21
+ guard-compat (1.2.1)
22
+ guard-minitest (2.4.6)
23
+ guard-compat (~> 1.2)
24
+ minitest (>= 3.0)
25
+ listen (3.1.5)
26
+ rb-fsevent (~> 0.9, >= 0.9.4)
27
+ rb-inotify (~> 0.9, >= 0.9.7)
28
+ ruby_dep (~> 1.2)
29
+ lumberjack (1.0.12)
30
+ method_source (0.9.0)
31
+ minitest (5.11.3)
32
+ nenv (0.3.0)
33
+ notiffany (0.1.1)
34
+ nenv (~> 0.1)
35
+ shellany (~> 0.0)
36
+ pry (0.11.3)
37
+ coderay (~> 1.1.0)
38
+ method_source (~> 0.9.0)
39
+ rb-fsevent (0.10.2)
40
+ rb-inotify (0.9.10)
41
+ ffi (>= 0.5.0, < 2)
42
+ ruby_dep (1.5.0)
43
+ shellany (0.0.1)
44
+ thor (0.20.0)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 1.16)
51
+ guard (~> 2.14)
52
+ guard-minitest (~> 2.4)
53
+ minitest (~> 5.11)
54
+ pry (~> 0.11)
55
+ yog!
56
+
57
+ RUBY VERSION
58
+ ruby 2.5.0p0
59
+
60
+ BUNDLED WITH
61
+ 1.16.1
@@ -0,0 +1,4 @@
1
+ guard :minitest do
2
+ watch(%r{_test\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
4
+ end
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright © 2018 John Barnette
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,7 @@
1
+ # Yog [<img align="right" alt="Build status" src="https://travis-ci.org/jbarnette/yog.svg">](https://travis-ci.org/jbarnette/yog)
2
+
3
+ A lightweight Ruby library for emitting [logfmt](https://brandur.org/logfmt) style `key=value` log lines.
4
+
5
+ ## Contributing
6
+
7
+ Yog is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). Bug reports and pull requests are welcome. Everyone interacting in the Yog project’s codebase, issues, and PRs is expected to follow the [code of conduct](docs/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ #!/bin/sh
2
+ # Run a REPL.
3
+
4
+ set -e
5
+
6
+ cd "$(dirname "$0")/.."
7
+
8
+ exec ruby -r bundler/setup -r pry -r yog -e Pry.start -- "$@"
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'guard' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("guard", "guard")
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+ # Install development dependencies.
3
+
4
+ set -e
5
+
6
+ cd "$(dirname "$0")/.."
7
+ bundle install
@@ -0,0 +1,9 @@
1
+ #!/bin/sh
2
+ # CI entrypoint, run the tests.
3
+
4
+ set -e
5
+
6
+ cd "$(dirname "$0")/.."
7
+
8
+ exec ruby -r bundler/setup -r minitest/autorun \
9
+ -e "Dir['test/**/*_test.rb'].each { |f| load f }"
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jbarnette@github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yog/logfmt"
4
+
5
+ # Writes structured log lines.
6
+ class Yog
7
+
8
+ # Get ready to do some yogging.
9
+ def initialize(generator: Logfmt, output: $stdout, context: {})
10
+ @context = context
11
+ @generator = generator
12
+ @output = output
13
+
14
+ freeze
15
+ end
16
+
17
+ # Log a message. Logs any given block's duration as `elapsed=float` ms.
18
+ def write(msg, **fields)
19
+ if msg.is_a?(Hash)
20
+ fields, msg = msg, nil
21
+ end
22
+
23
+ result = if block_given?
24
+ start = Time.now
25
+
26
+ yield.tap do
27
+ fields[:duration] = (Time.now - start) * 1000
28
+ end
29
+ end
30
+
31
+ unless @output.nil?
32
+ prefix = { now: Time.now.utc, msg: msg }
33
+ combined = Yog.merge(prefix, fields, @context)
34
+ @output.puts(@generator.generate(combined))
35
+ end
36
+
37
+ result
38
+ end
39
+
40
+ # Log an error.
41
+ def error(ex, **fields)
42
+ write(ex.message, at: "error", type: ex.class, **fields)
43
+ end
44
+
45
+ # Update the context.
46
+ def push(**fields)
47
+ self.tap { @context.merge!(fields) }
48
+ end
49
+
50
+ # Create a new log with more context.
51
+ def with(**fields)
52
+ self.class.new \
53
+ context: Yog.merge(@context, fields),
54
+ generator: @generator,
55
+ output: @output
56
+ end
57
+
58
+ ### Helpers for the default log
59
+
60
+ # The log for Yog(), Yog.push(), and Yog.with().
61
+ def self.current
62
+ logs.first
63
+ end
64
+
65
+ # A stack of receivers with the current log first.
66
+ def self.logs
67
+ Thread.current[:logs] ||= [new]
68
+ end
69
+
70
+ # Add fields to all Yog() calls in the given block.
71
+ def self.context(**fields)
72
+ yield logs.unshift(with(fields))
73
+ ensure
74
+ logs.shift
75
+ end
76
+
77
+ # Update the context of the current log.
78
+ def self.push(**fields)
79
+ current.push(fields)
80
+ end
81
+
82
+ # Create a new log by extending the context of the current log.
83
+ def self.with(**fields)
84
+ current.with(fields)
85
+ end
86
+
87
+ # Write a line to the current log.
88
+ def self.write(msg, **fields, &block)
89
+ current.write(msg, fields, &block)
90
+ end
91
+
92
+ # Log an error to the current log.
93
+ def self.error(ex, **fields)
94
+ current.error(ex, fields)
95
+ end
96
+
97
+ # Internal: Combine a list of contexts.
98
+ def self.merge(*contexts)
99
+ contexts.inject { |a, b| a.merge(b) }
100
+ end
101
+ end
102
+
103
+ # Write a line to the current log.
104
+ def Yog(msg, **fields, &block)
105
+ case msg
106
+ when Exception
107
+ Yog.error(msg, fields)
108
+ else
109
+ Yog.write(msg, fields, &block)
110
+ end
111
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ class Yog
6
+
7
+ # https://brandur.org/logfmt
8
+ module Logfmt
9
+ def self.generate(**fields)
10
+ fields.map { |k, v| "#{k}=#{render(v)}" }.join(" ")
11
+ end
12
+
13
+ private
14
+
15
+ def self.render(v)
16
+ case v
17
+ when DateTime, Time
18
+ v.iso8601(2)
19
+ when Float
20
+ sprintf("%.2f", v)
21
+ when NilClass
22
+ "nil"
23
+ when String
24
+ (/[ '",{}\[\]:=]/ =~ v) ? v.inspect : v
25
+ else
26
+ render(v.to_s)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "yog"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["John Barnette"]
5
+ spec.email = ["john@jbarnette.com"]
6
+
7
+ spec.summary = "A lightweight Ruby library for emitting logfmt style log lines."
8
+ spec.homepage = "https://github.com/jbarnette/yog"
9
+ spec.license = "MIT"
10
+
11
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| /^test/ =~ f }
12
+ spec.require_paths = ["lib"]
13
+
14
+ spec.add_development_dependency "bundler", "~> 1.16"
15
+ spec.add_development_dependency "guard", "~> 2.14"
16
+ spec.add_development_dependency "guard-minitest", "~> 2.4"
17
+ spec.add_development_dependency "minitest", "~> 5.11"
18
+ spec.add_development_dependency "pry", "~> 0.11"
19
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Barnette
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-15 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.11'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.11'
83
+ description:
84
+ email:
85
+ - john@jbarnette.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - Guardfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - bin/console
98
+ - bin/guard
99
+ - bin/setup
100
+ - bin/test
101
+ - docs/CODE_OF_CONDUCT.md
102
+ - lib/yog.rb
103
+ - lib/yog/logfmt.rb
104
+ - yog.gemspec
105
+ homepage: https://github.com/jbarnette/yog
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.7.3
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: A lightweight Ruby library for emitting logfmt style log lines.
129
+ test_files: []