scrolls 0.3.7 → 0.9.1

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
- SHA1:
3
- metadata.gz: 0dd730a76cce1b9b08ea5dfe7bae8edb9bed2fc3
4
- data.tar.gz: 1d48b1371bff58dc40ce28148ce95f91c756aefa
2
+ SHA256:
3
+ metadata.gz: ef35d7cda907156c55fc36b1486539ac755beb03fc91fd686e396f06609dc9f3
4
+ data.tar.gz: aa19774eeb977616f7e5e64b713bd612c870357ed640134c9dc9ad50d33e7c46
5
5
  SHA512:
6
- metadata.gz: 587412cda539145df827237b30a54d651ab67641689b972fe21bf75fcb79702b9263e7bc646b298301d95bcee75e7951be28e294179de3d77e1b3f93b8198b95
7
- data.tar.gz: db5eb2c16f307861ffaa1f9ab724e0eae99e675017736466a8e819f358a42cacba914473ce3291f640112ad3bde4d35045e824d78a1ee11e1e2484e5b271441a
6
+ metadata.gz: fab8615f38a24e2767bb2f4b1d291f744130af82ee3326109e2c74b8d769d525cfeb761c021f4dc5c95e7fbf859a9bb5db21b29a90c11b948107e11bfc935245
7
+ data.tar.gz: 37b338406b11a641ec6afb8ae28384d0460d3bf3970dd9c083cdc8eaa999d4bd4cf7b5c0dd2929719bda7751ea03cb2f7771dc06fe8911a306a0c74bedc355f0
data/Gemfile CHANGED
@@ -4,5 +4,7 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  group :test do
7
+ gem "minitest", require: 'minitest/autorun'
8
+ gem "minitest-reporters"
7
9
  gem "rake"
8
10
  end
data/README.md CHANGED
@@ -20,26 +20,64 @@ Or install it yourself as:
20
20
 
21
21
  Scrolls follows the belief that logs should be treated as data. One way to think of them is the blood of your infrastructure. Logs are a realtime view of what is happening on your systems.
22
22
 
23
- ## Need to know!
23
+ ## Usage
24
+
25
+ ### 0.9.0 and later
24
26
 
25
- The way Scrolls handles "global_context" is changing after v0.3.8. Please see the [release notes](https://github.com/asenchi/scrolls/releases/tag/v0.3.8) and [this documentation](https://github.com/asenchi/scrolls/tree/master/docs/global-context.md) for more information. I apologize for any trouble this may cause.
27
+ ```ruby
28
+ require 'scrolls'
26
29
 
27
- ## Documentation:
30
+ Scrolls.init(
31
+ timestamp: true,
32
+ global_context: {app: "scrolls", deploy: "production"},
33
+ exceptions: "multi"
34
+ )
28
35
 
29
- I apologize, some of these are a WIP.
36
+ Scrolls.log(at: "test")
30
37
 
31
- * [Sending logs to syslog using Scrolls](https://github.com/asenchi/scrolls/tree/master/docs/syslog.md)
32
- * Logging contexts
33
- * Adding timestamps by default
34
- * Misc Features
38
+ Scrolls.context(context: "block") do
39
+ Scrolls.log(at: "exec")
40
+ end
35
41
 
36
- ## Usage
42
+ begin
43
+ raise
44
+ rescue Exception => e
45
+ Scrolls.log_exception(e, at: "raise")
46
+ end
47
+ ```
48
+
49
+ You can also use `Scrolls#log` and `Scrolls#log_exception` without initalizing:
50
+
51
+ ```ruby
52
+ require 'scrolls'
53
+
54
+ Scrolls.log(test: "test")
55
+ ```
56
+
57
+ ### Defaults
58
+
59
+ Here are the defaults `Scrolls#init`:
60
+
61
+ ```
62
+ stream: STDOUT
63
+ facility: Syslog::LOG_USER
64
+ time_unit: "seconds"
65
+ timestamp: false
66
+ exceptions: "single"
67
+ global_context: {}
68
+ syslog_options: Syslog::LOG_PID|Syslog::LOG_CONS
69
+ escape_keys: false
70
+ ```
71
+
72
+ ## Older Versions
73
+
74
+ ### Pre 0.9.0
37
75
 
38
76
  ```ruby
39
77
  require 'scrolls'
40
78
 
41
79
  Scrolls.add_timestamp = true
42
- Scrolls.global_context(:app => "scrolls", :deploy => ENV["DEPLOY"])
80
+ Scrolls.global_context(:app => "scrolls", :deploy => "production")
43
81
 
44
82
  Scrolls.log(:at => "test")
45
83
 
@@ -57,10 +95,10 @@ end
57
95
  Produces:
58
96
 
59
97
  ```
60
- now="2014-01-17T16:11:39Z" app=scrolls deploy=nil at=test
61
- now="2014-01-17T16:11:39Z" app=scrolls deploy=nil context=block at=exec
62
- now="2014-01-17T16:11:39Z" app=scrolls deploy=nil at=exception class=RuntimeError message= exception_id=70312608019740
63
- now="2014-01-17T16:11:39Z" app=scrolls deploy=nil at=exception class= exception_id=70312608019740 site="./test.rb:16:in <main>"
98
+ now="2017-09-01T00:37:13Z" app=scrolls deploy=production at=test
99
+ now="2017-09-01T00:37:13Z" app=scrolls deploy=production context=block at=exec
100
+ now="2017-09-01T00:37:13Z" app=scrolls deploy=production at=exception class=RuntimeError exception_id=70149797587080
101
+ now="2017-09-01T00:37:13Z" app=scrolls deploy=production at=exception class=RuntimeError exception_id=70149797587080 site="./test-scrolls.rb:16:in <main>"
64
102
  ```
65
103
 
66
104
  ## History
data/Rakefile CHANGED
@@ -1,11 +1,13 @@
1
1
  #!/usr/bin/env rake
2
+
2
3
  require "bundler/gem_tasks"
4
+ require "rake/testtask"
3
5
 
4
6
  ENV['TESTOPTS'] = "-v"
5
7
 
6
- require "rake/testtask"
7
8
  Rake::TestTask.new do |t|
8
- t.pattern = "test/test_*.rb"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ t.verbose = true
9
11
  end
10
12
 
11
13
  task :default => :test
@@ -38,7 +38,7 @@ Scrolls.init(
38
38
  Scrolls.log(:t => "t")
39
39
  ```
40
40
 
41
- Is the same as this currently:
41
+ Is the same as this in versions prior to 0.9.0:
42
42
 
43
43
  ```ruby
44
44
  Scrolls.global_context(:g => "g")
data/docs/syslog.md CHANGED
@@ -6,6 +6,14 @@ By default Scrolls writes log messages to `STDOUT`. With the release of [v0.2.8]
6
6
  Scrolls.stream = "syslog"
7
7
  ```
8
8
 
9
+ Or using `Scrolls#init` in versions 0.9.0 and after:
10
+
11
+ ```ruby
12
+ Scrolls.init(
13
+ stream: "syslog"
14
+ )
15
+ ```
16
+
9
17
  This defaults to syslog facility USER and log level ERROR. You can adjust the log facility like so:
10
18
 
11
19
  ```ruby
@@ -14,4 +22,9 @@ Scrolls.facility = "local7"
14
22
 
15
23
  Scrolls generally doesn't care about log levels. The library defaults to ERROR (or 3), but ultimately is of the opinion that levels are useless. The reasoning behind this is that applications should log useful data, all of the time. Debugging data is great for development, but should never be deployed. The richness of structured logging allows exceptions and error messages to sit along side the context of the data in which the error was thrown, there is no need to send to an "emergency" level.
16
24
 
17
- With that said, if one wanted to adjust the log level, you can set an environment variable `LOG_LEVEL`. This allows this particular feature to be rather fluid throughout your application.
25
+ With that said, if one wanted to adjust the log level, you can set an environment variable `LOG_LEVEL` or use one of the level methods. This allows this particular feature to be rather fluid throughout your application.
26
+
27
+ ```ruby
28
+ Scrolls.info(d: "data")
29
+ Scrolls.warn(d: "data")
30
+ ```
data/lib/scrolls.rb CHANGED
@@ -1,6 +1,4 @@
1
- require "thread"
2
- require "scrolls/atomic"
3
- require "scrolls/log"
1
+ require "scrolls/logger"
4
2
  require "scrolls/version"
5
3
 
6
4
  module Scrolls
@@ -8,33 +6,26 @@ module Scrolls
8
6
 
9
7
  # Public: Initialize a Scrolls logger
10
8
  #
11
- # Convienence method to prepare for future releases. Currently mimics
12
- # behavior found in other methods. This prepares the developer for a future
13
- # backward incompatible change, see:
14
- # https://github.com/asenchi/scrolls/pull/54
15
- #
16
9
  # options - A hash of key/values for configuring Scrolls
17
- #
18
- def init(options)
19
- stream = options.fetch(:stream, STDOUT)
20
- facility = options.fetch(:facility, Syslog::LOG_USER)
21
- time_unit = options.fetch(:time_unit, "seconds")
22
- timestamp = options.fetch(:timestamp, false)
23
- exceptions = options.fetch(:exceptions, "multi")
24
- global_ctx = options.fetch(:global_context, {})
25
-
26
- Log.stream = stream
27
- Log.facility = facility if facility
28
- Log.time_unit = time_unit unless time_unit == "seconds"
29
- Log.add_timestamp = timestamp unless timestamp == false
30
-
31
- if exceptions == "single"
32
- Log.single_line_exceptions = true
33
- end
10
+ # stream - Stream to output data (default: STDOUT)
11
+ # log_facility - Syslog facility (default: Syslog::LOG_USER)
12
+ # time_unit - Unit of time (default: seconds)
13
+ # timestamp - Prepend logs with a timestamp (default: false)
14
+ # exceptions - Method for outputting exceptions (default: single line)
15
+ # global_context - Immutable context to prepend all messages with
16
+ # syslog_options - Syslog options (default: Syslog::LOG_PID|Syslog::LOG_CONS)
17
+ # escape_keys - Escape chars in keys
18
+ #
19
+ def init(options={})
20
+ # Set a hint whether #init was called.
21
+ @initialized = true
22
+ @log = Logger.new(options)
23
+ end
34
24
 
35
- unless global_ctx == {}
36
- Log.global_context = global_ctx
37
- end
25
+ # Public: Get the primary logger
26
+ #
27
+ def logger
28
+ @log.logger
38
29
  end
39
30
 
40
31
  # Public: Set a context in a block for logs
@@ -45,37 +36,13 @@ module Scrolls
45
36
  # Examples:
46
37
  #
47
38
  def context(data, &blk)
48
- Log.with_context(data, &blk)
49
- end
50
-
51
- # Deprecated: Get or set a global context that prefixs all logs
52
- #
53
- # data - A hash of key/values to prepend to each log
54
- #
55
- # This method will be deprecated two releases after 0.3.8.
56
- # See https://github.com/asenchi/scrolls/releases/tag/v0.3.8
57
- # for more details.
58
- #
59
- def global_context(data=nil)
60
- $stderr.puts "global_context() will be deprecated after v0.3.8, please see https://github.com/asenchi/scrolls for more information."
61
- if data
62
- Log.global_context = data
63
- else
64
- Log.global_context
65
- end
39
+ @log.with_context(data, &blk)
66
40
  end
67
41
 
68
- # Deprecated: Get or set a global context that prefixs all logs
69
- #
70
- # data - A hash of key/values to prepend to each log
71
- #
72
- # This method will be deprecated two releases after 0.3.8.
73
- # See https://github.com/asenchi/scrolls/releases/tag/v0.3.8
74
- # for more details.
42
+ # Public: Get the global context that prefixs all logs
75
43
  #
76
- def add_global_context(data)
77
- $stderr.puts "add_global_context will be deprecated after v0.3.8, please see https://github.com/asenchi/scrolls for more information."
78
- Log.add_global_context(data)
44
+ def global_context
45
+ @log.global_context
79
46
  end
80
47
 
81
48
  # Public: Log data and/or wrap a block with start/finish
@@ -96,26 +63,32 @@ module Scrolls
96
63
  # => nil
97
64
  #
98
65
  def log(data, &blk)
99
- Log.log(data, &blk)
66
+ # Allows us to call #log directly and initialize defaults
67
+ @log = Logger.new({}) unless @initialized
68
+
69
+ @log.log(data, &blk)
100
70
  end
101
71
 
102
72
  # Public: Log an exception
103
73
  #
104
- # data - A hash of key/values to log
105
74
  # e - An exception to pass to the logger
75
+ # data - A hash of key/values to log
106
76
  #
107
77
  # Examples:
108
78
  #
109
79
  # begin
110
80
  # raise Exception
111
81
  # rescue Exception => e
112
- # Scrolls.log_exception({test: "test"}, e)
82
+ # Scrolls.log_exception(e, {test: "test"})
113
83
  # end
114
84
  # test=test at=exception class=Exception message=Exception exception_id=70321999017240
115
85
  # ...
116
86
  #
117
- def log_exception(data, e)
118
- Log.log_exception(data, e)
87
+ def log_exception(e, data)
88
+ # Allows us to call #log directly and initialize defaults
89
+ @log = Logger.new({}) unless @initialized
90
+
91
+ @log.log_exception(e, data)
119
92
  end
120
93
 
121
94
  # Public: Setup a logging facility (default: Syslog::LOG_USER)
@@ -127,7 +100,7 @@ module Scrolls
127
100
  # Scrolls.facility = Syslog::LOG_LOCAL7
128
101
  #
129
102
  def facility=(f)
130
- Log.facility=(f)
103
+ @log.facility=(f)
131
104
  end
132
105
 
133
106
  # Public: Return the Syslog facility
@@ -138,7 +111,7 @@ module Scrolls
138
111
  # => 8
139
112
  #
140
113
  def facility
141
- Log.facility
114
+ @log.facility
142
115
  end
143
116
 
144
117
  # Public: Setup a new output (default: STDOUT)
@@ -154,7 +127,7 @@ module Scrolls
154
127
  # Scrolls.stream = StringIO.new
155
128
  #
156
129
  def stream=(out)
157
- Log.stream=(out)
130
+ @log.stream=(out)
158
131
  end
159
132
 
160
133
  # Public: Return the stream
@@ -165,7 +138,7 @@ module Scrolls
165
138
  # => #<IO:<STDOUT>>
166
139
  #
167
140
  def stream
168
- Log.stream
141
+ @log.stream
169
142
  end
170
143
 
171
144
  # Public: Set the time unit we use for 'elapsed' (default: "seconds")
@@ -177,7 +150,7 @@ module Scrolls
177
150
  # Scrolls.time_unit = "milliseconds"
178
151
  #
179
152
  def time_unit=(unit)
180
- Log.time_unit=(unit)
153
+ @log.time_unit = unit
181
154
  end
182
155
 
183
156
  # Public: Return the time unit currently configured
@@ -188,7 +161,7 @@ module Scrolls
188
161
  # => "seconds"
189
162
  #
190
163
  def time_unit
191
- Log.time_unit
164
+ @log.time_unit
192
165
  end
193
166
 
194
167
  # Public: Set whether to include a timestamp (now=<ISO8601>) field in the log
@@ -199,7 +172,7 @@ module Scrolls
199
172
  # Scrolls.add_timestamp = true
200
173
  #
201
174
  def add_timestamp=(boolean)
202
- Log.add_timestamp = boolean
175
+ @log.timestamp = boolean
203
176
  end
204
177
 
205
178
  # Public: Return whether the timestamp field will be included in the log
@@ -211,7 +184,7 @@ module Scrolls
211
184
  # => true
212
185
  #
213
186
  def add_timestamp
214
- Log.add_timestamp
187
+ @log.timestamp
215
188
  end
216
189
 
217
190
  # Public: Set whether exceptions should generate a single log
@@ -222,7 +195,7 @@ module Scrolls
222
195
  # Scrolls.single_line_exceptions = true
223
196
  #
224
197
  def single_line_exceptions=(boolean)
225
- Log.single_line_exceptions = boolean
198
+ @log.exceptions = boolean
226
199
  end
227
200
 
228
201
  # Public: Return whether exceptions generate a single log message.
@@ -233,7 +206,7 @@ module Scrolls
233
206
  # => true
234
207
  #
235
208
  def single_line_exceptions?
236
- Log.single_line_exceptions
209
+ @log.single_line_exceptions?
237
210
  end
238
211
 
239
212
  # Public: Convience method for Logger replacement
@@ -248,8 +221,8 @@ module Scrolls
248
221
  # => nil
249
222
  #
250
223
  def debug(data, &blk)
251
- data = data.merge(:level => "debug")
252
- Log.log(data, &blk)
224
+ data = data.merge(:level => "debug") if data.is_a?(Hash)
225
+ @log.log(data, &blk)
253
226
  end
254
227
 
255
228
  # Public: Convience method for Logger replacement
@@ -266,8 +239,8 @@ module Scrolls
266
239
  # => nil
267
240
  #
268
241
  def error(data, &blk)
269
- data = data.merge(:level => "warning")
270
- Log.log(data, &blk)
242
+ data = data.merge(:level => "warning") if data.is_a?(Hash)
243
+ @log.log(data, &blk)
271
244
  end
272
245
 
273
246
  # Public: Convience method for Logger replacement
@@ -284,8 +257,8 @@ module Scrolls
284
257
  # => nil
285
258
  #
286
259
  def fatal(data, &blk)
287
- data = data.merge(:level => "error")
288
- Log.log(data, &blk)
260
+ data = data.merge(:level => "error") if data.is_a?(Hash)
261
+ @log.log(data, &blk)
289
262
  end
290
263
 
291
264
  # Public: Convience method for Logger replacement
@@ -302,8 +275,8 @@ module Scrolls
302
275
  # => nil
303
276
  #
304
277
  def info(data, &blk)
305
- data = data.merge(:level => "info")
306
- Log.log(data, &blk)
278
+ data = data.merge(:level => "info") if data.is_a?(Hash)
279
+ @log.log(data, &blk)
307
280
  end
308
281
 
309
282
  # Public: Convience method for Logger replacement
@@ -320,8 +293,8 @@ module Scrolls
320
293
  # => nil
321
294
  #
322
295
  def warn(data, &blk)
323
- data = data.merge(:level => "notice")
324
- Log.log(data, &blk)
296
+ data = data.merge(:level => "notice") if data.is_a?(Hash)
297
+ @log.log(data, &blk)
325
298
  end
326
299
 
327
300
  # Public: Convience method for Logger replacement
@@ -338,8 +311,14 @@ module Scrolls
338
311
  # => nil
339
312
  #
340
313
  def unknown(data, &blk)
341
- data = data.merge(:level => "alert")
342
- Log.log(data, &blk)
314
+ data = data.merge(:level => "alert") if data.is_a?(Hash)
315
+ @log.log(data, &blk)
316
+ end
317
+
318
+ # Internal: The Logger initialized by #init
319
+ #
320
+ def internal
321
+ @log
343
322
  end
344
323
 
345
324
  end