tiny_log 1.1.0 → 2.1.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 +33 -9
- 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: 29bbda4af6bbfaa0b413cecb8f148633de78393f69168d4a287517573dbd3ee6
|
4
|
+
data.tar.gz: d4d85673432ea22a5f2a248c4d75497eafae8acd28a3068d294dbe69c1abeef9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81697f49eea9ed19a2ad733253e08c193cde95605ddaaab2c2e7e3c27fd83838f2dd7720a47d23e47ac3be9830da1730033d1c54f29041a12479b94171c70e5e
|
7
|
+
data.tar.gz: 3501862caab10809686c86c1be81ee9f55305852b74a699709275daa60071c118c4506446e863caac8438762f3fb3b100c17782176c45d3eb8c5679fe6a65b4f
|
data/lib/tiny_log.rb
CHANGED
@@ -7,7 +7,7 @@ require 'time'
|
|
7
7
|
# - the log message
|
8
8
|
#
|
9
9
|
# Ex:
|
10
|
-
# l =
|
10
|
+
# l = TinyLog.new
|
11
11
|
# l.erro('hi there')
|
12
12
|
# l.erro('hi there')
|
13
13
|
# 2022-11-18T01:26:37.086295Z 92967 ERRO hi there
|
@@ -16,26 +16,50 @@ 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
|
41
|
+
end
|
42
|
+
|
43
|
+
# flushes the underlying IO object; especially useful when buffering is
|
44
|
+
# enabled, or when you're exiting the application.
|
45
|
+
def flush
|
46
|
+
@io.flush
|
47
|
+
|
48
|
+
nil
|
26
49
|
end
|
27
50
|
|
28
51
|
# the clever bit that annotates the log message with a log level and UTC
|
29
52
|
# timestamp
|
30
53
|
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
|
-
|
54
|
+
msgs.each{|m| _build_lines(m).each{|l| @background_thread ? @msg_queue << l : @io.puts(l) } }
|
37
55
|
@io.flush unless @buffering
|
38
56
|
|
39
57
|
nil
|
40
58
|
end
|
59
|
+
|
60
|
+
def _build_lines(m)
|
61
|
+
m.to_s.lines.map do |l|
|
62
|
+
"#{Time.now.utc.iso8601(6)} #{Process.pid.to_s.rjust(6)} #{prefix.to_s.upcase} #{l}"
|
63
|
+
end
|
64
|
+
end
|
41
65
|
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.1.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-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: a tiny logger with almost no features
|
14
14
|
email: jefflunt@gmail.com
|