moon-logfmt 1.0.1 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5c64d365719d9a645530f96a230a949d7dbb1f6
4
- data.tar.gz: cbb71702fa16336bb4e0c0f04fed3b02de64508a
3
+ metadata.gz: bf7bce0bbd2512b2887937c51043c1d86b40c8a6
4
+ data.tar.gz: faf1d207712d2b8d577e072b259a0a58d94890c9
5
5
  SHA512:
6
- metadata.gz: bf70cd0b1bd2650727e5b48232b0dd8eb762d6510400a717213b508f600e7e26c5fbf4d883833204de4444055740639e9706d91c1669e870cfa8eaab0d8c78f4
7
- data.tar.gz: 478514355e11f0832544701c98d353910c5ad59e12ed3ad1effb5321ad66a91f94d753b49db061ec2207536a0df6c528625ef566be04a08944b6168f3c461c41
6
+ metadata.gz: 55970381b1653d84739073f4094f873369ab11bf13b06b18970d969cdcf8b8e2b631e91d487d801a9f21bc158a64de198cf4b4907968b0692f7cb37e76c7a65c
7
+ data.tar.gz: 778625cfca5db64c496091b4451c2de2dbba28eb89d288885fca10cbaf8e4b2e6eb434761dbfebd4160388b1dd192593fb8538b7a214a8f8c2da85450e421fe6
@@ -0,0 +1,10 @@
1
+ module Moon
2
+ module Logfmt
3
+ # Default KEY=VALUE formatter
4
+ #
5
+ # @param [String] key
6
+ # @param [String] value
7
+ # @return [String]
8
+ FORMATTER = ->(key, value) { "#{key}=#{value}" }
9
+ end
10
+ end
@@ -1,36 +1,22 @@
1
- require 'moon-null_io/null_io'
1
+ require 'moon-logfmt/logger'
2
+ require 'moon-logfmt/null_logger'
2
3
 
3
4
  module Moon
4
5
  # Implementation of logfmt for Moon
5
6
  module Logfmt
6
- # Logging severity.
7
- module Severity
8
- # Low-level information, mostly for developers.
9
- DEBUG = 0
10
- # Generic (useful) information about system operation.
11
- INFO = 1
12
- # A warning.
13
- WARN = 2
14
- # A handleable error condition.
15
- ERROR = 3
16
- # An unhandleable error that results in a program crash.
17
- FATAL = 4
18
- # An unknown message that should always be logged.
19
- UNKNOWN = 5
20
- end
21
-
22
7
  # Regular expression used for checking strings that may need escaping.
23
8
  # This regular expression will validate true if the string doesn't need
24
9
  # escaping.
25
- UNESCAPED_STRING = /\A[a-zA-Z0-9\.\-\_\,\:\;\/]*\z/i
10
+ UNESCAPED_STRING = /\A[\w\.\-\+\%\_\,\:\;\/]*\z/i
26
11
 
27
- # Formats provided data in a logfmt format.
12
+ # Escapes the context values and yields the result.
28
13
  #
29
14
  # @param [Hash<[String, Symbol], String>] data
30
- # @return [String]
31
- def self.format_context(data)
32
- data.map do |pair|
33
- key, value = *pair
15
+ # @yieldparam [String] key
16
+ # @yieldparam [String] value
17
+ def self.escape_context_data(data)
18
+ return to_enum :escape_context_data, data unless block_given?
19
+ data.each_pair do |key, value|
34
20
  case value
35
21
  when Array
36
22
  value = value.join(',')
@@ -38,146 +24,13 @@ module Moon
38
24
  value = value.to_s
39
25
  end
40
26
  value = value.dump unless value =~ UNESCAPED_STRING
41
- "#{key}=#{value}"
42
- end.join(' ')
43
- end
44
-
45
- # Basic Logger class for Logfmt writing
46
- # The main functions are #write and #new
47
- # #new will copy the current logger and append its context data
48
- class Logger
49
- include Severity
50
-
51
- # The underlaying IO to write to, the default is STDOUT
52
- # @return [IO, #puts]
53
- attr_accessor :io
54
- # Whether to prepend timestamps to the logs
55
- # @return [Boolean]
56
- attr_accessor :timestamp
57
- # Context related data, this protected, don't even think of using it.
58
- # @return [Hash<[String, Symbol], String>]
59
- attr_accessor :context
60
- protected :context
61
- protected :context=
62
-
63
- # @param [Hash<[String, Symbol], String>] data
64
- def initialize(data = {})
65
- @io = STDOUT
66
- @context = data
67
- @timestamp = true
68
- end
69
-
70
- # @param [Logfmt::Logger] org
71
- # @return [self]
72
- def initialize_copy(org)
73
- @io = org.io
74
- @timestamp = org.timestamp
75
- @context = org.context.dup
76
- self
77
- end
78
-
79
- # Formats the provided context data
80
- #
81
- # @param [Hash<[String, Symbol], String>] data
82
- # @return [String]
83
- private def format_context(data)
84
- Logfmt.format_context data
85
- end
86
-
87
- # Adds timestamp information to the provided data
88
- #
89
- # @param [Hash<Symbol, Object>] data to add timestamp to
90
- # @return [Hash] data given
91
- private def timestamp_context(data)
92
- t = Time.now
93
- fmt = '%04d-%02d-%02dT%02d:%02d:%02d%s'
94
- s = sprintf(fmt, t.year, t.mon, t.day, t.hour, t.min, t.sec, t.zone)
95
- data[:now] = s
96
- data
97
- end
98
-
99
- # Writes a new log line
100
- #
101
- # @param [Hash<[String, Symbol], String>] data
102
- def write(data)
103
- pre = {}
104
- timestamp_context(pre) if @timestamp
105
- @io.puts format_context(pre.merge(context.merge(data)))
106
- end
107
-
108
- # @!group std Logger interface
109
- # @param [Severity] severity
110
- # @param [String, nil] message
111
- # @param [String, nil] progname
112
- # @yieldreturn [String] message
113
- def add(severity, message = nil, progname = nil, &block)
114
- message = message || (block && block.call)
115
- msg = message || progname
116
- data = {}
117
- data[:progname] = progname if progname && message
118
- data[case severity
119
- when DEBUG then :debug
120
- when ERROR then :error
121
- when FATAL then :fatal
122
- when INFO then :msg
123
- when UNKNOWN then :msg
124
- when WARN then :warn
125
- end] = msg
126
- write data
127
- end
128
- alias :log :add
129
-
130
- # Logs a message
131
- #
132
- # @overload info(message)
133
- # @param [String] message
134
- # @overload info(progname, &block)
135
- # @param [String] progname
136
- # @yieldreturn [String] message
137
- def info(progname = nil, &block)
138
- add(INFO, nil, progname, &block)
139
- end
140
-
141
- # See {#info} for more information.
142
- # (see #info)
143
- def debug(progname = nil, &block)
144
- add(DEBUG, nil, progname, &block)
145
- end
146
-
147
- # See {#info} for more information.
148
- # (see #info)
149
- def error(progname = nil, &block)
150
- add(ERROR, nil, progname, &block)
151
- end
152
-
153
- # See {#info} for more information.
154
- # (see #info)
155
- def fatal(progname = nil, &block)
156
- add(FATAL, nil, progname, &block)
157
- end
158
-
159
- # See {#info} for more information.
160
- # (see #info)
161
- def unknown(progname = nil, &block)
162
- add(UNKNOWN, nil, progname, &block)
163
- end
164
-
165
- # See {#info} for more information.
166
- # (see #info)
167
- def warn(progname = nil, &block)
168
- add(WARN, nil, progname, &block)
169
- end
170
- # @!endgroup
171
-
172
- # Creates a new context by forking the current logger
173
- #
174
- # @param [Hash<[Symbol, String], String>] data
175
- def new(data)
176
- dup.tap { |l| l.context.merge!(data) }
27
+ yield key.to_s, value
177
28
  end
178
29
  end
179
30
 
180
- NullLogger = Logger.new
181
- NullLogger.io = NullIO::OUT
31
+ # (see Moon::Logfmt::Logger#initialize)
32
+ def self.new(*args, &block)
33
+ Moon::Logfmt::Logger.new(*args, &block)
34
+ end
182
35
  end
183
36
  end
@@ -0,0 +1,89 @@
1
+ require 'moon-logfmt/stdlib_loggable'
2
+ require 'moon-logfmt/formatter'
3
+
4
+ module Moon
5
+ module Logfmt
6
+ # Basic Logger class for Logfmt writing
7
+ # The main functions are #write and #new
8
+ # #new will copy the current logger and append its context data
9
+ class Logger
10
+ include StdlibLoggable
11
+
12
+ # The underlaying IO to write to, the default is STDOUT
13
+ # @return [IO, #puts]
14
+ attr_accessor :io
15
+
16
+ # A function which takes a key and value string and produces a string
17
+ # @return [Proc]
18
+ attr_accessor :formatter
19
+
20
+ # Whether to prepend timestamps to the logs
21
+ # @return [Boolean]
22
+ attr_accessor :timestamp
23
+
24
+ # Context related data, this protected, don't even think of using it.
25
+ # @return [Hash<[String, Symbol], String>]
26
+ attr_accessor :context
27
+ protected :context
28
+ protected :context=
29
+
30
+ # @param [Hash<[String, Symbol], String>] data
31
+ def initialize(data = {})
32
+ @io = STDOUT
33
+ @formatter = FORMATTER
34
+ @context = data
35
+ @timestamp = true
36
+ end
37
+
38
+ # @param [Logfmt::Logger] org
39
+ # @return [self]
40
+ def initialize_copy(org)
41
+ @io = org.io
42
+ @timestamp = org.timestamp
43
+ @context = org.context.dup
44
+ @formatter = org.formatter
45
+ self
46
+ end
47
+
48
+ # Formats the provided context data
49
+ #
50
+ # @param [Hash<[String, Symbol], String>] data
51
+ # @return [String]
52
+ private def format_context(data)
53
+ str = []
54
+ Logfmt.escape_context_data data do |key, value|
55
+ str << @formatter.call(key, value)
56
+ end
57
+ str.join(' ')
58
+ end
59
+
60
+ # Adds timestamp information to the provided data
61
+ #
62
+ # @param [Hash<Symbol, Object>] data to add timestamp to
63
+ # @return [Hash] data given
64
+ private def timestamp_context(data)
65
+ t = Time.now
66
+ fmt = '%04d-%02d-%02dT%02d:%02d:%02d%s'
67
+ s = sprintf(fmt, t.year, t.mon, t.day, t.hour, t.min, t.sec, t.zone)
68
+ data[:now] = s
69
+ data
70
+ end
71
+
72
+ # Writes a new log line
73
+ #
74
+ # @param [Hash<[String, Symbol], String>] data
75
+ def write(data)
76
+ pre = {}
77
+ timestamp_context(pre) if @timestamp
78
+ @io.puts format_context(pre.merge(context.merge(data)))
79
+ end
80
+
81
+ # Creates a new context by forking the current logger
82
+ #
83
+ # @param [Hash<[Symbol, String], String>] data
84
+ def new(data)
85
+ dup.tap { |l| l.context.merge!(data) }
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,9 @@
1
+ require 'moon-null_io/null_io'
2
+ require 'moon-logfmt/logger'
3
+
4
+ module Moon
5
+ module Logfmt
6
+ NullLogger = Logger.new
7
+ NullLogger.io = NullIO::OUT
8
+ end
9
+ end
@@ -0,0 +1,88 @@
1
+ module Moon
2
+ module Logfmt
3
+ # Logging severity.
4
+ module Severity
5
+ # Low-level information, mostly for developers.
6
+ DEBUG = 0
7
+ # Generic (useful) information about system operation.
8
+ INFO = 1
9
+ # A warning.
10
+ WARN = 2
11
+ # A handleable error condition.
12
+ ERROR = 3
13
+ # An unhandleable error that results in a program crash.
14
+ FATAL = 4
15
+ # An unknown message that should always be logged.
16
+ UNKNOWN = 5
17
+ end
18
+
19
+ # Interface for the stdlib Logger class
20
+ module StdlibLoggable
21
+ include Severity
22
+
23
+ # @!group std Logger interface
24
+ # @param [Severity] severity
25
+ # @param [String, nil] message
26
+ # @param [String, nil] progname
27
+ # @yieldreturn [String] message
28
+ def add(severity, message = nil, progname = nil, &block)
29
+ message = message || (block && block.call)
30
+ msg = message || progname
31
+ data = {}
32
+ data[:progname] = progname if progname && message
33
+ data[case severity
34
+ when DEBUG then :debug
35
+ when ERROR then :error
36
+ when FATAL then :fatal
37
+ when INFO then :msg
38
+ when UNKNOWN then :msg
39
+ when WARN then :warn
40
+ end] = msg
41
+ write data
42
+ end
43
+ alias :log :add
44
+
45
+ # Logs a message
46
+ #
47
+ # @overload info(message)
48
+ # @param [String] message
49
+ # @overload info(progname, &block)
50
+ # @param [String] progname
51
+ # @yieldreturn [String] message
52
+ def info(progname = nil, &block)
53
+ add(INFO, nil, progname, &block)
54
+ end
55
+
56
+ # See {#info} for more information.
57
+ # (see #info)
58
+ def debug(progname = nil, &block)
59
+ add(DEBUG, nil, progname, &block)
60
+ end
61
+
62
+ # See {#info} for more information.
63
+ # (see #info)
64
+ def error(progname = nil, &block)
65
+ add(ERROR, nil, progname, &block)
66
+ end
67
+
68
+ # See {#info} for more information.
69
+ # (see #info)
70
+ def fatal(progname = nil, &block)
71
+ add(FATAL, nil, progname, &block)
72
+ end
73
+
74
+ # See {#info} for more information.
75
+ # (see #info)
76
+ def unknown(progname = nil, &block)
77
+ add(UNKNOWN, nil, progname, &block)
78
+ end
79
+
80
+ # See {#info} for more information.
81
+ # (see #info)
82
+ def warn(progname = nil, &block)
83
+ add(WARN, nil, progname, &block)
84
+ end
85
+ # @!endgroup
86
+ end
87
+ end
88
+ end
@@ -3,7 +3,7 @@ module Moon
3
3
  # Version module
4
4
  module Version
5
5
  # @return [Integer, nil]
6
- MAJOR, MINOR, TEENY, PATCH = 1, 0, 1, nil
6
+ MAJOR, MINOR, TEENY, PATCH = 1, 0, 2, nil
7
7
  # @return [String]
8
8
  STRING = [MAJOR, MINOR, TEENY, PATCH].compact.join('.')
9
9
  end
data/spec/logfmt_spec.rb CHANGED
@@ -2,10 +2,10 @@ require 'spec_helper'
2
2
  require 'moon-logfmt/logfmt'
3
3
 
4
4
  describe Moon::Logfmt do
5
- context '.format_context' do
5
+ context '.escape_context_data' do
6
6
  it 'formats a Hash to a String' do
7
- actual = described_class.format_context(msg: 'Hello World', nums: [1, 2, 3])
8
- expect(actual).to eq('msg="Hello World" nums=1,2,3')
7
+ actual = described_class.escape_context_data(msg: 'Hello World', nums: [1, 2, 3]).to_a
8
+ expect(actual).to eq([['msg', '"Hello World"'], ['nums', '1,2,3']])
9
9
  end
10
10
  end
11
11
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moon-logfmt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blaž Hrastnik
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-09-01 00:00:00.000000000 Z
12
+ date: 2015-11-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: moon-null_io
@@ -130,9 +130,13 @@ extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
132
  - lib/moon-logfmt/_basalt.rb
133
+ - lib/moon-logfmt/formatter.rb
133
134
  - lib/moon-logfmt/load.rb
134
135
  - lib/moon-logfmt/logfmt.rb
136
+ - lib/moon-logfmt/logger.rb
137
+ - lib/moon-logfmt/null_logger.rb
135
138
  - lib/moon-logfmt/pkg.yml
139
+ - lib/moon-logfmt/stdlib_loggable.rb
136
140
  - lib/moon-logfmt/version.rb
137
141
  - spec/logfmt_spec.rb
138
142
  - spec/logger_spec.rb
@@ -157,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
161
  version: '0'
158
162
  requirements: []
159
163
  rubyforge_project:
160
- rubygems_version: 2.4.8
164
+ rubygems_version: 2.5.0
161
165
  signing_key:
162
166
  specification_version: 4
163
167
  summary: An implementation of Logfmt encoding.