mongo_beautiful_logger 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ffe8310478e9f97b296a0b367d4bdc69354e3fe22b549c0f7776695bcc449bd
4
- data.tar.gz: 739e2a0047a1743ed4545a819437dcb4a71cbdda1a3d44e9cb3b9529dc934ae1
3
+ metadata.gz: 4c58868b69432a45f1f40e031eeb8d60e88c33dae37aa48715475608f01afc53
4
+ data.tar.gz: e063316c20d3461c5957afcae9b52eaea496445138cf4c7fc78de5e912387abe
5
5
  SHA512:
6
- metadata.gz: 5ff6bd925008f5b36d431029f021e5f68dc4a0cfe980872aa9f674c640c2e6ecc4770881f17818b42371599520ca6999990ce6fa46c3bdd6a9ac808c6c67153d
7
- data.tar.gz: 21cbb10c42f10f9d96470d44d7afb60ce3d5518a939a08fe47c2bd1be58d8ada24a795c13c08891db1c6e6dfe7c2d851070142c42f02f58e073a52d72605cbeb
6
+ metadata.gz: 7a7e2cdd141db663be2969779eab22f9eee4081bc4d3b0abcb22d4f92484ba00b01d7ebbf3f50e89d9ec1c52b33591e4c4e9ddd440ae03ab407190627839b59d
7
+ data.tar.gz: de6694d1250d2459fa7762efdd85e751fa189760cbdad77a3d534767fde12a1dbd4a391d20d0030cd0c45539e4c9a059862c82776789fe3d401232972d8843a6
@@ -6,45 +6,65 @@ class MongoBeautifulLogger
6
6
  include MongoActions
7
7
  include Colors
8
8
 
9
- def initialize(logger = Logger.new($stdout))
10
- @logger = format_logger(logger)
9
+ def initialize(*targets)
10
+ nil_targets_error if targets.empty?
11
+ @targets = targets.map do |t|
12
+ logger = Logger.new(t)
13
+ format! logger
14
+ logger
15
+ end
11
16
  end
12
17
 
13
18
  private
14
19
 
15
- # filters out default logger prompt of:
20
+ def nil_targets_error
21
+ raise ArgumentError.new "wrong number of arguments (given 0, expected at least 1)"
22
+ end
23
+
24
+ # default logger format, removes prompt of:
16
25
  # +d, [2020-06-20 14:02:29#17329] INFO -- MONGODB:+
17
- def format_logger(logger)
26
+ def format!(logger)
18
27
  logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}" }
19
- logger
20
28
  end
21
29
 
22
30
  # define custom logger methods
23
- # call formatting methods, and then send message string back to the logger instance
31
+ # formats log message and then sends message back to the logger instance
24
32
  %w(debug info warn error fatal unknown).each do |level|
25
33
  class_eval <<-RUBY
26
34
  def #{level}(msg = nil, &block)
27
- msg = colorize_log(msg)
28
- msg = filter_unnecessary(msg)
29
- @logger.#{level}(msg, &block)
35
+ return if unnecessary?(msg)
36
+ msg = format_log(msg)
37
+ @targets.each { |t| t.#{level}(msg, &block) }; nil
30
38
  end
31
39
  RUBY
32
40
  end
33
41
 
42
+ # checks if a message if a message is included in the +UNNECESSARY+ array constant
43
+ def unnecessary?(msg)
44
+ UNNECESSARY.any? { |s| msg.downcase.include? s }
45
+ end
46
+
47
+ # takes a log message and returns the message formatted
48
+ def format_log(msg)
49
+ msg = colorize_log(msg)
50
+ msg = remove_prefix(msg)
51
+ "#{msg}\n"
52
+ end
53
+
34
54
  # colorize messages that are specified in ACTIONS constant
35
55
  def colorize_log(msg)
36
56
  ACTIONS.each { |a| msg = color(msg, a[:color]) if msg.downcase.include?(a[:match]) }
37
- msg
57
+ return msg
38
58
  end
39
59
 
40
- # filter out any unnecessary messages
41
- def filter_unnecessary(msg)
42
- UNNECESSARY.any? { |s| msg.downcase.include? s } ? "" : "#{msg.sub(PREFIX_REGEX, "|")}\n"
60
+ # remove prefix defined in +PREFIX_REGEX+ from message
61
+ def remove_prefix(msg)
62
+ msg.sub(PREFIX_REGEX, "|")
43
63
  end
44
64
 
45
65
  # send all other methods back to logger instance
46
66
  def method_missing(method, *args, &block)
47
- @logger.send(method, *args, &block)
67
+ @targets.each { |t| t.send(method, *args, &block) }
48
68
  end
49
69
 
50
70
  # set color based one the defined constants.
@@ -1,5 +1,6 @@
1
+ # ANSI escape sequences for outputting colored logs
2
+
1
3
  module Colors
2
- # ANSI escape sequences for outputting colored logs
3
4
  WHITE = "\e[37m"
4
5
  CYAN = "\e[36m"
5
6
  MAGENTA = "\e[35m"
@@ -13,15 +13,16 @@ module MongoActions
13
13
  FAILED = { match: "failed", color: RED }
14
14
  ERROR = { match: "error", color: RED }
15
15
  ENDSESSION = { match: "\"endsessions\"=>", color: YELLOW }
16
+ INITIALIZING = { match: "initializing", color: GREEN }
16
17
  ACTIONS = [ FIND, UPDATE, INSERT, DELETE, AGGREGATE,
17
- SUCCEEDED, FAILED, ERROR, ENDSESSION ]
18
+ SUCCEEDED, FAILED, ERROR, ENDSESSION, INITIALIZING ]
18
19
 
19
20
  # substring matches for unnecessary log messages that will be filtered out
20
21
  UNNECESSARY = ["opology", "server description"]
21
22
 
22
23
  # regex for for the log prefix that will be filtered out
23
24
  # matches: +| localhost:27017 | app_test.update | STARTED |+
24
- # note: +| STARTED |+ and +| SUCCEEDED |+ will be filtered due to redundancy
25
+ # note: +| STARTED |+ and +| SUCCEEDED |+ will be filtered due to redundancy,
25
26
  # but +| FAILED |+ and others will not
26
27
  PREFIX_REGEX = /\|.*?\|.*?\|( (SUCCEEDED|STARTED) \|)?/
27
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_beautiful_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ibraheem Ahmed
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-12 00:00:00.000000000 Z
11
+ date: 2020-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -48,7 +48,7 @@ files:
48
48
  - lib/mongo_beautiful_logger.rb
49
49
  - lib/mongo_beautiful_logger/colors.rb
50
50
  - lib/mongo_beautiful_logger/mongo_actions.rb
51
- homepage: https://github.com/redline-gh/mongo_beautiful_logger
51
+ homepage: https://github.com/ibraheemdev/mongo_beautiful_logger
52
52
  licenses:
53
53
  - MIT
54
54
  metadata: {}