pretty_logger 1.1.4 → 1.2.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: de26eada3a4365d89821cd2097a08193907e4e423396df9ca5f6ed3e5ef513dc
4
- data.tar.gz: 0a9c56ace96ab075c1792ef01a4149a50c599eb069b5781669f714026c3678c7
3
+ metadata.gz: 2dffc0a00043f3c8cdd85dbf8357c14056635577225f2e85d608e4f8cdcd62e0
4
+ data.tar.gz: ba73d0d550463108a0d0f8d1a049bb6cb8a97a7b708d3569b46be3bd7da9431e
5
5
  SHA512:
6
- metadata.gz: 30848208db020360ac6612fc0b9410c6d281ba67d6abe41a2c62a06252f3f3f05d46a30aa6d9950507c2936fbed2bb09387a6e8e2b29569f832a1569a2deb405
7
- data.tar.gz: 2afefaec6fcabfa73d41e8542aa04eddbce676ebf30d152bdfe413d75e5b4e51ff24ced2218ebd4918895fc0712c574549ef4860c6182efa53716d3f203aa4a3
6
+ metadata.gz: 746a7712134dc57153a6fa6ddca97a42bb5f2be6aeb79df7340903ef100f584331cc5d0c16127eaeafb75c62ee78392871f98b1278f6cfdf9ff332d42756aa51
7
+ data.tar.gz: 2cb1701efb5d9c66d44727aed1074c566253902336a4c03c0e921c61b297c20a92d2b785b988fa2c592f5a45a57fb28de5a27c1987356cdacc83b0cd69342c51
data/README.md CHANGED
@@ -29,6 +29,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
29
29
  - [ ] Add a config that can be set in an initializer for accessing user and prettifying in logs
30
30
  - [ ] Customize location of custom_log file
31
31
  - [ ] Don't auto-hijack errors, should need to add that manually?
32
+ - [ ] Don't require Rails- should be able to include in a basic Ruby project just as easily
32
33
 
33
34
  ## License
34
35
 
@@ -0,0 +1,121 @@
1
+ module PrettyLogger
2
+ module_function
3
+
4
+ def instance
5
+ @instance ||= ::ActiveSupport::Logger.new("log/custom.log")
6
+ end
7
+
8
+ def timestamp
9
+ Time.current.in_time_zone("Mountain Time (US & Canada)").strftime("[%b %d, %I:%M:%S%P]")
10
+ end
11
+
12
+ def pretty_message(obj)
13
+ return obj if obj.is_a?(::String)
14
+
15
+ ::CodeRay.scan(obj, :ruby).terminal.gsub(
16
+ /\e\[36m:(\w+)\e\[0m=>/i, ("\e[36m" + '\1: ' + "\e[0m") # hashrocket(sym) to colon(sym)
17
+ ).gsub(
18
+ /\e\[0m=>/, "\e[0m: " # all hashrockets to colons
19
+ )
20
+ end
21
+
22
+ def debug(*messages)
23
+ instance.debug("\e[90m#{timestamp}\e[90m[DEBUG]\e[0m " + messages.map { |m| pretty_message(m) }.join("\n"))
24
+ end
25
+
26
+ def info(*messages)
27
+ instance.info("\e[90m#{timestamp}\e[36m[INFO]\e[0m " + messages.map { |m| pretty_message(m) }.join("\n"))
28
+ end
29
+
30
+ def warn(*messages)
31
+ instance.warn("\e[90m#{timestamp}\e[38;5;208m[WARN]\e[0m " + messages.map { |m| pretty_message(m) }.join("\n"))
32
+ end
33
+
34
+ def error(*messages)
35
+ instance.error("\e[90m#{timestamp}\e[31m[ERROR]\e[0m " + messages.map { |m| pretty_message(m) }.join("\n"))
36
+ end
37
+
38
+ def cl
39
+ "\033[0m"
40
+ end
41
+
42
+ def rgb(r, g, b)
43
+ "\033[38;2;#{r};#{g};#{b}m"
44
+ end
45
+
46
+ def colorize(name, text)
47
+ return "" if text.blank?
48
+ "#{colors[name]}#{text}#{cl}"
49
+ end
50
+
51
+ def colors
52
+ {
53
+ black: rgb(0, 0, 0),
54
+ white: rgb(255, 255, 255),
55
+ lime: rgb(0, 255, 0),
56
+ red: rgb(255, 0, 0),
57
+ blue: rgb(0, 0, 255),
58
+ yellow: rgb(255, 255, 0),
59
+ cyan: rgb(0, 255, 255),
60
+ magenta: rgb(255, 0, 255),
61
+ gold: rgb(218, 165, 32),
62
+ silver: rgb(192, 192, 192),
63
+ grey: rgb(150, 150, 150),
64
+ maroon: rgb(128, 0, 0),
65
+ olive: rgb(128, 128, 0),
66
+ green: rgb(0, 128, 0),
67
+ purple: rgb(128, 0, 128),
68
+ teal: rgb(0, 128, 128),
69
+ navy: rgb(0, 0, 128),
70
+ rocco: rgb(1, 96, 255),
71
+ orange: rgb(255, 150, 0),
72
+ pink: rgb(255, 150, 150),
73
+ }
74
+ end
75
+
76
+ def focused_backtrace(trace)
77
+ return [] unless trace
78
+ trace.select { |line|
79
+ line.include?("/app/")
80
+ }.map { |line|
81
+ line.gsub(/^.*?#{Rails.root}/, "").gsub(/(app)?\/app\//, "app/").gsub(":in `", " `").gsub(/(:\d+) .*?$/, '\1')
82
+ }
83
+ end
84
+
85
+ def clean_message(message)
86
+ message.gsub(/\#\<([\w\:]+)( id: \d+)?.*?\>\n/im) { |found|
87
+ "#<#{Regexp.last_match(1)}#{Regexp.last_match(2)}>\n"
88
+ }
89
+ end
90
+
91
+ def truncate(input, max_visible_length=2000, with: "...")
92
+ full = input.length
93
+ return input if full <= max_visible_length
94
+
95
+ clean = input.gsub(/\e\[[\d;]*[a-z]/i, "").length
96
+ return input if clean <= max_visible_length
97
+
98
+ max_visible_length -= with.gsub(/\e\[[\d;]*[a-z]/i, "").length
99
+
100
+ visible_length = 0
101
+ truncated_string = ""
102
+
103
+ input.scan(/(\e\[[\d;]*[a-z]|[^\e]+)/i) do |match|
104
+ part = match[0]
105
+ if part.match?(/\e\[[\d;]*[a-z]/i) # Match ANSI escape sequences
106
+ truncated_string += part # Do not add to the count
107
+ else
108
+ part.each_char do |char|
109
+ if visible_length < max_visible_length
110
+ truncated_string += char
111
+ visible_length += 1
112
+ else
113
+ break
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ "#{truncated_string}#{with}"
120
+ end
121
+ end
@@ -1,4 +1,5 @@
1
- class PrettyLogger::RequestLogger < PrettyLogger::BaseLogger
1
+ class PrettyLogger::RequestLogger
2
+ include PrettyLogger
2
3
  attr_accessor :request, :current_user
3
4
 
4
5
  def initialize(request:nil, current_user:nil)
@@ -7,17 +8,16 @@ class PrettyLogger::RequestLogger < PrettyLogger::BaseLogger
7
8
  end
8
9
 
9
10
  def log_request(extra_text=nil)
10
- # Nothing change
11
- ::PrettyLogger::BaseLogger.info([
11
+ info([
12
12
  "#{pretty_user} #{request.method.upcase} #{request.path} #{extra_text}",
13
- params.blank? ? nil : ::PrettyLogger::BaseLogger.pretty_message(params).truncate(2000),
13
+ params.blank? ? nil : truncate(pretty_message(params)),
14
14
  ].compact.join("\n"))
15
15
  end
16
16
 
17
17
  def log_error(exception)
18
- ::PrettyLogger::BaseLogger.error([
18
+ error([
19
19
  "#{pretty_user} #{request.path} #{colorize(:red, exception.class)}",
20
- params.blank? ? nil : ::PrettyLogger::BaseLogger.pretty_message(params).truncate(2000),
20
+ params.blank? ? nil : truncate(pretty_message(params)),
21
21
  colorize(:red, focused_backtrace(exception.backtrace).first),
22
22
  colorize(:red, clean_message("#{exception.class} #{exception.message}")),
23
23
  ].compact.join("\n"))
@@ -1,3 +1,3 @@
1
- module PrettyLogger
2
- VERSION = "1.1.4"
1
+ class PrettyLogger
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/pretty_logger.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "pretty_logger/version"
4
+ require_relative "pretty_logger/pretty_logger"
4
5
  require_relative "pretty_logger/override_console_colors"
5
- require_relative "pretty_logger/base_logger"
6
6
  require_relative "pretty_logger/request_logger"
7
7
  require_relative "pretty_logger/controller_methods"
8
8
  require_relative "pretty_logger/railtie" if defined?(Rails)
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/pretty_logger/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pretty_logger"
7
+ spec.version = PrettyLogger::VERSION
8
+ spec.authors = ["Rocco Nicholls"]
9
+ spec.email = ["rocco11nicholls@gmail.com"]
10
+
11
+ spec.summary = "Quick logger that lets you output to a different log file for more controlled breakdowns of site visits."
12
+ spec.description = "Quick logger that lets you output to a different log file for more controlled breakdowns of site visits"
13
+ spec.homepage = "https://github.com/Rockster160/pretty_logger"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/Rockster160/pretty_logger"
19
+ spec.metadata["changelog_uri"] = "https://github.com/Rockster160/pretty_logger/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
24
+ f.match(%r{\A(?:bin/|test/|spec/|features/|\.git|\.circleci/|appveyor\.yml)})
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ spec.add_dependency "coderay", "~> 1.1.3"
32
+
33
+ # For more information and examples about making a new gem, check out our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rocco Nicholls
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-13 00:00:00.000000000 Z
11
+ date: 2024-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coderay
@@ -41,12 +41,13 @@ files:
41
41
  - README.md
42
42
  - Rakefile
43
43
  - lib/pretty_logger.rb
44
- - lib/pretty_logger/base_logger.rb
45
44
  - lib/pretty_logger/controller_methods.rb
46
45
  - lib/pretty_logger/override_console_colors.rb
46
+ - lib/pretty_logger/pretty_logger.rb
47
47
  - lib/pretty_logger/railtie.rb
48
48
  - lib/pretty_logger/request_logger.rb
49
49
  - lib/pretty_logger/version.rb
50
+ - pretty_logger.gemspec
50
51
  - sig/pretty_logger.rbs
51
52
  homepage: https://github.com/Rockster160/pretty_logger
52
53
  licenses:
@@ -1,95 +0,0 @@
1
- # debug
2
- # info
3
- # warn
4
- # error
5
-
6
- class PrettyLogger::BaseLogger < ActiveSupport::Logger
7
- class << self
8
- def instance
9
- @instance ||= new("log/custom.log")
10
- end
11
-
12
- def timestamp
13
- Time.current.in_time_zone("Mountain Time (US & Canada)").strftime("[%b %d, %I:%M:%S%P]")
14
- end
15
-
16
- def pretty_message(obj)
17
- return obj if obj.is_a?(::String)
18
-
19
- ::CodeRay.scan(obj, :ruby).terminal.gsub(
20
- /\e\[36m:([a-z]+)\e\[0m=>/i, ("\e[36m" + '\1: ')
21
- ).gsub(
22
- /\e\[0m=>/, "\e[0m: "
23
- )
24
- end
25
-
26
- def debug(message)
27
- instance.debug("\e[90m#{timestamp}\e[90m[DEBUG]\e[0m #{pretty_message(message)}")
28
- end
29
-
30
- def info(message)
31
- instance.info("\e[90m#{timestamp}\e[36m[INFO]\e[0m #{pretty_message(message)}")
32
- end
33
-
34
- def warn(message)
35
- instance.warn("\e[90m#{timestamp}\e[38;5;208m[WARN]\e[0m #{pretty_message(message)}")
36
- end
37
-
38
- def error(message)
39
- instance.error("\e[90m#{timestamp}\e[31m[ERROR]\e[0m #{pretty_message(message)}")
40
- end
41
- end
42
-
43
- def cl
44
- "\033[0m"
45
- end
46
-
47
- def rgb(r, g, b)
48
- "\033[38;2;#{r};#{g};#{b}m"
49
- end
50
-
51
- def colorize(name, text)
52
- return "" if text.blank?
53
- "#{colors[name]}#{text}#{cl}"
54
- end
55
-
56
- def colors
57
- {
58
- black: rgb(0, 0, 0),
59
- white: rgb(255, 255, 255),
60
- lime: rgb(0, 255, 0),
61
- red: rgb(255, 0, 0),
62
- blue: rgb(0, 0, 255),
63
- yellow: rgb(255, 255, 0),
64
- cyan: rgb(0, 255, 255),
65
- magenta: rgb(255, 0, 255),
66
- gold: rgb(218, 165, 32),
67
- silver: rgb(192, 192, 192),
68
- grey: rgb(150, 150, 150),
69
- maroon: rgb(128, 0, 0),
70
- olive: rgb(128, 128, 0),
71
- green: rgb(0, 128, 0),
72
- purple: rgb(128, 0, 128),
73
- teal: rgb(0, 128, 128),
74
- navy: rgb(0, 0, 128),
75
- rocco: rgb(1, 96, 255),
76
- orange: rgb(255, 150, 0),
77
- pink: rgb(255, 150, 150),
78
- }
79
- end
80
-
81
- def focused_backtrace(trace)
82
- return [] unless trace
83
- trace.select { |line|
84
- line.include?("/app/")
85
- }.map { |line|
86
- line.gsub(/^.*?#{Rails.root}/, "").gsub(/(app)?\/app\//, "app/").gsub(":in `", " `").gsub(/(:\d+) .*?$/, '\1')
87
- }
88
- end
89
-
90
- def clean_message(message)
91
- message.gsub(/\#\<([\w\:]+)( id: \d+)?.*?\>\n/im) { |found|
92
- "#<#{Regexp.last_match(1)}#{Regexp.last_match(2)}>\n"
93
- }
94
- end
95
- end