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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5ed542d2cf2c1f5c60ace389fc31b6a1193f3b4
4
- data.tar.gz: 56739345db59ef3d3f9099da0b5a1690a6f24a4f
3
+ metadata.gz: 03e831d9e6274fc66ee8ea2b21ee5c587ca15873
4
+ data.tar.gz: 6683cc58b620f25cd70fa7c0eaff2bbd862503ac
5
5
  SHA512:
6
- metadata.gz: 9f8e21cfb001c4b183cffc44037fc65958b0cdb350bac281c18524b86ad6029c4da9caf18ed79953b5da88a60bf2917c3091fe2ae4addc8be25ecf0f25730199
7
- data.tar.gz: 83593613546ba256221a364cb5dece3c7605eca8b0e1b17a4b676933a3a91dfb5ef03debd6fc5c94de6ed5fd424ff5f695f415b2d093bbf5425c12de97c9e1fc
6
+ metadata.gz: 9d825450fe6a0272312f35a0220b6f88f92f1bfec062a9ad0a1612f5a11e68cacf3acabc8fc2e40748513f2976a94277047b83043f23c6a253a9f50aa0f232b4
7
+ data.tar.gz: 7a639eb992efe00f05a819c010a233e5b52fd19fbf086386e134ff85a30264b8803a80ab6fc1fa482510c227e3d82b1e3814d4a3fc7a69e46990543c5f4db71f
data/.rubocop.yml ADDED
@@ -0,0 +1,3 @@
1
+ Metrics/MethodLength:
2
+ CountComments: false # count full line comments?
3
+ Max: 15
data/lib/rumbrl/env.rb CHANGED
@@ -17,8 +17,12 @@ module Rumbrl
17
17
  ENV.fetch('LOG_TIME_FORMAT', '[%F %T %z]')
18
18
  end
19
19
 
20
- def self.log_format
20
+ def self.data_format
21
21
  ENV.fetch('LOG_DATA_FORMAT', '[%s] [%s]')
22
22
  end
23
+
24
+ def self.log_format
25
+ ENV.fetch('LOG_FORMAT', nil)
26
+ end
23
27
  end
24
28
  end
@@ -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
@@ -2,5 +2,5 @@
2
2
 
3
3
  # version const for gem
4
4
  module Rumbrl
5
- VERSION = '0.0.1'
5
+ VERSION = '0.1.0'
6
6
  end
@@ -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
- allow(logger_double).to receive(:level=)
9
- allow(logger_double).to receive(:datetime_format=)
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 'does things woo' do
42
- expect(logger_double).to receive(:info)
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.1
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