rumbrl 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/lib/rumbrl/env.rb +5 -1
- data/lib/rumbrl/factory.rb +3 -2
- data/lib/rumbrl/log.rb +24 -1
- data/lib/rumbrl/version.rb +1 -1
- data/spec/rumbrl/log_spec.rb +43 -6
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03e831d9e6274fc66ee8ea2b21ee5c587ca15873
|
4
|
+
data.tar.gz: 6683cc58b620f25cd70fa7c0eaff2bbd862503ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d825450fe6a0272312f35a0220b6f88f92f1bfec062a9ad0a1612f5a11e68cacf3acabc8fc2e40748513f2976a94277047b83043f23c6a253a9f50aa0f232b4
|
7
|
+
data.tar.gz: 7a639eb992efe00f05a819c010a233e5b52fd19fbf086386e134ff85a30264b8803a80ab6fc1fa482510c227e3d82b1e3814d4a3fc7a69e46990543c5f4db71f
|
data/.rubocop.yml
ADDED
data/lib/rumbrl/env.rb
CHANGED
data/lib/rumbrl/factory.rb
CHANGED
@@ -6,7 +6,7 @@ module Rumbrl
|
|
6
6
|
# broken regulations & underaged workers abound
|
7
7
|
class Factory
|
8
8
|
attr_reader :dest, :name
|
9
|
-
attr_accessor :size, :age, :level, :log_format, :time_format
|
9
|
+
attr_accessor :size, :age, :level, :log_format, :data_format, :time_format
|
10
10
|
|
11
11
|
def self.create(nm, path: nil, age: nil, size: nil, level: nil)
|
12
12
|
new(nm, path: path, age: age, size: size, level: level).generate
|
@@ -15,6 +15,7 @@ module Rumbrl
|
|
15
15
|
def initialize(nm, path: nil, age: nil, size: nil, level: nil)
|
16
16
|
@name = nm
|
17
17
|
@log_format = Env.log_format
|
18
|
+
@data_format = Env.data_format
|
18
19
|
@time_format = Env.time_format
|
19
20
|
@size ||= (size || Env.shift_size)
|
20
21
|
@age ||= (age || Env.shift_age)
|
@@ -30,7 +31,7 @@ module Rumbrl
|
|
30
31
|
private
|
31
32
|
|
32
33
|
def generate_logger
|
33
|
-
logger = Log.new(dest, age, size, log_format)
|
34
|
+
logger = Log.new(dest, age, size, data_format, log_format)
|
34
35
|
logger.level = level
|
35
36
|
logger.datetime_format = time_format
|
36
37
|
logger
|
data/lib/rumbrl/log.rb
CHANGED
@@ -11,6 +11,7 @@ module Rumbrl
|
|
11
11
|
attr_reader :logger, :data_format
|
12
12
|
|
13
13
|
def_delegators :logger,
|
14
|
+
:datetime_format,
|
14
15
|
:datetime_format=,
|
15
16
|
:level=,
|
16
17
|
:log,
|
@@ -19,9 +20,11 @@ module Rumbrl
|
|
19
20
|
:fatal?,
|
20
21
|
:info?
|
21
22
|
|
22
|
-
def initialize(path, age, size, data_format)
|
23
|
+
def initialize(path, age, size, data_format, log_format = nil)
|
23
24
|
@logger = ::Logger.new(log_file(path), shift_age: age, shift_size: size)
|
24
25
|
@data_format = data_format
|
26
|
+
|
27
|
+
@logger.formatter = log_formatter(log_format) if log_format
|
25
28
|
end
|
26
29
|
|
27
30
|
def method_missing(name, *args)
|
@@ -45,5 +48,25 @@ module Rumbrl
|
|
45
48
|
def format_message(message, data)
|
46
49
|
sprintf @data_format, message, data
|
47
50
|
end
|
51
|
+
|
52
|
+
def log_formatter(log_format)
|
53
|
+
proc do |severity, datetime, progname, message|
|
54
|
+
datetime = datetime.strftime(datetime_format) if datetime_format
|
55
|
+
|
56
|
+
values = {
|
57
|
+
severity: severity,
|
58
|
+
datetime: datetime,
|
59
|
+
pid: Process.pid,
|
60
|
+
progname: progname,
|
61
|
+
message: message
|
62
|
+
}
|
63
|
+
|
64
|
+
values.each do |k, v|
|
65
|
+
log_format = log_format.gsub(/%#{k.to_s}%/, v.to_s)
|
66
|
+
end
|
67
|
+
|
68
|
+
"#{log_format}\n"
|
69
|
+
end
|
70
|
+
end
|
48
71
|
end
|
49
72
|
end
|
data/lib/rumbrl/version.rb
CHANGED
data/spec/rumbrl/log_spec.rb
CHANGED
@@ -1,13 +1,18 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
|
+
# message is a regex string
|
4
|
+
def default_log_string(message)
|
5
|
+
datetime = Time.now.strftime('%Y-%m-%dT%H:%M:%S.')
|
6
|
+
message = 'message \["data"\]'
|
7
|
+
/I, \[#{datetime}[0-9]+ ##{Process.pid}\] INFO -- : #{message}\n/
|
8
|
+
end
|
9
|
+
|
3
10
|
describe Rumbrl::Log do
|
4
11
|
let(:log) { Rumbrl::Log.new 'path/to/log', 'weekly', 123, '%s %s' }
|
5
|
-
let!(:logger_double) { double }
|
6
12
|
|
7
13
|
before(:each) do
|
8
|
-
|
9
|
-
|
10
|
-
allow(::Logger).to receive(:new).and_return logger_double
|
14
|
+
allow_any_instance_of(Logger::LogDevice).to receive(:write)
|
15
|
+
|
11
16
|
# TODO: make this...less hacky
|
12
17
|
allow(::File).to receive(:open).and_return(STDOUT)
|
13
18
|
end
|
@@ -38,10 +43,42 @@ describe Rumbrl::Log do
|
|
38
43
|
.to raise_error(RuntimeError, /message and data/)
|
39
44
|
end
|
40
45
|
|
41
|
-
it '
|
42
|
-
|
46
|
+
it 'logs correctly using default log_format' do
|
47
|
+
expect_any_instance_of(Logger::LogDevice)
|
48
|
+
.to receive(:write).with(default_log_string('message \["data"\]'))
|
49
|
+
|
50
|
+
log.info('message', ['data'])
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'logs correctly using custom log_format' do
|
54
|
+
log = Rumbrl::Log.new('path/to/log', 'weekly', 123,
|
55
|
+
'%s %s', '%severity% %message%')
|
56
|
+
|
57
|
+
expected_string = "INFO message [\"data\"]\n"
|
58
|
+
|
59
|
+
expect_any_instance_of(Logger::LogDevice)
|
60
|
+
.to receive(:write).with(expected_string)
|
61
|
+
|
43
62
|
log.info('message', ['data'])
|
44
63
|
end
|
64
|
+
|
65
|
+
it 'logs correctly with custom datetime and custom log_format' do
|
66
|
+
allow(ENV).to receive(:fetch).and_call_original
|
67
|
+
allow(ENV).to receive(:fetch)
|
68
|
+
.with('LOG_TIME_FORMAT', anything).and_return('[%F]')
|
69
|
+
allow(ENV).to receive(:fetch)
|
70
|
+
.with('LOG_FORMAT', anything).and_return('%datetime% %message%')
|
71
|
+
|
72
|
+
log = Rumbrl::Factory.create('STDOUT')
|
73
|
+
|
74
|
+
datetime = Time.now.strftime('%Y-%m-%d')
|
75
|
+
expected_string = "[#{datetime}] [message] [data]\n"
|
76
|
+
|
77
|
+
expect_any_instance_of(Logger::LogDevice)
|
78
|
+
.to receive(:write).with(expected_string)
|
79
|
+
|
80
|
+
log.info('message', 'data')
|
81
|
+
end
|
45
82
|
end
|
46
83
|
end
|
47
84
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rumbrl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- chr0n1x
|
@@ -88,6 +88,7 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- .gitignore
|
91
|
+
- .rubocop.yml
|
91
92
|
- .ruby-version
|
92
93
|
- .singularityrc
|
93
94
|
- Gemfile
|