pretty_logger 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8011a8e32da441f9f61037dd6610a912085f0e88a9ecfd89fe1c223c91fc304c
4
+ data.tar.gz: 730b3399d1e55c2554672bfe680612e752f934d4afd48731b91437fdc5da9642
5
+ SHA512:
6
+ metadata.gz: 0acdcda71d20029e545996736ad7187576c259842a87bc2ae2850dba8e01905e6dc4e578fada5c744f4ddf5eb356c18d714d310851a5695046da662979957b89
7
+ data.tar.gz: 1600cf3f467bff769e99842571a7eae327c5b5c3fc7530b3bf6173a74be434408452be093f0a6acc3a347c06fb25594478690592550cb85754bea778bf88e902
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-05-13
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at rocco11nicholls@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pretty_logger.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Rocco Nicholls
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # PrettyLogger
2
+
3
+ Quick and simple Rails logger that lets you output to a different log file for more controlled breakdowns of site visits.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add pretty_logger
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install pretty_logger
14
+
15
+ ## Usage
16
+
17
+ Add `before_action :pretty_logit` to your ApplicationController. Now you have pretty logs getting output to `log/custom_log.log`
18
+
19
+
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ - [ ] Add a config that can be set in an initializer for accessing user and prettifying in logs
30
+ - [ ] Customize location of custom_log file
31
+ - [ ] Don't auto-hijack errors, should need to add that manually?
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the PrettyLogger project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/pretty_logger/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,95 @@
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
@@ -0,0 +1,14 @@
1
+ module PrettyLogger::ControllerMethods
2
+ def pretty_logit
3
+ request_logger.log_request
4
+ end
5
+
6
+ def request_logger
7
+ @request_logger ||= ::PrettyLogger::RequestLogger.new(request: request)
8
+ end
9
+
10
+ def prettylog_and_reraise!(exception)
11
+ request_logger.log_error(exception)
12
+ raise exception
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ require "coderay"
2
+ # https://github.com/rubychan/coderay/blob/master/lib/coderay/encoders/terminal.rb
3
+ term_overrides = {
4
+ string: {
5
+ self: "\e[32m",
6
+ modifier: "\e[1;32m",
7
+ char: "\e[1;33m",
8
+ delimiter: "\e[1;32m",
9
+ escape: "\e[1;32m",
10
+ },
11
+ symbol: {
12
+ self: "\e[36m",
13
+ delimiter: "\e[1;36m",
14
+ },
15
+ # attribute_name: "\e[36m",
16
+ # decorator: "\e[36m",
17
+ }
18
+ term_overrides.each do |key, val|
19
+ ::CodeRay::Encoders::Terminal::TOKEN_COLORS[key] = val
20
+ end
@@ -0,0 +1,10 @@
1
+ module PrettyLogger
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "pretty_logger.configure_rails_initialization" do
4
+ ActiveSupport.on_load(:action_controller) do
5
+ include PrettyLogger::ControllerMethods
6
+ rescue_from ::StandardError, with: :prettylog_and_reraise!
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,40 @@
1
+ class PrettyLogger::RequestLogger < PrettyLogger::BaseLogger
2
+ attr_accessor :request, :current_user
3
+
4
+ def initialize(request:nil, current_user:nil)
5
+ @request = request
6
+ @current_user = current_user
7
+ end
8
+
9
+ def log_request
10
+ ::PrettyLogger::BaseLogger.info([
11
+ "#{pretty_user} #{request.method.upcase} #{request.path}",
12
+ params.blank? ? nil : ::PrettyLogger::BaseLogger.pretty_message(params).truncate(2000),
13
+ ].compact.join("\n"))
14
+ end
15
+
16
+ def log_error(exception)
17
+ ::PrettyLogger::BaseLogger.error([
18
+ "#{pretty_user} #{request.path} #{colorize(:red, exception.class)}",
19
+ params.blank? ? nil : ::PrettyLogger::BaseLogger.pretty_message(params).truncate(2000),
20
+ colorize(:red, focused_backtrace(exception.backtrace).first),
21
+ colorize(:red, clean_message("#{exception.class} #{exception.message}")),
22
+ ].compact.join("\n"))
23
+ end
24
+
25
+ def params
26
+ return unless @request.present?
27
+
28
+ @request.filtered_parameters.except(:action, :controller, :id, :authenticity_token, :_method)
29
+ end
30
+
31
+ def pretty_user
32
+ return colorize(:grey, "[?]") unless current_user.present?
33
+
34
+ name = current_user.try(:username).presence
35
+ name ||= current_user.try(:name).presence
36
+ name ||= "#{current_user.class.name}:#{current_user.id}"
37
+
38
+ colorize(:magenta, "[#{name}]")
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module PrettyLogger
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "pretty_logger/version"
4
+ require_relative "pretty_logger/override_console_colors"
5
+ require_relative "pretty_logger/base_logger"
6
+ require_relative "pretty_logger/request_logger"
7
+ require_relative "pretty_logger/controller_methods"
8
+ require_relative "pretty_logger/railtie" if defined?(Rails)
9
+
10
+ module PrettyLogger
11
+ class Error < StandardError; end
12
+ end
@@ -0,0 +1,4 @@
1
+ module PrettyLogger
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Rocco Nicholls
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: coderay
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.3
27
+ description: Quick logger that lets you output to a different log file for more controlled
28
+ breakdowns of site visits
29
+ email:
30
+ - rocco11nicholls@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/pretty_logger.rb
43
+ - lib/pretty_logger/base_logger.rb
44
+ - lib/pretty_logger/controller_methods.rb
45
+ - lib/pretty_logger/override_console_colors.rb
46
+ - lib/pretty_logger/railtie.rb
47
+ - lib/pretty_logger/request_logger.rb
48
+ - lib/pretty_logger/version.rb
49
+ - sig/pretty_logger.rbs
50
+ homepage: https://github.com/Rockster160/pretty_logger
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/Rockster160/pretty_logger
55
+ source_code_uri: https://github.com/Rockster160/pretty_logger
56
+ changelog_uri: https://github.com/Rockster160/pretty_logger/blob/master/CHANGELOG.md
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.6.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.4.1
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Quick logger that lets you output to a different log file for more controlled
76
+ breakdowns of site visits.
77
+ test_files: []