log2slack 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d9c1ea119a5689c4232dbf027427137bc94e324373a30deda89b84bbf3e2bd9
4
- data.tar.gz: 983eb03bf04687559f7152d5549b36fee4fb036df2939ee46aa33c337bee3c6d
3
+ metadata.gz: 24809c147d1748c95cb99546f7112c66219019dc682b6e6c622f999b2533091f
4
+ data.tar.gz: d977a2afabc432fe33c902663df203ecbe49ff6aab584636ebdca88f8b270929
5
5
  SHA512:
6
- metadata.gz: ff0d7c6a694ac03feb759bf812df6a8f7ad4d00bb05b784c668bd48b5d715438ae196870074c820fb62022858064b7bab6bc96ff2d10ad31a13a2cf818cf2ba9
7
- data.tar.gz: 95a5260ec4743d9d1c517f1a6abb7845ae709df020c32fec54c29853b858d820da72210b19a8a721a181923f3dcca93e53b36e3fcb18df7cd371ec73a1abaf26
6
+ metadata.gz: 6fcf7836690b9edaf8c375f4eed9cd4afd2b29ed5e6dc9b8726d9bde16286bb1f6aba5a399a43db683191fb03828a57751eb9f81f759d264ac5dfefe33fe830f
7
+ data.tar.gz: dd36b39f41d280cbab2d8dddc76c9d4f79ee5d019f866149be09e0c8b36fff46741050d392facced5d489526b5f903db45106cea0a8ed58cff4e3bac5569bc59
data/Gemfile CHANGED
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source "https://rubygems.org"
3
+ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in log2slack.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
8
+ gem 'rake', '~> 13.0'
9
9
 
10
- gem "rspec", "~> 3.0"
10
+ gem 'rspec', '~> 3.0'
11
11
 
12
- gem "slack-notifier", "~> 2.3.2"
12
+ gem 'slack-notifier', '~> 2.4.0'
13
13
 
14
- gem "rubocop"
14
+ gem 'rubocop'
data/Gemfile.lock CHANGED
@@ -1,49 +1,56 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- log2slack (0.1.0)
4
+ log2slack (0.1.2)
5
+ slack-notifier (~> 2.4.0)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
- ast (2.4.2)
10
- diff-lcs (1.5.0)
11
- parallel (1.21.0)
12
- parser (3.1.1.0)
10
+ ast (2.4.3)
11
+ diff-lcs (1.6.1)
12
+ json (2.7.6)
13
+ parallel (1.24.0)
14
+ parser (3.3.7.3)
13
15
  ast (~> 2.4.1)
16
+ racc
17
+ racc (1.8.1)
14
18
  rainbow (3.1.1)
15
- rake (13.0.6)
16
- regexp_parser (2.2.1)
17
- rexml (3.2.5)
18
- rspec (3.11.0)
19
- rspec-core (~> 3.11.0)
20
- rspec-expectations (~> 3.11.0)
21
- rspec-mocks (~> 3.11.0)
22
- rspec-core (3.11.0)
23
- rspec-support (~> 3.11.0)
24
- rspec-expectations (3.11.0)
19
+ rake (13.2.1)
20
+ regexp_parser (2.10.0)
21
+ rexml (3.4.1)
22
+ rspec (3.13.0)
23
+ rspec-core (~> 3.13.0)
24
+ rspec-expectations (~> 3.13.0)
25
+ rspec-mocks (~> 3.13.0)
26
+ rspec-core (3.13.3)
27
+ rspec-support (~> 3.13.0)
28
+ rspec-expectations (3.13.3)
25
29
  diff-lcs (>= 1.2.0, < 2.0)
26
- rspec-support (~> 3.11.0)
27
- rspec-mocks (3.11.0)
30
+ rspec-support (~> 3.13.0)
31
+ rspec-mocks (3.13.2)
28
32
  diff-lcs (>= 1.2.0, < 2.0)
29
- rspec-support (~> 3.11.0)
30
- rspec-support (3.11.0)
31
- rubocop (1.25.1)
33
+ rspec-support (~> 3.13.0)
34
+ rspec-support (3.13.2)
35
+ rubocop (1.50.2)
36
+ json (~> 2.3)
32
37
  parallel (~> 1.10)
33
- parser (>= 3.1.0.0)
38
+ parser (>= 3.2.0.0)
34
39
  rainbow (>= 2.2.2, < 4.0)
35
40
  regexp_parser (>= 1.8, < 3.0)
36
- rexml
37
- rubocop-ast (>= 1.15.1, < 2.0)
41
+ rexml (>= 3.2.5, < 4.0)
42
+ rubocop-ast (>= 1.28.0, < 2.0)
38
43
  ruby-progressbar (~> 1.7)
39
- unicode-display_width (>= 1.4.0, < 3.0)
40
- rubocop-ast (1.16.0)
41
- parser (>= 3.1.1.0)
42
- ruby-progressbar (1.11.0)
43
- slack-notifier (2.3.2)
44
- unicode-display_width (2.1.0)
44
+ unicode-display_width (>= 2.4.0, < 3.0)
45
+ rubocop-ast (1.30.0)
46
+ parser (>= 3.2.1.0)
47
+ ruby-progressbar (1.13.0)
48
+ slack-notifier (2.4.0)
49
+ unicode-display_width (2.6.0)
45
50
 
46
51
  PLATFORMS
52
+ arm64-darwin-20
53
+ universal-darwin-24
47
54
  x86_64-darwin-19
48
55
 
49
56
  DEPENDENCIES
@@ -51,7 +58,7 @@ DEPENDENCIES
51
58
  rake (~> 13.0)
52
59
  rspec (~> 3.0)
53
60
  rubocop
54
- slack-notifier (~> 2.3.2)
61
+ slack-notifier (~> 2.4.0)
55
62
 
56
63
  BUNDLED WITH
57
64
  2.3.8
data/README.md CHANGED
@@ -1,8 +1,19 @@
1
1
  # Log2slack
2
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/log2slack`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](https://badge.fury.io/rb/log2slack.svg)](https://badge.fury.io/rb/log2slack)
4
+ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.txt)
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ Log2slack is a simple Ruby gem for recording application log messages and notifying them to Slack. It allows you to easily send logs generated from batch processes, web applications, and other systems to Slack channels.
7
+
8
+ ## Features
9
+
10
+ - Support for standard log levels (INFO, WARN, ERROR)
11
+ - Automatic appearance changes based on log level
12
+ - INFO: Green color
13
+ - WARN: Yellow color + channel notification
14
+ - ERROR: Red color + channel notification
15
+ - Custom notification format support
16
+ - Simple and easy-to-use API
6
17
 
7
18
  ## Installation
8
19
 
@@ -14,15 +25,74 @@ gem 'log2slack'
14
25
 
15
26
  And then execute:
16
27
 
17
- $ bundle install
28
+ ```bash
29
+ $ bundle install
30
+ ```
18
31
 
19
32
  Or install it yourself as:
20
33
 
21
- $ gem install log2slack
34
+ ```bash
35
+ $ gem install log2slack
36
+ ```
22
37
 
23
38
  ## Usage
24
39
 
25
- TODO: Write usage instructions here
40
+ ### Basic Usage
41
+
42
+ ```ruby
43
+ require 'log2slack'
44
+
45
+ # Initialize the logger
46
+ logger = Log2slack::Logger.new
47
+
48
+ # Record logs
49
+ # Normal logs (not notified to Slack)
50
+ logger.info("Process started")
51
+ logger.warn("Warning occurred")
52
+ logger.error("Error occurred")
53
+
54
+ # Record logs for Slack notification (specify notify: true)
55
+ logger.info("Process started", notify: true)
56
+ logger.warn("Warning occurred", notify: true)
57
+ logger.error("Error occurred", notify: true)
58
+
59
+ # Notify to Slack
60
+ webhook_url = "https://hooks.slack.com/services/xxx/yyy/zzz"
61
+ channel = "#general"
62
+ user_name = "Log Notifier"
63
+ title = "Batch Process Result"
64
+
65
+ logger.notify_to_slack(webhook_url, channel, user_name, title)
66
+ ```
67
+
68
+ ### Custom Notification Format
69
+
70
+ You can also notify Slack with a custom format using a block:
71
+
72
+ ```ruby
73
+ logger.notify_to_slack(webhook_url, channel, user_name, title) do
74
+ {
75
+ text: "Custom text",
76
+ attachments: [
77
+ {
78
+ title: "Detailed Information",
79
+ text: "Detailed description here",
80
+ color: "#36a64f"
81
+ }
82
+ ]
83
+ }
84
+ end
85
+ ```
86
+
87
+ ### Log Levels and Notification Appearance
88
+
89
+ The appearance of Slack notifications automatically changes based on the log level:
90
+
91
+ - **INFO**: Green attachment
92
+ - **WARN**: Yellow attachment + channel notification (`<!channel>`)
93
+ - **ERROR**: Red attachment + channel notification (`<!channel>`)
94
+
95
+ Once an ERROR level log is recorded, the status will not be overwritten by subsequent INFO or WARN level logs. This prevents important error messages from being missed.
26
96
 
27
97
  ## Development
28
98
 
@@ -32,7 +102,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
102
 
33
103
  ## Contributing
34
104
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/log2slack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/log2slack/blob/master/CODE_OF_CONDUCT.md).
105
+ Bug reports and pull requests are welcome on GitHub at https://github.com/delightech/log2slack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
36
106
 
37
107
  ## License
38
108
 
@@ -40,4 +110,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
110
 
41
111
  ## Code of Conduct
42
112
 
43
- Everyone interacting in the Log2slack project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/log2slack/blob/master/CODE_OF_CONDUCT.md).
113
+ Everyone interacting in the Log2slack project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
@@ -4,66 +4,137 @@ require 'logger'
4
4
  require 'slack-notifier'
5
5
  module Log2slack
6
6
  class Logger
7
+ # Log level constants
8
+ LOG_LEVEL_INFO = 'INFO'
9
+ LOG_LEVEL_WARN = 'WARN'
10
+ LOG_LEVEL_ERROR = 'ERROR'
11
+
7
12
  attr_reader :messages, :status
8
13
 
9
- def initialize
14
+ # Initialize a new Logger instance
15
+ #
16
+ # @param output [IO, String] The log output destination (STDOUT, file path, etc.)
17
+ # @param max_files [Integer] Maximum number of log files to keep
18
+ # @param max_size [Integer] Maximum size of each log file in bytes
19
+ # @return [Logger] A new Logger instance
20
+ def initialize(output = STDOUT, max_files = 10, max_size = 1024 * 1024)
10
21
  # load logger module
11
- @logger = ::Logger.new(STDOUT)
22
+ @logger = ::Logger.new(output, max_files, max_size)
12
23
  @messages = []
13
- @status = 'INFO'
24
+ @status = LOG_LEVEL_INFO
14
25
  end
15
26
 
27
+ # Log a message with the specified level
28
+ #
29
+ # @param message [String] The message to log
30
+ # @param level [Symbol] The log level (:info, :warn, :error)
31
+ # @param notify [Boolean] Whether to store the message for Slack notification
32
+ # @return [void]
16
33
  def log(message, level: :info, notify: false)
17
34
  @logger.send(level, message)
18
35
  @messages.push("[#{level.to_s.upcase}]#{message}") if notify
19
36
  @status = level.to_s.upcase if level == :error
20
37
  # Do not overwrite with warn in case of error
21
- @status = level.to_s.upcase if level == :warn && @status != 'ERROR'
38
+ @status = level.to_s.upcase if level == :warn && @status != LOG_LEVEL_ERROR
22
39
  end
23
40
 
41
+ # Log a message with INFO level
42
+ #
43
+ # @param message [String] The message to log
44
+ # @param notify [Boolean] Whether to store the message for Slack notification
45
+ # @return [void]
24
46
  def info(message, notify: false)
25
47
  log(message, level: :info, notify: notify)
26
48
  end
27
49
 
50
+ # Log a message with WARN level
51
+ #
52
+ # @param message [String] The message to log
53
+ # @param notify [Boolean] Whether to store the message for Slack notification
54
+ # @return [void]
28
55
  def warn(message, notify: false)
29
56
  log(message, level: :warn, notify: notify)
30
57
  end
31
58
 
59
+ # Log a message with ERROR level
60
+ #
61
+ # @param message [String] The message to log
62
+ # @param notify [Boolean] Whether to store the message for Slack notification
63
+ # @return [void]
32
64
  def error(message, notify: false)
33
65
  log(message, level: :error, notify: notify)
34
66
  end
35
67
 
68
+ # Create attachment payload for Slack notification
69
+ #
70
+ # @param title [String] The title for the Slack message
71
+ # @return [Hash] The attachment payload
36
72
  def make_attachments(title)
37
- if @status == 'ERROR'
38
- title = "<!channel> #{title}"
39
- color = 'danger'
40
- elsif @status == 'WARN'
41
- title = "<!channel> #{title}"
42
- color = 'warning'
43
- else
44
- color = 'good'
45
- end
46
- { attachments: {
47
- fallback: title,
48
- title: title,
73
+ { attachments: [
74
+ {
75
+ fallback: format_title(title),
76
+ title: format_title(title),
49
77
  text: @messages.join("\n"),
50
- color: color
78
+ color: status_color
51
79
  }
52
- }
80
+ ] }
81
+ end
82
+
83
+ # Format the title based on the current status
84
+ #
85
+ # @param title [String] The original title
86
+ # @return [String] The formatted title
87
+ def format_title(title)
88
+ if @status == LOG_LEVEL_ERROR || @status == LOG_LEVEL_WARN
89
+ "<!channel> #{title}"
90
+ else
91
+ title
92
+ end
53
93
  end
54
94
 
95
+ # Determine the color based on the current status
96
+ #
97
+ # @return [String] The color code for Slack attachment
98
+ def status_color
99
+ case @status
100
+ when LOG_LEVEL_ERROR
101
+ 'danger'
102
+ when LOG_LEVEL_WARN
103
+ 'warning'
104
+ else
105
+ 'good'
106
+ end
107
+ end
108
+
109
+ # Send notification to Slack
110
+ #
111
+ # @param webhook_url [String] The Slack webhook URL
112
+ # @param channel [String] The Slack channel to post to
113
+ # @param user_name [String] The username to display in Slack
114
+ # @param title [String] The title for the Slack message
115
+ # @yield [optional] Block to provide custom payload
116
+ # @return [void]
117
+ # @raise [Log2slack::Error] If the Slack notification fails
55
118
  def notify_to_slack(webhook_url, channel, user_name, title)
56
119
  args = if block_given?
57
120
  yield()
58
121
  else
59
122
  make_attachments(title)
60
123
  end
61
- @notifier = Slack::Notifier.new(
62
- webhook_url,
63
- channel: channel,
64
- username: user_name
65
- ) if @notifier.nil?
66
- @notifier.post(args)
124
+ if @notifier.nil?
125
+ @notifier = Slack::Notifier.new(
126
+ webhook_url,
127
+ channel: channel,
128
+ username: user_name
129
+ )
130
+ end
131
+
132
+ begin
133
+ @notifier.post(args)
134
+ rescue StandardError => e
135
+ @logger.error("Failed to send notification to Slack: #{e.message}")
136
+ raise Log2slack::Error, "Slack notification failed: #{e.message}"
137
+ end
67
138
  end
68
139
  end
69
140
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Log2slack
4
- VERSION = "0.1.0"
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/log2slack.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "log2slack/version"
4
- require_relative "log2slack/logger"
3
+ require_relative 'log2slack/version'
4
+ require_relative 'log2slack/logger'
5
5
 
6
6
  module Log2slack
7
7
  class Error < StandardError; end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log2slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - delightech
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-05 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2025-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: slack-notifier
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.4.0
13
27
  description: Send log summaries to slack webhook.
14
28
  email:
15
29
  - hisafumi.kikkawa@gmail.com
@@ -27,7 +41,6 @@ files:
27
41
  - lib/log2slack.rb
28
42
  - lib/log2slack/logger.rb
29
43
  - lib/log2slack/version.rb
30
- - log2slack.gemspec
31
44
  - sig/log2slack.rbs
32
45
  homepage: https://github.com/delightech/log2slack
33
46
  licenses:
@@ -36,7 +49,7 @@ metadata:
36
49
  homepage_uri: https://github.com/delightech/log2slack
37
50
  source_code_uri: https://github.com/delightech/log2slack
38
51
  changelog_uri: https://github.com/delightech/log2slack
39
- post_install_message:
52
+ post_install_message:
40
53
  rdoc_options: []
41
54
  require_paths:
42
55
  - lib
@@ -51,8 +64,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
64
  - !ruby/object:Gem::Version
52
65
  version: '0'
53
66
  requirements: []
54
- rubygems_version: 3.1.6
55
- signing_key:
67
+ rubygems_version: 3.0.3.1
68
+ signing_key:
56
69
  specification_version: 4
57
70
  summary: Send log summaries to slack webhook.
58
71
  test_files: []
data/log2slack.gemspec DELETED
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/log2slack/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "log2slack"
7
- spec.version = Log2slack::VERSION
8
- spec.authors = ["delightech"]
9
- spec.email = ["hisafumi.kikkawa@gmail.com"]
10
-
11
- spec.summary = "Send log summaries to slack webhook."
12
- spec.description = "Send log summaries to slack webhook."
13
- spec.homepage = "https://github.com/delightech/log2slack"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.6.0"
16
-
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://github.com/delightech/log2slack"
19
- spec.metadata["changelog_uri"] = "https://github.com/delightech/log2slack"
20
-
21
- # Specify which files should be added to the gem when it is released.
22
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
- `git ls-files -z`.split("\x0").reject do |f|
25
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
- end
27
- end
28
- spec.bindir = "exe"
29
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
- spec.require_paths = ["lib"]
31
-
32
- # Uncomment to register a new dependency of your gem
33
- # spec.add_dependency "example-gem", "~> 1.0"
34
-
35
- # For more information and examples about making a new gem, check out our
36
- # guide at: https://bundler.io/guides/creating_gem.html
37
- end