tty-logger 0.0.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TTY
4
+ class Logger
5
+ module Handlers
6
+ class Null
7
+ def initialize(*)
8
+ end
9
+
10
+ def call(*)
11
+ # noop
12
+ end
13
+ end # Null
14
+ end # Handlers
15
+ end # Logger
16
+ end # TTY
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module TTY
6
+ class Logger
7
+ module Handlers
8
+ class Stream
9
+ include Base
10
+
11
+ attr_reader :output
12
+
13
+ attr_reader :config
14
+
15
+ attr_reader :level
16
+
17
+ def initialize(output: $stderr, formatter: nil, config: nil, level: nil)
18
+ @output = Array[output].flatten
19
+ @formatter = coerce_formatter(formatter || config.formatter).new
20
+ @config = config
21
+ @level = level || @config.level
22
+ @mutex = Mutex.new
23
+ end
24
+
25
+ # @api public
26
+ def call(event)
27
+ @mutex.lock
28
+
29
+ data = {}
30
+ metadata.each do |meta|
31
+ case meta
32
+ when :date
33
+ data["date"] = event.metadata[:time].strftime(config.date_format)
34
+ when :time
35
+ data["time"] = event.metadata[:time].strftime(config.time_format)
36
+ when :file
37
+ data["path"] = format_filepath(event)
38
+ when :pid
39
+ data["pid"] = event.metadata[:pid]
40
+ else
41
+ raise "Unknown metadata `#{meta}`"
42
+ end
43
+ end
44
+ data["level"] = event.metadata[:level]
45
+ data["message"] = event.message.join(' ')
46
+ unless event.fields.empty?
47
+ data.merge!(event.fields)
48
+ end
49
+ unless event.backtrace.empty?
50
+ data.merge!("backtrace" => event.backtrace.join(","))
51
+ end
52
+
53
+ output.each do |out|
54
+ out.puts @formatter.dump(data, max_bytes: config.max_bytes,
55
+ max_depth: config.max_depth)
56
+ end
57
+ ensure
58
+ @mutex.unlock
59
+ end
60
+ end # Stream
61
+ end # Handlers
62
+ end # Logger
63
+ end # TTY
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TTY
4
+ class Logger
5
+ module Levels
6
+ DEBUG_LEVEL = 0
7
+ INFO_LEVEL = 1
8
+ WARN_LEVEL = 2
9
+ ERROR_LEVEL = 3
10
+ FATAL_LEVEL = 4
11
+
12
+ LEVEL_NAMES = {
13
+ debug: DEBUG_LEVEL,
14
+ info: INFO_LEVEL,
15
+ warn: WARN_LEVEL,
16
+ error: ERROR_LEVEL,
17
+ fatal: FATAL_LEVEL
18
+ }.freeze
19
+
20
+ # All the default level names
21
+ #
22
+ # @return [Array[Symbol]]
23
+ #
24
+ # @api private
25
+ def level_names
26
+ LEVEL_NAMES.keys
27
+ end
28
+
29
+ # Convert level name to level number
30
+ #
31
+ # @param [Symbol] level
32
+ #
33
+ # @return [Integer]
34
+ #
35
+ # @api private
36
+ def level_to_number(level)
37
+ LEVEL_NAMES[level.to_s.downcase.to_sym] ||
38
+ raise(ArgumentError, "Invalid level #{level.inspect}")
39
+ end
40
+
41
+ # Convert level number to level name
42
+ #
43
+ # @param [Integer] number
44
+ #
45
+ # @return [Symbol]
46
+ #
47
+ # @api private
48
+ def number_to_level(number)
49
+ LEVEL_NAMES.key(number)
50
+ end
51
+
52
+ # Compares two levels by name or number
53
+ #
54
+ # @return [Symbol]
55
+ # either :lt, :gt or :eq
56
+ #
57
+ # @api private
58
+ def compare_levels(left, right)
59
+ left = left.is_a?(Integer) ? left : level_to_number(left)
60
+ right = right.is_a?(Integer) ? right : level_to_number(right)
61
+
62
+ return :eq if left == right
63
+
64
+ left < right ? :lt : :gt
65
+ end
66
+ end # Levels
67
+ end # Logger
68
+ end # TTY
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class Logger
5
- VERSION = "0.0.0"
6
- end
7
- end
5
+ VERSION = "0.5.0"
6
+ end # Logger
7
+ end # TTY
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-07 00:00:00.000000000 Z
11
+ date: 2020-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: pastel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
20
- type: :development
19
+ version: '0.8'
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '0.8'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -42,37 +42,43 @@ dependencies:
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '3.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
55
  description: Readable, structured and beautiful terminal logging
56
56
  email:
57
- - me@piotrmurach.com
57
+ - piotr@piotrmurach.com
58
58
  executables: []
59
59
  extensions: []
60
- extra_rdoc_files: []
60
+ extra_rdoc_files:
61
+ - README.md
62
+ - CHANGELOG.md
63
+ - LICENSE.txt
61
64
  files:
62
65
  - CHANGELOG.md
63
66
  - LICENSE.txt
64
67
  - README.md
65
- - Rakefile
66
68
  - lib/tty-logger.rb
67
69
  - lib/tty/logger.rb
70
+ - lib/tty/logger/config.rb
71
+ - lib/tty/logger/data_filter.rb
72
+ - lib/tty/logger/event.rb
73
+ - lib/tty/logger/formatters/json.rb
74
+ - lib/tty/logger/formatters/text.rb
75
+ - lib/tty/logger/handlers/base.rb
76
+ - lib/tty/logger/handlers/console.rb
77
+ - lib/tty/logger/handlers/null.rb
78
+ - lib/tty/logger/handlers/stream.rb
79
+ - lib/tty/logger/levels.rb
68
80
  - lib/tty/logger/version.rb
69
- - spec/spec_helper.rb
70
- - spec/unit/log_spec.rb
71
- - tasks/console.rake
72
- - tasks/coverage.rake
73
- - tasks/spec.rake
74
- - tty-logger.gemspec
75
- homepage: https://piotrmurach.github.io/tty
81
+ homepage: https://ttytoolkit.org
76
82
  licenses:
77
83
  - MIT
78
84
  metadata:
@@ -80,7 +86,7 @@ metadata:
80
86
  bug_tracker_uri: https://github.com/piotrmurach/tty-logger/issues
81
87
  changelog_uri: https://github.com/piotrmurach/tty-logger/blob/master/CHANGELOG.md
82
88
  documentation_uri: https://www.rubydoc.info/gems/tty-logger
83
- homepage_uri: https://piotrmurach.github.io/tty
89
+ homepage_uri: https://ttytoolkit.org
84
90
  source_code_uri: https://github.com/piotrmurach/tty-logger
85
91
  post_install_message:
86
92
  rdoc_options: []
@@ -97,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
103
  - !ruby/object:Gem::Version
98
104
  version: '0'
99
105
  requirements: []
100
- rubygems_version: 3.0.3
106
+ rubygems_version: 3.1.2
101
107
  signing_key:
102
108
  specification_version: 4
103
109
  summary: Readable, structured and beautiful terminal logging
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require "bundler/gem_tasks"
2
-
3
- FileList['tasks/**/*.rake'].each(&method(:import))
4
-
5
- desc 'Run all specs'
6
- task ci: %w[ spec ]
7
-
8
- task default: :spec
@@ -1,14 +0,0 @@
1
- require "bundler/setup"
2
- require "tty-logger"
3
-
4
- RSpec.configure do |config|
5
- # Enable flags like --only-failures and --next-failure
6
- config.example_status_persistence_file_path = ".rspec_status"
7
-
8
- # Disable RSpec exposing methods globally on `Module` and `main`
9
- config.disable_monkey_patching!
10
-
11
- config.expect_with :rspec do |c|
12
- c.syntax = :expect
13
- end
14
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Logger do
4
- it "logs a message" do
5
- TTY::Logger.new
6
- end
7
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- desc 'Load gem inside irb console'
4
- task :console do
5
- require 'irb'
6
- require 'irb/completion'
7
- require File.join(__FILE__, '../../lib/tty-logger')
8
- ARGV.clear
9
- IRB.start
10
- end
11
- task c: %w[ console ]
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- desc 'Measure code coverage'
4
- task :coverage do
5
- begin
6
- original, ENV['COVERAGE'] = ENV['COVERAGE'], 'true'
7
- Rake::Task['spec'].invoke
8
- ensure
9
- ENV['COVERAGE'] = original
10
- end
11
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- begin
4
- require 'rspec/core/rake_task'
5
-
6
- desc 'Run all specs'
7
- RSpec::Core::RakeTask.new(:spec) do |task|
8
- task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
9
- end
10
-
11
- namespace :spec do
12
- desc 'Run unit specs'
13
- RSpec::Core::RakeTask.new(:unit) do |task|
14
- task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
15
- end
16
-
17
- desc 'Run integration specs'
18
- RSpec::Core::RakeTask.new(:integration) do |task|
19
- task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
20
- end
21
- end
22
-
23
- rescue LoadError
24
- %w[spec spec:unit spec:integration].each do |name|
25
- task name do
26
- $stderr.puts "In order to run #{name}, do `gem install rspec`"
27
- end
28
- end
29
- end
@@ -1,33 +0,0 @@
1
- lib = File.expand_path("lib", __dir__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "tty/logger/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "tty-logger"
7
- spec.version = TTY::Logger::VERSION
8
- spec.authors = ["Piotr Murach"]
9
- spec.email = ["me@piotrmurach.com"]
10
- spec.summary = %q{Readable, structured and beautiful terminal logging}
11
- spec.description = %q{Readable, structured and beautiful terminal logging}
12
- spec.homepage = "https://piotrmurach.github.io/tty"
13
- spec.license = "MIT"
14
-
15
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
- spec.metadata["bug_tracker_uri"] = "https://github.com/piotrmurach/tty-logger/issues"
17
- spec.metadata["changelog_uri"] = "https://github.com/piotrmurach/tty-logger/blob/master/CHANGELOG.md"
18
- spec.metadata["documentation_uri"] = "https://www.rubydoc.info/gems/tty-logger"
19
- spec.metadata["homepage_uri"] = spec.homepage
20
- spec.metadata["source_code_uri"] = "https://github.com/piotrmurach/tty-logger"
21
-
22
- spec.files = Dir['{lib,spec,examples}/**/*.rb']
23
- spec.files += Dir['tasks/*', 'tty-logger.gemspec']
24
- spec.files += Dir['README.md', 'CHANGELOG.md', 'LICENSE.txt', 'Rakefile']
25
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
- spec.require_paths = ["lib"]
27
-
28
- spec.required_ruby_version = '>= 2.0.0'
29
-
30
- spec.add_development_dependency "bundler", ">= 1.5"
31
- spec.add_development_dependency "rake"
32
- spec.add_development_dependency "rspec", "~> 3.0"
33
- end