mongo_beautiful_logger 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5ffe8310478e9f97b296a0b367d4bdc69354e3fe22b549c0f7776695bcc449bd
4
+ data.tar.gz: 739e2a0047a1743ed4545a819437dcb4a71cbdda1a3d44e9cb3b9529dc934ae1
5
+ SHA512:
6
+ metadata.gz: 5ff6bd925008f5b36d431029f021e5f68dc4a0cfe980872aa9f674c640c2e6ecc4770881f17818b42371599520ca6999990ce6fa46c3bdd6a9ac808c6c67153d
7
+ data.tar.gz: 21cbb10c42f10f9d96470d44d7afb60ce3d5518a939a08fe47c2bd1be58d8ada24a795c13c08891db1c6e6dfe7c2d851070142c42f02f58e073a52d72605cbeb
@@ -0,0 +1,57 @@
1
+ require "mongo_beautiful_logger/colors"
2
+ require "mongo_beautiful_logger/mongo_actions"
3
+ require "logger"
4
+
5
+ class MongoBeautifulLogger
6
+ include MongoActions
7
+ include Colors
8
+
9
+ def initialize(logger = Logger.new($stdout))
10
+ @logger = format_logger(logger)
11
+ end
12
+
13
+ private
14
+
15
+ # filters out default logger prompt of:
16
+ # +d, [2020-06-20 14:02:29#17329] INFO -- MONGODB:+
17
+ def format_logger(logger)
18
+ logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}" }
19
+ logger
20
+ end
21
+
22
+ # define custom logger methods
23
+ # call formatting methods, and then send message string back to the logger instance
24
+ %w(debug info warn error fatal unknown).each do |level|
25
+ class_eval <<-RUBY
26
+ def #{level}(msg = nil, &block)
27
+ msg = colorize_log(msg)
28
+ msg = filter_unnecessary(msg)
29
+ @logger.#{level}(msg, &block)
30
+ end
31
+ RUBY
32
+ end
33
+
34
+ # colorize messages that are specified in ACTIONS constant
35
+ def colorize_log(msg)
36
+ ACTIONS.each { |a| msg = color(msg, a[:color]) if msg.downcase.include?(a[:match]) }
37
+ msg
38
+ end
39
+
40
+ # filter out any unnecessary messages
41
+ def filter_unnecessary(msg)
42
+ UNNECESSARY.any? { |s| msg.downcase.include? s } ? "" : "#{msg.sub(PREFIX_REGEX, "|")}\n"
43
+ end
44
+
45
+ # send all other methods back to logger instance
46
+ def method_missing(method, *args, &block)
47
+ @logger.send(method, *args, &block)
48
+ end
49
+
50
+ # set color based one the defined constants.
51
+ # If a third option is set to +true+, the string will be made bold.
52
+ # CLEAR to the is automatically appended to the string for reset
53
+ def color(text, color, bold = false)
54
+ bold = bold ? BOLD : ""
55
+ "#{bold}#{color}#{text}#{CLEAR}"
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ module Colors
2
+ # ANSI escape sequences for outputting colored logs
3
+ WHITE = "\e[37m"
4
+ CYAN = "\e[36m"
5
+ MAGENTA = "\e[35m"
6
+ BLUE = "\e[34m"
7
+ YELLOW = "\e[33m"
8
+ GREEN = "\e[32m"
9
+ RED = "\e[31m"
10
+ BLACK = "\e[30m"
11
+ BOLD = "\e[1m"
12
+ CLEAR = "\e[0m"
13
+ end
@@ -0,0 +1,27 @@
1
+ require "mongo_beautiful_logger/colors"
2
+
3
+ module MongoActions
4
+ include Colors
5
+
6
+ # substring matches and the corresponding colors for mongodb actions
7
+ FIND = { match: "\"find\"=>", color: BLUE }
8
+ UPDATE = { match: "\"update\"=>", color: YELLOW }
9
+ INSERT = { match: "\"insert\"=>", color: GREEN }
10
+ DELETE = { match: "\"delete\"=>", color: RED }
11
+ AGGREGATE = { match: "\"aggregate\"=>", color: MAGENTA }
12
+ SUCCEEDED = { match: "succeeded", color: GREEN }
13
+ FAILED = { match: "failed", color: RED }
14
+ ERROR = { match: "error", color: RED }
15
+ ENDSESSION = { match: "\"endsessions\"=>", color: YELLOW }
16
+ ACTIONS = [ FIND, UPDATE, INSERT, DELETE, AGGREGATE,
17
+ SUCCEEDED, FAILED, ERROR, ENDSESSION ]
18
+
19
+ # substring matches for unnecessary log messages that will be filtered out
20
+ UNNECESSARY = ["opology", "server description"]
21
+
22
+ # regex for for the log prefix that will be filtered out
23
+ # matches: +| localhost:27017 | app_test.update | STARTED |+
24
+ # note: +| STARTED |+ and +| SUCCEEDED |+ will be filtered due to redundancy
25
+ # but +| FAILED |+ and others will not
26
+ PREFIX_REGEX = /\|.*?\|.*?\|( (SUCCEEDED|STARTED) \|)?/
27
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_beautiful_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ibraheem Ahmed
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - ibrah1440@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/mongo_beautiful_logger.rb
49
+ - lib/mongo_beautiful_logger/colors.rb
50
+ - lib/mongo_beautiful_logger/mongo_actions.rb
51
+ homepage: https://github.com/redline-gh/mongo_beautiful_logger
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.0.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: A simple and beautiful logger for MongoDB/Mongoid in you Ruby/Rails app.
74
+ test_files: []