ougai-formatters-customizable 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 64f9b36ed7779caff3e9ce05fe6062d1f6c0ab7e7754b55463a3475147bc1b27
4
+ data.tar.gz: 62c6c4db4205f4faea3a3c5f6cc5cb1c1b3cdbf6849f1cf3085636eb5e5e19b0
5
+ SHA512:
6
+ metadata.gz: 445010e474a5863bf5311395b474857825962a67b7f2f72ad030656f46e8e99b339264dff77057d06e2f8bde3eda75ce661c620fd7afade58f94461b0a2393db
7
+ data.tar.gz: 7b06933a749c5632e87d7828f6009145c4d21ec3e01a23696d982e160bc96bd1fa8854a71c8a142d5e400deac0becbe2264055c9c17005855f50cbd1a448509a
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Al-un
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
+ # Ougai::Formatters::Customizable
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ougai/formatters/customizable`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ougai-formatters-customizable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ougai-formatters-customizable
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ougai-formatters-customizable.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ougai
4
+ module Formatters
5
+ # List of useful ANSI escape sequences for terminal font formatting. Source
6
+ # is https://gist.github.com/chrisopedia/8754917.
7
+ module Colors
8
+ # -- Comments compared to Ougai initial coloring --
9
+ # Non-bold font colors do not use \e[0;{value}m because it seems to be
10
+ # incompatible with background colors: \e[41m\e[0;34mtext\e[0m does not print
11
+ # background red while \e[41m\e[34mtext\e[0m works. However, to put font in
12
+ # bold/bright mode, \e[41m\e[1;34mtext\e[0m works
13
+ # => Tested on Windows PowerShell and MinGW64
14
+ #
15
+ # Colors values cannot be frozen as .concat is called upon them
16
+
17
+ # Reset formatting. To be appended after every formatted text
18
+ RESET = "\e[0m"
19
+ # Foreground colors
20
+ BLACK = "\e[30m"
21
+ RED = "\e[31m"
22
+ GREEN = "\e[32m"
23
+ YELLOW = "\e[33m"
24
+ BLUE = "\e[34m"
25
+ PURPLE = "\e[35m"
26
+ CYAN = "\e[36m"
27
+ WHITE = "\e[37m"
28
+ BOLD_RED = "\e[1;31m"
29
+ BOLD_GREEN = "\e[1;32m"
30
+ BOLD_YELLOW = "\e[1;33m"
31
+ BOLD_BLUE = "\e[1;34m"
32
+ BOLD_MAGENTA = "\e[1;35m"
33
+ BOLD_CYAN = "\e[1;36m"
34
+ BOLD_WHITE = "\e[1;37m"
35
+ # Background colors
36
+ BG_BLACK = "\e[40m"
37
+ BG_RED = "\e[41m"
38
+ BG_GREEN = "\e[42m"
39
+ BG_YELLOW = "\e[43m"
40
+ BG_BLUE = "\e[44m"
41
+ BG_MAGENTA = "\e[45m"
42
+ BG_CYAN = "\e[46m"
43
+ BG_WHITE = "\e[47m"
44
+
45
+ class << self
46
+ # Color a text by prepending a font styling escape sequence and
47
+ # appending a reset sequence. This method does a pure String concatenation
48
+ # and does not check the values are properly escaped. This allows
49
+ # customization, depending on user's terminal, to use custom escape
50
+ # sequences.
51
+ #
52
+ # @param [String] color color to prepend. Color can be from the list
53
+ # above or have a complete custom String value depending on the
54
+ # terminal. If +nil+, text is not modified.
55
+ # @param [String] text text to be colored
56
+ # @param [String] reset reset font styling escape sequence. Default
57
+ # to +\e[0m+
58
+ def color_text(color, text, reset = Ougai::Formatters::Colors::RESET)
59
+ return text if color.nil?
60
+
61
+ # .concat is preferred in Ruby:
62
+ # https://coderwall.com/p/ac5j9g/or-concat-what-is-faster-for-appending-string-in-ruby
63
+ ''.dup.concat(color).concat(text).concat(reset)
64
+ end
65
+ end
66
+
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ougai/formatters/colors'
4
+
5
+ module Ougai
6
+ module Formatters
7
+ module Colors
8
+ # Handle the colorization of output, mainly aimed at console formatting.
9
+ # The configuration,split by subject such as +level+, +msg+,
10
+ # or +datetime+ is basically a Hash: +config+ with the subject as key
11
+ # and values. Values can be have three types:
12
+ # - String: the color escape sequence for the subject
13
+ # - Hash: the color escape sequence per severity. If not all severities
14
+ # are defined, a +:default+ value must be defined
15
+ # - Symbol: refers to another key and same coloring is applied
16
+ class Configuration
17
+ class << self
18
+ # list default color configuration
19
+ # @note 'any' severity label decided in
20
+ # +Ougai::Logging::Severity#to_label+
21
+ # @note values are copied from +Ougai::Formatters::Readable+ coloring
22
+ # values
23
+ def default_configuration
24
+ {
25
+ severity: {
26
+ trace: Ougai::Formatters::Colors::BLUE,
27
+ debug: Ougai::Formatters::Colors::WHITE,
28
+ info: Ougai::Formatters::Colors::CYAN,
29
+ warn: Ougai::Formatters::Colors::YELLOW,
30
+ error: Ougai::Formatters::Colors::RED,
31
+ fatal: Ougai::Formatters::Colors::PURPLE,
32
+ any: Ougai::Formatters::Colors::GREEN
33
+ }
34
+ }
35
+ end
36
+ end
37
+
38
+ # @param [Hash] configuration Color configuration mapping. Cannot be nil
39
+ # @param [Boolean] load_default_config If true, then default configuration
40
+ # values is fetched to fill missing value from the provided
41
+ # configuration. Default is true.
42
+ def initialize(configuration = {})
43
+ # check if loading or not from default configuration
44
+ if configuration.fetch(:load_default_config) { true }
45
+ @config = Configuration.default_configuration
46
+ else
47
+ @config = {}
48
+ end
49
+
50
+ configuration.each do |key, val|
51
+ default_val = @config[key]
52
+ # default value is a Hash AND input value is a Hash => merge
53
+ if val.is_a?(Hash) && default_val.is_a?(Hash)
54
+ @config[key] = default_val.merge(val)
55
+ # Input value is assigned because one of the follow
56
+ # 1) input value is not defined in default configuration
57
+ # 2) input value is not a Hash which overrides the default value
58
+ # 3) default value is not a Hash and input is a Hash => Arbitrary
59
+ else
60
+ @config[key] = val
61
+ end
62
+ end
63
+ end
64
+
65
+ # @param [Symbol] subject_key to fetch the color to color the text
66
+ # @param [String] text to be colored text
67
+ # @param [Symbol] severity log level
68
+ # @return a colored text depending on the subject
69
+ def color(subject_key, text, severity)
70
+ color = get_color_for(subject_key, severity)
71
+ Ougai::Formatters::Colors.color_text(color, text)
72
+ end
73
+
74
+ # Return the color for a given suject and a given severity. This color
75
+ # can then be applied to any text via
76
+ # +Ougai::Formatters::Colors.color_text+
77
+ #
78
+ # +get_color_for+ handles color inheritance: if a subject inherit color
79
+ # from another subject, subject value is the symbol refering to the
80
+ # other subject.
81
+ # !!WARNING!!: Circular references are not checked and lead to infinite
82
+ # loop
83
+ #
84
+ # @param [Symbol] subject_key: to define the color to color the text
85
+ # @param [Symbol] severity: log level
86
+ # @return requested color String value or +nil+ if not colored
87
+ def get_color_for(subject_key, severity)
88
+ # no colorization
89
+ return nil unless @config.key?(subject_key)
90
+
91
+ # no severity dependence nor inheritance
92
+ color = @config[subject_key]
93
+ return color if color.is_a? String
94
+
95
+ # inheritance from another subject
96
+ return get_color_for(color, severity) if color.is_a? Symbol
97
+
98
+ # severity dependent but not inherited value or return +nil+ if
99
+ # configuration is incorrect
100
+ severity = severity.downcase.to_sym
101
+ color.fetch(severity) { color[:default] }
102
+ end
103
+
104
+ end
105
+
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ougai/formatters/base'
4
+ require 'ougai/formatters/colors/configuration'
5
+
6
+ module Ougai
7
+ module Formatters
8
+ # Ougai log printing can be split in three components:
9
+ # 1. Main log message: usually with timestamp, log severity and a single
10
+ # line message
11
+ # 2. Log data: the structured logging component. Can be represented by
12
+ # a Hash
13
+ # 3. Errors: errors require specific log formatting
14
+ #
15
+ # Customizable offers a flexible way to handle each component
16
+ # independently with three procs:
17
+ # +format_msg+ Format message. Proc arguments are
18
+ # +|level, datetime, progname, data|+. This block must
19
+ # remove the key +:msg+ from +data+
20
+ # +format_data+ Format data. Proc argument is +|data|+.
21
+ # +format_err+ Format err. Proc argument is +|data|+. The proc must
22
+ # remove the key +:err+
23
+ class Customizable < Ougai::Formatters::Base
24
+ class << self
25
+ # Define the default main log message formatting to use.
26
+ # @param [Ougai::Formatters::Colors::Configuration] color_config: the
27
+ # color configuration to use. Cannot be null but can have an
28
+ # empty +config+ hash meaning that there is no colorization
29
+ def default_msg_format(color_config)
30
+ proc do |severity, datetime, _progname, data|
31
+ msg = data.delete(:msg)
32
+ severity = color_config.color(:severity, severity, severity)
33
+
34
+ "[#{datetime}] #{severity}: #{msg}"
35
+ end
36
+ end
37
+
38
+ # Define the default error formatting to use.
39
+ # @param [Array<Symbol>] excluded_fields list of key to exclude from
40
+ # +data+ before printing logs
41
+ # @param [Boolean] plain parameter to define if Awesome-Print renders
42
+ # in plain mode or not
43
+ def default_data_format(excluded_fields, plain)
44
+ proc do |data|
45
+ excluded_fields.each { |field| data.delete(field) }
46
+ next nil if data.empty?
47
+
48
+ data.ai(plain: plain)
49
+ end
50
+ end
51
+
52
+ # Define the default error formatting to use.
53
+ # @param [Integer] trace_indent space indentation to prepend before
54
+ # trace content
55
+ def default_err_format(trace_indent = 4)
56
+ proc do |data|
57
+ next nil unless data.key?(:err)
58
+
59
+ err = data.delete(:err)
60
+ err_str = " #{err[:name]} (#{err[:message]}):"
61
+ err_str += "\n" + (' ' * trace_indent) + err[:stack] if err.key?(:stack)
62
+ err_str
63
+ end
64
+ end
65
+ end
66
+
67
+ # Intialize a formatter
68
+ #
69
+ # @param [String] app_name application name (execution program name if nil)
70
+ # @param [String] hostname hostname (hostname if nil)
71
+ # @param [Hash] opts the initial values of attributes
72
+ # @option opts [String] :trace_max_lines (100) the value of trace_max_lines attribute
73
+ # @option opts [String] :plain (false) the value of plain attribute
74
+ # @option opts [String] :excluded_fields ([]) the value of excluded_fields attribute
75
+ # @option opts [Ougai::Formatters::Colors::Configuration] :color_config
76
+ # assign a color configuration. Takes predecence over :colors
77
+ # @option opts [Proc] :format_msg main message formatter
78
+ # @option opts [Proc] :format_data data formatter
79
+ # @option opts [Proc] :format_err error formatter
80
+ def initialize(app_name = nil, hostname = nil, opts = {})
81
+ aname, hname, opts = Base.parse_new_params([app_name, hostname, opts])
82
+ super(aname, hname, opts)
83
+
84
+ # Message logging
85
+ color_config = opts.fetch(:color_config) {
86
+ color_config = Ougai::Formatters::Colors::Configuration.new({})
87
+ }
88
+ @format_msg = opts.fetch(:format_msg) {
89
+ Customizable.default_msg_format(color_config)
90
+ }
91
+
92
+ # Data logging
93
+ plain = opts.fetch(:plain) { false }
94
+ excluded_fields = opts[:excluded_fields] || []
95
+ @format_data = opts.fetch(:format_data) {
96
+ Customizable.default_data_format(excluded_fields, plain)
97
+ }
98
+
99
+ # Error logging
100
+ trace_indent = opts.fetch(:trace_indent) { 4 }
101
+ @format_err = opts.fetch(:format_err) {
102
+ Customizable.default_err_format(trace_indent)
103
+ }
104
+
105
+ # Ensure dependency are present
106
+ load_dependent
107
+ end
108
+
109
+ # Format a log entry
110
+ #
111
+ # @param [String] severity log severity, in capital letters
112
+ # @param [Time] time timestamp of the log. Is formatted by +strftime+
113
+ # @param [String] progname optional program name
114
+ # @param [Hash] data log data. Main message is stored under the key +:msg+
115
+ # while errors are logged under the key +:err+.
116
+ def _call(severity, time, progname, data)
117
+ strs = ''.dup
118
+ # Main message
119
+ dt = format_datetime(time)
120
+ msg_str = @format_msg.call(severity, dt, progname, data)
121
+ strs.concat(msg_str)
122
+
123
+ # Error: displayed before additional data
124
+ err_str = @format_err.call(data)
125
+ strs.concat("\n").concat(err_str) unless err_str.nil?
126
+
127
+ # Additional data
128
+ data_str = @format_data.call(data)
129
+ strs.concat("\n").concat(data_str) unless data_str.nil?
130
+
131
+ strs.concat("\n")
132
+ end
133
+
134
+ protected
135
+
136
+ # Ensure `awesompe_print` is loaded
137
+ def load_dependent
138
+ require 'awesome_print'
139
+ rescue LoadError
140
+ puts 'You must install the awesome_print gem to use this output.'
141
+ raise
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ougai
4
+ module Formatters
5
+ CUSTOMIZABLE_VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ougai-formatters-customizable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Al-un
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: awesome_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.8.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.8'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.8.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: ougai
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.7'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.7.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.7'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.7.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.16'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.16'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '10.0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '10.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '3.0'
95
+ description: |2
96
+ This library aims at providing a fully flexible formatter compatible with the
97
+ Ougai library. Customization is about colorization and log formatting.
98
+ email:
99
+ - alun.sng@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - LICENSE.txt
105
+ - README.md
106
+ - lib/ougai/formatters/colors.rb
107
+ - lib/ougai/formatters/colors/configuration.rb
108
+ - lib/ougai/formatters/customizable.rb
109
+ - lib/ougai/formatters/customizable/version.rb
110
+ homepage: https://github.com/Al-un/ougai-formatters-customizable
111
+ licenses:
112
+ - MIT
113
+ metadata:
114
+ allowed_push_host: https://rubygems.org
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.7.6
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Customizable formatter for Ougai library
135
+ test_files: []