relaton-logger 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2da06b3c702a902ef2fc5110c19a2c1f0c997caa3409a2c014fa2b8968ec0873
4
- data.tar.gz: 7908c829ec6d272cc924d0ae64a100f13b473945f0bca7496679215f515ccb8a
3
+ metadata.gz: 56155023be9761bf409a576e22703b1b2f2cc1ef9ef2290d264c29c7ad875549
4
+ data.tar.gz: a3cd75358fc69210004a40ad4081e8942db2313a260daf1a0e36754e8b42c273
5
5
  SHA512:
6
- metadata.gz: 6b484c092d45bc81bab1c0551f48f81df0b64eb1f23eea52fa341f1fc1a8893264ee03e3de18a9af2f056e316a86d6002e9227e98555c80357d922465cc8d81d
7
- data.tar.gz: cbae4869240542f537409d49c05864de5ca358fc17698fab793c794e7a2338cb8673524e450aebc9e1ea27adc7416c05912622bf6f92bd23e4ff684887554b80
6
+ metadata.gz: 5a868f415f0be67022bb18a4192f031c875cb42be1a698fb8b160d2154b95165cedd3b4417aea2070b3ff514bddd62cba01173a70dfaa157536242f4a589fb36
7
+ data.tar.gz: 921e56e9526a7f1ad6e3fac4205d042ef21a996ab9c4bbbfd7e6841b5e6ad99bc44a9a3243252e529017809b73249d02ee1f46e32942e42ed91865b1eb93d783
data/README.adoc CHANGED
@@ -48,22 +48,26 @@ Relaton.logger_pool.error("progname", key: "KEY") { "message" }
48
48
  [progname] ERROR: (KEY) message
49
49
  ----
50
50
 
51
- The logger pool can configured to contain multiple loggers. The loggers in the pool are called in the order they are added to the pool. To add a logger to the pool, call the `<<` method of the logger pool and pass the logger as an argument.
51
+ The logger pool can be configured to contain multiple loggers. Initially, there is one `:default` logger. Any instance of loggers in the logger pool are accessible with `[]` method. To add or replace a logger use `[]=` method of the logger pool and pass the logger as an argument. The loggers in the pool are called in the order they are added to the pool.
52
52
 
53
53
  [source, ruby]
54
54
  ----
55
- Relaton::Logger.configure do |config|
56
- config.logger_pool << Relaton::Logger::Log.new("relaton.log", levels: [:info, :warn])
57
- end
55
+ # Access logger
56
+ Relaton.logger_pool[:default]
57
+ =>
58
+ #<Relaton::Logger::Log:0x00000001212068c8
59
+
60
+ # Add a new logger
61
+ Relaton.logger_pool[:file] = Relaton::Logger::Log.new("relaton.log", levels: [:info, :warn])
58
62
  ----
59
63
 
60
- To replace the loggers in the pool, use `=` method of the logger pool and pass the new loggers as an argument.
64
+ To change the log level of a logger, call the `level=` method of the logger.
61
65
 
62
66
  [source, ruby]
63
67
  ----
64
- Relaton::Logger.configure do |config|
65
- config.logger_pool = [Relaton::Logger::Log.new("relaton.log", levels: [:info, :warn])]
66
- end
68
+ Relaton.logger_pool[:default].levels = [:info, :warn, :error]
69
+ Relaton.logger_pool[:default].add_level :fatal
70
+ Relaton.logger_pool[:default].remove_level :warn
67
71
  ----
68
72
 
69
73
  To create a new logger, call the `new` method of the `Relaton::Logger::Log` class and pass arguments:
@@ -0,0 +1,76 @@
1
+ require "net/http"
2
+
3
+ module Relaton
4
+ module Logger
5
+ module Channels
6
+ #
7
+ # This class is used to create a GitHub issue with the log content.
8
+ # The issue will be created in the repository specified in the
9
+ # initializer.
10
+ # The log content is stored in the issue body. Only unique log messages
11
+ # are stored.
12
+ # Token is required to create an issue. It should be stored in the
13
+ # environment variable GITHUB_TOKEN.
14
+ # To create an issue, call the create_issue method after all log messages
15
+ # are written.
16
+ #
17
+ class GhIssue
18
+ #
19
+ # Create a new instance of the class.
20
+ #
21
+ # @param [String] repo owner/repo name
22
+ # @param [String] title title of the issue
23
+ #
24
+ def initialize(repo, title)
25
+ @repo = repo
26
+ @title = title
27
+ @log = Set.new
28
+ puts "GITHUB_TOKEN is not set!" if ENV["GITHUB_TOKEN"].nil?
29
+ end
30
+
31
+ def write(string)
32
+ @log << string
33
+ end
34
+
35
+ def close
36
+ end
37
+
38
+ def create_issue
39
+ return if @log.empty? || ENV["GITHUB_TOKEN"].nil?
40
+
41
+ responce = post_issue
42
+
43
+ if responce.code.to_i == 201
44
+ puts "Issue created!"
45
+ else
46
+ puts "Failed to create issue: #{responce.code} #{responce.message}"
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def post_issue
53
+ uri = URI("https://api.github.com/repos/#{@repo}/issues")
54
+ http = Net::HTTP.new(uri.host, uri.port)
55
+ http.use_ssl = true
56
+
57
+ request = Net::HTTP::Post.new(uri.request_uri, headers)
58
+ request.body = issue_body.to_json
59
+
60
+ http.request(request)
61
+ end
62
+
63
+ def issue_body
64
+ { title: @title, body: @log.join("\n") }
65
+ end
66
+
67
+ def headers
68
+ {
69
+ "Content-Type" => "application/json",
70
+ "Authorization" => "token #{ENV['GITHUB_TOKEN']}",
71
+ }
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -15,7 +15,7 @@ module Relaton::Logger
15
15
 
16
16
  def initialize
17
17
  @logger_pool ||= Pool.new
18
- @logger_pool << Log.new($stderr, levels: %i[info warn error fatal])
18
+ @logger_pool[:default] = Log.new($stderr, levels: %i[info warn error fatal])
19
19
  end
20
20
 
21
21
  #
@@ -1,23 +1,23 @@
1
1
  module Relaton::Logger
2
2
  class Pool
3
3
  extend Forwardable
4
- def_delegators :@loggers, :<<, :[], :first, :last, :empty?, :any?, :size, :each, :detect, :length
4
+ def_delegators :@loggers, :[], :[]=, :size, :each
5
5
 
6
6
  attr_accessor :loggers
7
7
 
8
8
  def initialize
9
- @loggers = []
9
+ @loggers = {}
10
10
  end
11
11
 
12
12
  def unknown(message = nil, progname = nil, **args, &block)
13
- @loggers.each { |logger| logger.send(__callee__, message, progname, **args, &block) }
13
+ @loggers.each { |_, logger| logger.send(__callee__, message, progname, **args, &block) }
14
14
  nil
15
15
  end
16
16
 
17
17
  %i[fatal error warn info debug].each { |m| alias_method m, :unknown }
18
18
 
19
19
  def truncate
20
- @loggers.each(&:truncate)
20
+ @loggers.each { |_, logger| logger.truncate }
21
21
  nil
22
22
  end
23
23
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Relaton
4
4
  module Logger
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  end
@@ -9,6 +9,7 @@ require_relative "logger/pool"
9
9
  require_relative "logger/formatter_string"
10
10
  require_relative "logger/formatter_json"
11
11
  require_relative "logger/config"
12
+ require_relative "logger/channels/gh_issue"
12
13
 
13
14
  module Relaton
14
15
  def self.logger_pool
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-20 00:00:00.000000000 Z
11
+ date: 2024-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logger
@@ -37,6 +37,7 @@ files:
37
37
  - README.adoc
38
38
  - Rakefile
39
39
  - lib/relaton/logger.rb
40
+ - lib/relaton/logger/channels/gh_issue.rb
40
41
  - lib/relaton/logger/config.rb
41
42
  - lib/relaton/logger/formatter_json.rb
42
43
  - lib/relaton/logger/formatter_string.rb
@@ -66,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
67
  - !ruby/object:Gem::Version
67
68
  version: '0'
68
69
  requirements: []
69
- rubygems_version: 3.3.26
70
+ rubygems_version: 3.3.27
70
71
  signing_key:
71
72
  specification_version: 4
72
73
  summary: The logger for Relaton gem.