log2slack 0.1.0 → 0.1.1
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 +4 -4
- data/Gemfile.lock +2 -0
- data/README.md +77 -7
- data/lib/log2slack/logger.rb +85 -17
- data/lib/log2slack/version.rb +1 -1
- metadata +18 -5
- data/log2slack.gemspec +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c5bbeb34a831b9328fbee0e9f89b468a1d5c41255c73c483e7066b12c354c61
|
4
|
+
data.tar.gz: 5582f2449133a1cef9783d4ce5e4cab331358d311ebf081d7f32a42d1c7979d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdde12fe8d4a34d5d25a140f5ca94e686c2a34a304f5ac9a07a55db0f46bea39a99068c5317a34c60bab6dbd4d6fa096b9c5f51f771ab30580153e7541cab938
|
7
|
+
data.tar.gz: 281c7bb1df8b27daa3b9db1059ae60befbf19d8eae24eaca82be5743a525b39fa3599c77f3bf14ce82043b951bb27258c5059ddb9f8e4aac9733d5e16e4725a1
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,19 @@
|
|
1
1
|
# Log2slack
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/log2slack)
|
4
|
+
[](LICENSE.txt)
|
4
5
|
|
5
|
-
|
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
|
-
|
28
|
+
```bash
|
29
|
+
$ bundle install
|
30
|
+
```
|
18
31
|
|
19
32
|
Or install it yourself as:
|
20
33
|
|
21
|
-
|
34
|
+
```bash
|
35
|
+
$ gem install log2slack
|
36
|
+
```
|
22
37
|
|
23
38
|
## Usage
|
24
39
|
|
25
|
-
|
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/
|
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](
|
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/lib/log2slack/logger.rb
CHANGED
@@ -4,54 +4,116 @@ 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
|
-
|
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(
|
22
|
+
@logger = ::Logger.new(output, max_files, max_size)
|
12
23
|
@messages = []
|
13
|
-
@status =
|
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 !=
|
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
73
|
{ attachments: {
|
47
|
-
fallback: title,
|
48
|
-
title: title,
|
74
|
+
fallback: format_title(title),
|
75
|
+
title: format_title(title),
|
49
76
|
text: @messages.join("\n"),
|
50
|
-
color:
|
77
|
+
color: status_color
|
51
78
|
}
|
52
79
|
}
|
53
80
|
end
|
54
81
|
|
82
|
+
# Format the title based on the current status
|
83
|
+
#
|
84
|
+
# @param title [String] The original title
|
85
|
+
# @return [String] The formatted title
|
86
|
+
def format_title(title)
|
87
|
+
if @status == LOG_LEVEL_ERROR || @status == LOG_LEVEL_WARN
|
88
|
+
"<!channel> #{title}"
|
89
|
+
else
|
90
|
+
title
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Determine the color based on the current status
|
95
|
+
#
|
96
|
+
# @return [String] The color code for Slack attachment
|
97
|
+
def status_color
|
98
|
+
case @status
|
99
|
+
when LOG_LEVEL_ERROR
|
100
|
+
'danger'
|
101
|
+
when LOG_LEVEL_WARN
|
102
|
+
'warning'
|
103
|
+
else
|
104
|
+
'good'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# Send notification to Slack
|
109
|
+
#
|
110
|
+
# @param webhook_url [String] The Slack webhook URL
|
111
|
+
# @param channel [String] The Slack channel to post to
|
112
|
+
# @param user_name [String] The username to display in Slack
|
113
|
+
# @param title [String] The title for the Slack message
|
114
|
+
# @yield [optional] Block to provide custom payload
|
115
|
+
# @return [void]
|
116
|
+
# @raise [Log2slack::Error] If the Slack notification fails
|
55
117
|
def notify_to_slack(webhook_url, channel, user_name, title)
|
56
118
|
args = if block_given?
|
57
119
|
yield()
|
@@ -63,7 +125,13 @@ module Log2slack
|
|
63
125
|
channel: channel,
|
64
126
|
username: user_name
|
65
127
|
) if @notifier.nil?
|
66
|
-
|
128
|
+
|
129
|
+
begin
|
130
|
+
@notifier.post(args)
|
131
|
+
rescue => e
|
132
|
+
@logger.error("Failed to send notification to Slack: #{e.message}")
|
133
|
+
raise Log2slack::Error, "Slack notification failed: #{e.message}"
|
134
|
+
end
|
67
135
|
end
|
68
136
|
end
|
69
137
|
end
|
data/lib/log2slack/version.rb
CHANGED
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.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- delightech
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2025-03-26 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.3.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.3.2
|
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:
|
@@ -51,7 +64,7 @@ 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.
|
67
|
+
rubygems_version: 3.1.4
|
55
68
|
signing_key:
|
56
69
|
specification_version: 4
|
57
70
|
summary: Send log summaries to slack webhook.
|
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
|