vigilant-ruby 0.0.6 → 0.0.7
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/README.md +19 -7
- data/lib/vigilant-ruby/logger.rb +2 -9
- data/lib/vigilant-ruby/rails/logger.rb +248 -0
- data/lib/vigilant-ruby/rails/railtie.rb +16 -0
- data/lib/vigilant-ruby/version.rb +1 -1
- data/lib/vigilant-ruby.rb +8 -6
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1922622d6ddc6f7e1630e48672cd6d78c8d988436319af621e8c9a6e688dbd6
|
4
|
+
data.tar.gz: b73e50dca1371a51a5e7f83d1016e0b16b1c1d9ff43043bc280a418edd3ff3f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84dc689092a381d52aa4740a894d42fe95dc6e744c739b44daf92777cefece4711260c0f05f5e05f15964cdfcf03783d0b881fd06eda13a8a0fa98b627941967
|
7
|
+
data.tar.gz: b60229476423fffa95cd0697de5b3e8e7151eba286905ed0a08b8419781aa3ae0b4254ade52c909cba2539f3f62c0ad425cecc639ba2f16c1925c0c8b79da13f
|
data/README.md
CHANGED
@@ -5,7 +5,21 @@ This is the Ruby SDK for Vigilant (https://vigilant.run).
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
```bash
|
8
|
-
|
8
|
+
bundle add vigilant-ruby
|
9
|
+
```
|
10
|
+
|
11
|
+
## Logging Usage (Rails)
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
# In config/application.rb
|
15
|
+
require 'vigilant-ruby'
|
16
|
+
|
17
|
+
module YourApp
|
18
|
+
class Application < Rails::Application
|
19
|
+
config.logger = Vigilant::Rails::Logger.new(token: 'tk_1234567890', name: 'test-app')
|
20
|
+
config.log_level = :debug
|
21
|
+
end
|
22
|
+
end
|
9
23
|
```
|
10
24
|
|
11
25
|
## Logging Usage (Standard)
|
@@ -16,8 +30,7 @@ require 'vigilant-ruby'
|
|
16
30
|
# Initialize the logger
|
17
31
|
logger = Vigilant::Logger.new(
|
18
32
|
name: 'test-app',
|
19
|
-
|
20
|
-
token: "tk_0000000000000000",
|
33
|
+
token: 'tk_1234567890',
|
21
34
|
)
|
22
35
|
|
23
36
|
# Basic logging
|
@@ -41,18 +54,17 @@ require 'vigilant-ruby'
|
|
41
54
|
# Initialize the logger
|
42
55
|
logger = Vigilant::Logger.new(
|
43
56
|
name: 'test-app',
|
44
|
-
|
45
|
-
token: "tk_0000000000000000",
|
57
|
+
token: 'tk_1234567890',
|
46
58
|
)
|
47
59
|
|
48
60
|
# Enable autocapture
|
49
61
|
logger.autocapture_enable
|
50
62
|
|
51
63
|
# Log with autocapture
|
52
|
-
puts
|
64
|
+
puts 'A print statement'
|
53
65
|
|
54
66
|
# Log without autocapture
|
55
|
-
logger.info(
|
67
|
+
logger.info('A regular log')
|
56
68
|
|
57
69
|
# Shutdown the logger
|
58
70
|
logger.shutdown
|
data/lib/vigilant-ruby/logger.rb
CHANGED
@@ -22,8 +22,8 @@ module Vigilant
|
|
22
22
|
# @param endpoint [String] The base endpoint for the Vigilant API (e.g. "ingress.vigilant.run").
|
23
23
|
# @param token [String] The authentication token for the Vigilant API.
|
24
24
|
# @param insecure [Boolean] Whether to use HTTP instead of HTTPS (optional, defaults to false).
|
25
|
-
# @param passthrough [Boolean] Whether to also print logs to stdout/stderr (optional, defaults to
|
26
|
-
def initialize(
|
25
|
+
# @param passthrough [Boolean] Whether to also print logs to stdout/stderr (optional, defaults to false).
|
26
|
+
def initialize(name:, token:, endpoint:, insecure: false, passthrough: false)
|
27
27
|
@name = name
|
28
28
|
@token = token
|
29
29
|
|
@@ -49,22 +49,18 @@ module Vigilant
|
|
49
49
|
start_dispatcher
|
50
50
|
end
|
51
51
|
|
52
|
-
# Logs a debug message.
|
53
52
|
def debug(body, attributes = {})
|
54
53
|
enqueue_log(DEBUG, body, attributes)
|
55
54
|
end
|
56
55
|
|
57
|
-
# Logs an info message.
|
58
56
|
def info(body, attributes = {})
|
59
57
|
enqueue_log(INFO, body, attributes)
|
60
58
|
end
|
61
59
|
|
62
|
-
# Logs a warning message.
|
63
60
|
def warn(body, attributes = {})
|
64
61
|
enqueue_log(WARNING, body, attributes)
|
65
62
|
end
|
66
63
|
|
67
|
-
# Logs an error message.
|
68
64
|
def error(body, error = nil, attributes = {})
|
69
65
|
if error.nil?
|
70
66
|
enqueue_log(ERROR, body, attributes)
|
@@ -74,7 +70,6 @@ module Vigilant
|
|
74
70
|
end
|
75
71
|
end
|
76
72
|
|
77
|
-
# Enables stdout/stderr autocapture.
|
78
73
|
def autocapture_enable
|
79
74
|
return if @autocapture_enabled
|
80
75
|
|
@@ -83,7 +78,6 @@ module Vigilant
|
|
83
78
|
$stderr = StderrInterceptor.new(self, @original_stderr)
|
84
79
|
end
|
85
80
|
|
86
|
-
# Disables stdout/stderr autocapture.
|
87
81
|
def autocapture_disable
|
88
82
|
return unless @autocapture_enabled
|
89
83
|
|
@@ -92,7 +86,6 @@ module Vigilant
|
|
92
86
|
$stderr = @original_stderr
|
93
87
|
end
|
94
88
|
|
95
|
-
# Shuts down the logger, flushing any pending logs.
|
96
89
|
def shutdown
|
97
90
|
flush_if_needed(force: true)
|
98
91
|
@mutex.synchronize { @shutdown = true }
|
@@ -0,0 +1,248 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/logger'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Vigilant
|
7
|
+
module Rails
|
8
|
+
# A wrapper that delegates Rails-style logging calls to Vigilant::Logger
|
9
|
+
class Logger < ::ActiveSupport::Logger
|
10
|
+
include ::ActiveSupport::LoggerThreadSafeLevel if defined?(::ActiveSupport::LoggerThreadSafeLevel)
|
11
|
+
|
12
|
+
if defined?(::ActiveSupport::LoggerSilence)
|
13
|
+
include ::ActiveSupport::LoggerSilence
|
14
|
+
elsif defined?(::LoggerSilence)
|
15
|
+
include ::LoggerSilence
|
16
|
+
end
|
17
|
+
|
18
|
+
SEVERITY_MAP = {
|
19
|
+
::Logger::DEBUG => Vigilant::DEBUG,
|
20
|
+
::Logger::INFO => Vigilant::INFO,
|
21
|
+
::Logger::WARN => Vigilant::WARNING,
|
22
|
+
::Logger::ERROR => Vigilant::ERROR,
|
23
|
+
::Logger::FATAL => Vigilant::ERROR,
|
24
|
+
::Logger::UNKNOWN => Vigilant::ERROR
|
25
|
+
}.freeze
|
26
|
+
|
27
|
+
def initialize(name: Vigilant.configuration.name,
|
28
|
+
endpoint: Vigilant.configuration.endpoint,
|
29
|
+
token: Vigilant.configuration.token,
|
30
|
+
insecure: Vigilant.configuration.insecure,
|
31
|
+
passthrough: Vigilant.configuration.passthrough)
|
32
|
+
super(nil)
|
33
|
+
|
34
|
+
@vigilant_logger = Vigilant::Logger.new(
|
35
|
+
endpoint: endpoint,
|
36
|
+
token: token,
|
37
|
+
name: name,
|
38
|
+
insecure: insecure,
|
39
|
+
passthrough: passthrough
|
40
|
+
)
|
41
|
+
|
42
|
+
at_exit { close }
|
43
|
+
|
44
|
+
self.level = ::Logger::DEBUG
|
45
|
+
@tags = []
|
46
|
+
@extra_loggers = []
|
47
|
+
end
|
48
|
+
|
49
|
+
def kind_of?(klass)
|
50
|
+
return true if defined?(::ActiveSupport::BroadcastLogger) && klass == ::ActiveSupport::BroadcastLogger
|
51
|
+
|
52
|
+
super(klass)
|
53
|
+
end
|
54
|
+
alias is_a? kind_of?
|
55
|
+
|
56
|
+
def broadcasts
|
57
|
+
[self] + @extra_loggers
|
58
|
+
end
|
59
|
+
|
60
|
+
def broadcast_to(*io_devices_and_loggers)
|
61
|
+
io_devices_and_loggers.each do |io_device_or_logger|
|
62
|
+
extra_logger =
|
63
|
+
if io_device_or_logger.is_a?(::Logger)
|
64
|
+
io_device_or_logger
|
65
|
+
else
|
66
|
+
::ActiveSupport::Logger.new(io_device_or_logger)
|
67
|
+
end
|
68
|
+
@extra_loggers << extra_logger
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def stop_broadcasting_to(io_device_or_logger)
|
73
|
+
if io_device_or_logger.is_a?(::Logger)
|
74
|
+
@extra_loggers.delete(io_device_or_logger)
|
75
|
+
else
|
76
|
+
@extra_loggers.reject! do |logger|
|
77
|
+
defined?(::ActiveSupport::Logger) &&
|
78
|
+
::ActiveSupport::Logger.logger_outputs_to?(logger, io_device_or_logger)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def debug(progname = nil, &block)
|
84
|
+
add(::Logger::DEBUG, block, progname)
|
85
|
+
end
|
86
|
+
|
87
|
+
def info(progname = nil, &block)
|
88
|
+
add(::Logger::INFO, block, progname)
|
89
|
+
end
|
90
|
+
|
91
|
+
def warn(progname = nil, &block)
|
92
|
+
add(::Logger::WARN, block, progname)
|
93
|
+
end
|
94
|
+
|
95
|
+
def error(progname = nil, &block)
|
96
|
+
add(::Logger::ERROR, block, progname)
|
97
|
+
end
|
98
|
+
|
99
|
+
def fatal(progname = nil, &block)
|
100
|
+
add(::Logger::FATAL, block, progname)
|
101
|
+
end
|
102
|
+
|
103
|
+
def unknown(progname = nil, &block)
|
104
|
+
add(::Logger::UNKNOWN, block, progname)
|
105
|
+
end
|
106
|
+
|
107
|
+
def add(severity, message_or_block = nil, progname = nil)
|
108
|
+
return true if severity < level
|
109
|
+
|
110
|
+
msg =
|
111
|
+
if message_or_block.respond_to?(:call)
|
112
|
+
message_or_block.call.to_s
|
113
|
+
else
|
114
|
+
(message_or_block || progname).to_s
|
115
|
+
end.strip
|
116
|
+
|
117
|
+
vigilant_severity = SEVERITY_MAP.fetch(severity, Vigilant::ERROR)
|
118
|
+
log_to_vigilant(vigilant_severity, msg)
|
119
|
+
|
120
|
+
@extra_loggers.each { |logger| logger.add(severity, msg, progname) }
|
121
|
+
true
|
122
|
+
end
|
123
|
+
|
124
|
+
def tagged(*tags)
|
125
|
+
push_tags(*tags)
|
126
|
+
yield self
|
127
|
+
ensure
|
128
|
+
pop_tags(tags.size)
|
129
|
+
end
|
130
|
+
|
131
|
+
def push_tags(*tags)
|
132
|
+
@tags.concat(tags)
|
133
|
+
end
|
134
|
+
|
135
|
+
def pop_tags(amount = 1)
|
136
|
+
@tags.pop(amount)
|
137
|
+
end
|
138
|
+
|
139
|
+
def current_tags
|
140
|
+
@tags
|
141
|
+
end
|
142
|
+
|
143
|
+
def silence(temporary_level = ::Logger::ERROR)
|
144
|
+
old_level = level
|
145
|
+
self.level = temporary_level
|
146
|
+
yield self
|
147
|
+
ensure
|
148
|
+
self.level = old_level
|
149
|
+
end
|
150
|
+
|
151
|
+
def flush
|
152
|
+
@vigilant_logger.flush if @vigilant_logger.respond_to?(:flush)
|
153
|
+
rescue StandardError
|
154
|
+
nil
|
155
|
+
end
|
156
|
+
|
157
|
+
def reopen(_device = nil)
|
158
|
+
nil
|
159
|
+
end
|
160
|
+
|
161
|
+
def close
|
162
|
+
@vigilant_logger.shutdown if @vigilant_logger.respond_to?(:shutdown)
|
163
|
+
end
|
164
|
+
|
165
|
+
def datetime_format=(_format)
|
166
|
+
nil
|
167
|
+
end
|
168
|
+
|
169
|
+
def datetime_format
|
170
|
+
nil
|
171
|
+
end
|
172
|
+
|
173
|
+
attr_accessor :formatter
|
174
|
+
|
175
|
+
private
|
176
|
+
|
177
|
+
def log_to_vigilant(severity, message)
|
178
|
+
formatted_message, attributes = format_message(message)
|
179
|
+
case severity
|
180
|
+
when Vigilant::DEBUG
|
181
|
+
@vigilant_logger.debug(formatted_message, attributes)
|
182
|
+
when Vigilant::INFO
|
183
|
+
@vigilant_logger.info(formatted_message, attributes)
|
184
|
+
when Vigilant::WARNING
|
185
|
+
@vigilant_logger.warn(formatted_message, attributes)
|
186
|
+
when Vigilant::ERROR
|
187
|
+
@vigilant_logger.error(formatted_message, nil, attributes)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def format_message(message)
|
192
|
+
formatted_message = format_tags(message)
|
193
|
+
attributes = {}
|
194
|
+
|
195
|
+
begin
|
196
|
+
attributes = format_json_attributes(message) if message.start_with?('{') && message.end_with?('}')
|
197
|
+
rescue JSON::ParserError
|
198
|
+
nil
|
199
|
+
end
|
200
|
+
|
201
|
+
begin
|
202
|
+
attributes = format_key_value_attributes(message) if message.match?(/^[\w\-.]+=/)
|
203
|
+
rescue StandardError
|
204
|
+
nil
|
205
|
+
end
|
206
|
+
|
207
|
+
[formatted_message, attributes]
|
208
|
+
end
|
209
|
+
|
210
|
+
def format_json_attributes(message)
|
211
|
+
json_data = JSON.parse(message)
|
212
|
+
attributes = json_data['attributes'] ||= {}
|
213
|
+
|
214
|
+
json_data.each do |key, value|
|
215
|
+
attributes[key] = value
|
216
|
+
json_data.delete(key)
|
217
|
+
end
|
218
|
+
|
219
|
+
attributes['tags'] = @tags
|
220
|
+
attributes
|
221
|
+
end
|
222
|
+
|
223
|
+
def format_key_value_attributes(message)
|
224
|
+
attributes = {}
|
225
|
+
message.split(' ').each do |pair|
|
226
|
+
key, value = pair.split('=', 2)
|
227
|
+
value = case value
|
228
|
+
when /^\d+$/
|
229
|
+
value.to_i
|
230
|
+
when /^\d*\.\d+$/
|
231
|
+
value.to_f
|
232
|
+
else
|
233
|
+
value
|
234
|
+
end
|
235
|
+
attributes[key] = value
|
236
|
+
end
|
237
|
+
|
238
|
+
attributes['tags'] = @tags
|
239
|
+
attributes
|
240
|
+
end
|
241
|
+
|
242
|
+
def format_tags(message)
|
243
|
+
tag_prefix = @tags.map { |t| "[#{t}] " }.join
|
244
|
+
"#{tag_prefix}#{message}"
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/railtie'
|
4
|
+
require 'vigilant-ruby/logger'
|
5
|
+
require 'vigilant-ruby/rails/logger'
|
6
|
+
|
7
|
+
module Vigilant
|
8
|
+
module Rails
|
9
|
+
# Railtie for integrating Vigilant with Rails.
|
10
|
+
class Railtie < ::Rails::Railtie
|
11
|
+
initializer 'vigilant.rails_integration' do
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/vigilant-ruby.rb
CHANGED
@@ -2,21 +2,22 @@
|
|
2
2
|
|
3
3
|
require 'vigilant-ruby/version'
|
4
4
|
require 'vigilant-ruby/logger'
|
5
|
+
require 'vigilant-ruby/rails/railtie' if defined?(Rails)
|
5
6
|
|
6
|
-
# Vigilant is a logging
|
7
|
-
# with asynchronous batch processing and thread-safe operations.
|
7
|
+
# Vigilant is a logging library for the Vigilant platform.
|
8
8
|
module Vigilant
|
9
9
|
class Error < StandardError; end
|
10
10
|
|
11
11
|
# Configuration for the Vigilant logging service.
|
12
12
|
class Configuration
|
13
|
-
attr_accessor :
|
13
|
+
attr_accessor :name, :token, :endpoint, :insecure, :passthrough
|
14
14
|
|
15
15
|
def initialize
|
16
|
+
@name = 'test-app'
|
17
|
+
@token = 'tk_1234567890'
|
16
18
|
@endpoint = 'ingress.vigilant.run'
|
17
19
|
@insecure = false
|
18
|
-
@
|
19
|
-
@passthrough = true
|
20
|
+
@passthrough = false
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
@@ -31,9 +32,10 @@ module Vigilant
|
|
31
32
|
|
32
33
|
def logger
|
33
34
|
@logger ||= Vigilant::Logger.new(
|
35
|
+
name: configuration.name,
|
36
|
+
token: configuration.token,
|
34
37
|
endpoint: configuration.endpoint,
|
35
38
|
insecure: configuration.insecure,
|
36
|
-
token: configuration.token,
|
37
39
|
passthrough: configuration.passthrough
|
38
40
|
)
|
39
41
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vigilant-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vigilant
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-01-
|
10
|
+
date: 2025-01-29 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: json
|
@@ -89,6 +89,8 @@ files:
|
|
89
89
|
- README.md
|
90
90
|
- lib/vigilant-ruby.rb
|
91
91
|
- lib/vigilant-ruby/logger.rb
|
92
|
+
- lib/vigilant-ruby/rails/logger.rb
|
93
|
+
- lib/vigilant-ruby/rails/railtie.rb
|
92
94
|
- lib/vigilant-ruby/version.rb
|
93
95
|
homepage: https://vigilant.run
|
94
96
|
licenses:
|