tiny_log 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tiny_log.rb +24 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e5e2cc1eee03fc86a34571b2fb6e55a080162bb3ac6b9598bbb34f0ef491afb
|
4
|
+
data.tar.gz: b68b82266ca779f4b83e700ca1e13314255a5ad481951b37d54a406c1aa715ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d110256b17ab42ebf1d4acde5dd10c981ad7889245dd37ae0e38e4b890e5a46a3224459e87491aac210ea7de88ff0979e4f14c21bc18e74db1659afe33febf82
|
7
|
+
data.tar.gz: 00c580ecfdecb2a8882264ed92b8d4f1cc80631fa9e0a3648eedd6bae9581257d0148110f116c757c50857dfe3249dfd10472c64d312e6b55606065527a7cb3f
|
data/lib/tiny_log.rb
CHANGED
@@ -16,26 +16,42 @@ class TinyLog
|
|
16
16
|
# filename: the I/O stream to send log messages to
|
17
17
|
# if unspecified, will default to $stdout
|
18
18
|
# if specified, attempts to open a file with the specified name to append to
|
19
|
-
#
|
19
|
+
# buffering: whether or not to buffer log output
|
20
20
|
# true, or not specified: log buffering is enabled
|
21
21
|
# false: log buffering is disabled - most useful for dev/test environments
|
22
22
|
# where you want to watch output in realtime
|
23
|
-
|
23
|
+
# background_thread: whether logging should be done in a background Thread
|
24
|
+
# true, or not specified, use a Thread to write to the log every 5 seconds
|
25
|
+
# false, write to the log immediately
|
26
|
+
def initialize(filename: nil, buffering: true, background_thread: true)
|
24
27
|
@buffering = !!buffering
|
25
28
|
@io = filename.is_a?(String) ? File.open(filename, 'a') : $stdout
|
29
|
+
|
30
|
+
if background_thread
|
31
|
+
@msg_queue = Queue.new
|
32
|
+
@background_thread = Thread.new do |t|
|
33
|
+
loop do
|
34
|
+
@msg_queue
|
35
|
+
.length
|
36
|
+
.times{ @io.puts @msg_queue.shift }
|
37
|
+
sleep 5
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
26
41
|
end
|
27
42
|
|
28
43
|
# the clever bit that annotates the log message with a log level and UTC
|
29
44
|
# timestamp
|
30
45
|
def method_missing(prefix, *msgs)
|
31
|
-
msgs.each
|
32
|
-
m.to_s.lines.each do |l|
|
33
|
-
@io.puts "#{Time.now.utc.iso8601(6)} #{Process.pid.to_s.rjust(6)} #{prefix.to_s.upcase} #{l}"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
46
|
+
msgs.each{|m| _build_lines(m).each{|l| @background_thread ? @msg_queue << l : @io.puts(l) } }
|
37
47
|
@io.flush unless @buffering
|
38
48
|
|
39
49
|
nil
|
40
50
|
end
|
51
|
+
|
52
|
+
def _build_lines(m)
|
53
|
+
m.to_s.lines.map do |l|
|
54
|
+
"#{Time.now.utc.iso8601(6)} #{Process.pid.to_s.rjust(6)} #{prefix.to_s.upcase} #{l}"
|
55
|
+
end
|
56
|
+
end
|
41
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Lunt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: a tiny logger with almost no features
|
14
14
|
email: jefflunt@gmail.com
|