compact_log_formatter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3421ae0537f6a3bb7fd11e82a0bf90913ad91cee5ad621f5b992e052d40fe57d
4
+ data.tar.gz: a8ae1eee4dacc2aeebfde20b8c638069cc38150be04f928caba732540b1c2ed6
5
+ SHA512:
6
+ metadata.gz: ae5db7daa8774b84eb8fda900a38f4e002d23585ee9f356fcdd85c15f79dce805b6f84b655f51297df3984a20f93b58214f1e4155e3e7ec986aff0ea7f229b79
7
+ data.tar.gz: a906bfb91153c097b01423f5afd907344362ac275efa65c6624b40f067290ebb7f462ee9630bf5edd5c46da3dda1c4e2e275655e1a584334fc0b865e6117d4e6
@@ -0,0 +1,21 @@
1
+ # MIT LICENSE
2
+
3
+ Copyright (c) 2020 Mattia Roccoberton <mat@blocknot.es>
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.
@@ -0,0 +1,25 @@
1
+ # CompactLogFormatter
2
+ A Compact Log Formatter for Rails.
3
+
4
+ ## Usage
5
+ - Add to `Gemfile` (and execute `bundle`):
6
+ ```rb
7
+ gem 'compact_log_formatter', git: 'https://github.com/blocknotes/compact_log_formatter'
8
+ ```
9
+ - Add to `application.rb`:
10
+ ```rb
11
+ Rails.logger = Logger.new("log/#{Rails.env}.log")
12
+ Rails.logger.formatter = CompactLogFormatter::Formatter.new
13
+ ```
14
+
15
+ ## Screenshot
16
+ ![screenshot](screenshot.jpg)
17
+
18
+ ## Do you like it? Star it!
19
+ If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
20
+
21
+ ## Contributors
22
+ - [Mattia Roccoberton](https://blocknot.es/): author
23
+
24
+ ## License
25
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CompactLogFormatter
4
+ class Formatter < Logger::Formatter
5
+ def call(severity, datetime, _app, message)
6
+ timestamp = datetime.strftime('%y-%m-%d %H:%M:%S.%L')
7
+ level = "\e[#{color(severity)}m[#{status(severity)}]\e[0m"
8
+ body = %w[ERROR FATAL].include?(severity) ? "\e[#{color(severity)}m#{message}\e[0m" : highlight(message)
9
+ [timestamp, level, body].join(' ') + "\n"
10
+ end
11
+
12
+ private
13
+
14
+ def color(severity)
15
+ (@color ||= {
16
+ 'DEBUG' => 32,
17
+ 'ERROR' => 31,
18
+ 'FATAL' => 31,
19
+ # 'INFO' => 36,
20
+ 'WARN' => 33
21
+ })[severity] || 39
22
+ end
23
+
24
+ def highlight(message) # rubocop:disable Metrics/MethodLength
25
+ if message.match(/\AStarted\s[A-Z]+\s/)
26
+ "\e[36m#{message}\e[0m"
27
+ elsif (result = message.match(/\ACompleted\s(\d+)\s/))
28
+ color = {
29
+ '2' => 32,
30
+ '3' => 33,
31
+ '4' => 35,
32
+ '5' => 31
33
+ }[result[1][0]] || 36
34
+ "\e[#{color}m#{message}\e[0m"
35
+ else
36
+ message
37
+ end
38
+ end
39
+
40
+ def status(severity)
41
+ (@status ||= {
42
+ 'DEBUG' => 'DBG',
43
+ 'ERROR' => 'ERR',
44
+ 'FATAL' => 'FAT',
45
+ 'INFO' => 'NFO',
46
+ 'UNKNOWN' => 'UNK',
47
+ 'WARN' => 'WRN'
48
+ })[severity]
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CompactLogFormatter
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: compact_log_formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mattia Roccoberton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Compact Log Formatter for Rails
14
+ email:
15
+ - mat@blocknot.es
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.md
21
+ - README.md
22
+ - lib/compact_log_formatter.rb
23
+ - lib/compact_log_formatter/version.rb
24
+ homepage: https://github.com/blocknotes/compact_log_formatter
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.0.3
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Compact Log Formatter
47
+ test_files: []