logcraft 2.0.1 → 2.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 +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile +2 -2
- data/README.md +26 -20
- data/gemfiles/rails_7.1.gemfile +13 -0
- data/lib/logcraft/log_layout.rb +6 -2
- data/lib/logcraft/railtie.rb +6 -2
- data/lib/logcraft/rspec/helpers.rb +1 -1
- data/lib/logcraft/rspec.rb +1 -1
- data/lib/logcraft/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f83b1cb57e7d3a8072d8fc2d7b5b7d952cd9f96eb72edecb88244179384d4f61
|
4
|
+
data.tar.gz: 2f679955495954defc5ba217f0ef36318f0fae6e8d050c48b834c582b8742ff8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad933459ec43e29bcf9c13627d591ded635601e1a2df160c7178c98abadb5f103bacdd3b160d5ab7900b83fa0b254d701be6eaeb7e1277dfc211a76303f4b376
|
7
|
+
data.tar.gz: a1693794447ab3fc3180c9aa905d397fd4ae7f4e23fe7474d7c7817cfef8acecf41935db8f4a1589ee691133af7186d486d60a7df432ff02230a089483010fcf
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## [2.2] - 2023-10-23
|
8
|
+
### Added
|
9
|
+
- Added support for Rails 7.1.
|
10
|
+
|
11
|
+
## [2.1] - 2023-06-13
|
12
|
+
### Added
|
13
|
+
- Added support for custom log formatters.
|
14
|
+
|
7
15
|
## [2.0.1] - 2022-10-07
|
8
16
|
### Fixed
|
9
17
|
- Fixed a bug where request log tracing didn't work with the DataDog integration. Had to move up logging
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -17,6 +17,7 @@ Logcraft's purpose is threefold:
|
|
17
17
|
Logcraft supports:
|
18
18
|
|
19
19
|
* [Ruby](https://www.ruby-lang.org) 2.6 and up (tested with 2.6, 2.7, 3.0 and 3.1)
|
20
|
+
* [Rails](https://rubyonrails.org/) 5 and up (tested with 5.2, 6.0, 6.1, 7.0 and 7.1)
|
20
21
|
* [Rails](https://rubyonrails.org/) 5 and up (tested with 5.2, 6.0, 6.1 and 7.0)
|
21
22
|
* [Sidekiq](https://github.com/mperham/sidekiq) integration is provided via
|
22
23
|
the [Logcraft::Sidekiq](https://github.com/zormandi/logcraft-sidekiq) gem
|
@@ -27,17 +28,17 @@ structured logging solution.
|
|
27
28
|
## Table of contents
|
28
29
|
|
29
30
|
* [Installation](#installation)
|
30
|
-
|
31
|
-
|
31
|
+
* [Rails](#rails)
|
32
|
+
* [Non-Rails applications](#non-rails-applications)
|
32
33
|
* [Usage](#usage)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
* [Structured logging](#structured-logging)
|
35
|
+
* [Adding context information to log messages](#adding-context-information-to-log-messages)
|
36
|
+
* [Rails logging](#rails-logging)
|
37
|
+
* [The log level](#the-log-level)
|
38
|
+
* [JSON serialization](#json-serialization)
|
38
39
|
* [Configuration options](#configuration-options)
|
39
|
-
|
40
|
-
|
40
|
+
* [Rails configuration](#rails-configuration)
|
41
|
+
* [Non-Rails configuration](#non-rails-configuration)
|
41
42
|
* [Integration with DataDog](#integration-with-datadog)
|
42
43
|
* [RSpec support](#rspec-support)
|
43
44
|
* [Development](#development)
|
@@ -190,7 +191,7 @@ In more detail:
|
|
190
191
|
|
191
192
|
* The `Rails.logger` is set up to be a Logcraft logger with the name `Application`.
|
192
193
|
* Rails's default logging of uncaught errors is modified and instead of spreading the error message across several
|
193
|
-
lines, Logcraft
|
194
|
+
lines, Logcraft logs every uncaught error in 1 line (per error), including the error's name and context (stack trace,
|
194
195
|
etc.).
|
195
196
|
* Most importantly, Rails's default request logging - which logs several lines per event during the processing of an
|
196
197
|
action - is replaced by Logcraft's own access log middleware. The end result is an access log that
|
@@ -215,7 +216,7 @@ Processing by PagesController#welcome as HTML
|
|
215
216
|
Completed 200 OK in 31ms (Views: 27.3ms | ActiveRecord: 0.0ms)
|
216
217
|
|
217
218
|
With Logcraft:
|
218
|
-
{"timestamp":"2022-06-26T18:07:08.103+02:00","level":"INFO","logger":"AccessLog","hostname":"MacbookPro.local","pid":80908,"request_id":"9a43631b-284c-4677-9d08-9c1cc5c7d3a7","message":"GET /welcome?subsession_id=34ea8596f9764f475f81158667bc2654 - 200 (OK)","remote_ip":"127.0.0.1","method":"GET","path":"/welcome?subsession_id=34ea8596f9764f475f81158667bc2654","params":{"subsession_id":"34ea8596f9764f475f81158667bc2654","controller":"pages","action":"welcome"},"response_status_code":200,"duration":
|
219
|
+
{"timestamp":"2022-06-26T18:07:08.103+02:00","level":"INFO","logger":"AccessLog","hostname":"MacbookPro.local","pid":80908,"request_id":"9a43631b-284c-4677-9d08-9c1cc5c7d3a7","message":"GET /welcome?subsession_id=34ea8596f9764f475f81158667bc2654 - 200 (OK)","remote_ip":"127.0.0.1","method":"GET","path":"/welcome?subsession_id=34ea8596f9764f475f81158667bc2654","params":{"subsession_id":"34ea8596f9764f475f81158667bc2654","controller":"pages","action":"welcome"},"response_status_code":200,"duration":31,"duration_sec":0.031}
|
219
220
|
|
220
221
|
Formatted for readability:
|
221
222
|
{
|
@@ -235,8 +236,8 @@ Formatted for readability:
|
|
235
236
|
"action": "welcome"
|
236
237
|
},
|
237
238
|
"response_status_code": 200,
|
238
|
-
"duration":
|
239
|
-
"duration_sec": 0.
|
239
|
+
"duration": 31,
|
240
|
+
"duration_sec": 0.031
|
240
241
|
}
|
241
242
|
```
|
242
243
|
|
@@ -275,6 +276,7 @@ I highly recommend using the [Oj](https://github.com/ohler55/oj) gem which - if
|
|
275
276
|
picked up by Logcraft, as it is significantly faster and will serialize your messages as you would expect.
|
276
277
|
|
277
278
|
In a nutshell:
|
279
|
+
|
278
280
|
```ruby
|
279
281
|
# With default ActiveSupport serialization
|
280
282
|
Rails.logger.info 'foo > bar'
|
@@ -293,8 +295,9 @@ Logcraft provides the following configuration options for Rails:
|
|
293
295
|
|
294
296
|
| Option | Default value | Description |
|
295
297
|
|-------------------------------------------------------|--------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
296
|
-
| logcraft.global_context | `{}`
|
297
|
-
| logcraft.layout_options
|
298
|
+
| logcraft.global_context | `{} \| lambda \| proc` | A global log context that will be included in every log message. Must be either a Hash or a lambda/proc returning a Hash. |
|
299
|
+
| logcraft.layout_options.formatter | `lambda \| proc` | A custom formatter for the entire log event. Must return a single string (see examples for usage). |
|
300
|
+
| logcraft.layout_options.level_formatter | `lambda \| proc` | A custom formatter for the log level specifically (see examples for usage). |
|
298
301
|
| logcraft.access_log.logger_name | `'AccessLog'` | The name of the logger emitting access log messages. |
|
299
302
|
| logcraft.access_log.exclude_paths | `[]` | A list of paths (array of strings or RegExps) not to include in the access log. |
|
300
303
|
| logcraft.access_log.log_only_whitelisted_params | `false` | If `true`, the access log will only contain whitelisted parameters. |
|
@@ -305,7 +308,7 @@ Logcraft provides the following configuration options for Rails:
|
|
305
308
|
Examples:
|
306
309
|
|
307
310
|
```ruby
|
308
|
-
# Use these options in your Rails configuration files (e.g. application.rb)
|
311
|
+
# Use these options in your Rails configuration files (e.g. config/application.rb or config/environments/*.rb)
|
309
312
|
|
310
313
|
# Set up a global context you want to see in every log message
|
311
314
|
config.logcraft.global_context = -> do
|
@@ -315,10 +318,13 @@ config.logcraft.global_context = -> do
|
|
315
318
|
}
|
316
319
|
end
|
317
320
|
|
321
|
+
# Set up a custom log formatter (e.g. output logs in YAML format in the development environment - config/environments/development.rb)
|
322
|
+
config.logcraft.layout_options.formatter = ->(event) { YAML.dump event }
|
323
|
+
# or just make the JSON more readable
|
324
|
+
config.logcraft.layout_options.formatter = ->(event) { JSON.pretty_generate(event) + "\n----------------\n" }
|
325
|
+
|
318
326
|
# Set up a custom log level formatter (e.g. Ougai-like numbers)
|
319
|
-
config.logcraft.layout_options = {
|
320
|
-
level_formatter: ->(level_number) { (level_number + 2) * 10 }
|
321
|
-
}
|
327
|
+
config.logcraft.layout_options.level_formatter = ->(level_number) { (level_number + 2) * 10 }
|
322
328
|
Rails.logger.error('Boom!')
|
323
329
|
# => {...,"level":50,"message":"Boom!"}
|
324
330
|
|
@@ -356,7 +362,7 @@ config.logcraft.global_context = -> do
|
|
356
362
|
service: correlation.service.to_s,
|
357
363
|
version: correlation.version.to_s
|
358
364
|
},
|
359
|
-
ddsource:
|
365
|
+
ddsource: 'ruby'
|
360
366
|
}
|
361
367
|
end
|
362
368
|
```
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in logcraft.gemspec
|
6
|
+
gemspec path: '..'
|
7
|
+
|
8
|
+
group :test do
|
9
|
+
gem 'rails', '~> 7.1.0'
|
10
|
+
gem 'rspec-rails', '~> 6.0'
|
11
|
+
gem 'sqlite3', '~> 1.4'
|
12
|
+
gem 'net-smtp', require: false
|
13
|
+
end
|
data/lib/logcraft/log_layout.rb
CHANGED
@@ -4,16 +4,20 @@ require 'time'
|
|
4
4
|
|
5
5
|
module Logcraft
|
6
6
|
class LogLayout < Logging::Layout
|
7
|
+
JSON_FORMATTER = ->(event) { MultiJson.dump(event) + "\n" }.freeze
|
8
|
+
LOGGING_LEVEL_FORMATTER = ->(level) { Logging::LNAMES[level] }.freeze
|
9
|
+
|
7
10
|
def initialize(global_context = {}, options = {})
|
8
11
|
@global_context = global_context
|
9
|
-
@
|
12
|
+
@formatter = options.fetch :formatter, JSON_FORMATTER
|
13
|
+
@level_formatter = options.fetch :level_formatter, LOGGING_LEVEL_FORMATTER
|
10
14
|
end
|
11
15
|
|
12
16
|
def format(event)
|
13
17
|
log_entry = background_of(event).merge evaluated_global_context,
|
14
18
|
dynamic_log_context,
|
15
19
|
message_from(event.data)
|
16
|
-
|
20
|
+
@formatter.call log_entry
|
17
21
|
end
|
18
22
|
|
19
23
|
private
|
data/lib/logcraft/railtie.rb
CHANGED
@@ -6,7 +6,7 @@ module Logcraft
|
|
6
6
|
class Railtie < ::Rails::Railtie
|
7
7
|
config.logcraft = ActiveSupport::OrderedOptions.new
|
8
8
|
config.logcraft.global_context = {}
|
9
|
-
config.logcraft.layout_options =
|
9
|
+
config.logcraft.layout_options = ActiveSupport::OrderedOptions.new
|
10
10
|
|
11
11
|
config.logcraft.access_log = ActiveSupport::OrderedOptions.new
|
12
12
|
config.logcraft.access_log.logger_name = 'AccessLog'
|
@@ -45,7 +45,11 @@ module Logcraft
|
|
45
45
|
end
|
46
46
|
|
47
47
|
config.before_configuration do |app|
|
48
|
-
app.config.logger =
|
48
|
+
app.config.logger = if defined? ActiveSupport::BroadcastLogger
|
49
|
+
ActiveSupport::BroadcastLogger.new Logcraft.logger('Application')
|
50
|
+
else
|
51
|
+
Logcraft.logger 'Application'
|
52
|
+
end
|
49
53
|
app.config.log_level = ENV['LOG_LEVEL'] || :info
|
50
54
|
end
|
51
55
|
end
|
data/lib/logcraft/rspec.rb
CHANGED
@@ -7,7 +7,7 @@ require_relative 'rspec/matchers'
|
|
7
7
|
require_relative 'log_layout'
|
8
8
|
|
9
9
|
RSpec.configure do |config|
|
10
|
-
config.include
|
10
|
+
config.include Logcraft::RSpec::Helpers
|
11
11
|
config.before(:suite) do
|
12
12
|
Logging.appenders.string_io('__logcraft_stringio__', layout: Logging.logger.root.appenders.first&.layout || Logcraft::LogLayout.new)
|
13
13
|
config.capture_log_messages to: '__logcraft_stringio__'
|
data/lib/logcraft/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logcraft
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: '2.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zoltan Ormandi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: logging
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- gemfiles/rails_6.0.gemfile
|
98
98
|
- gemfiles/rails_6.1.gemfile
|
99
99
|
- gemfiles/rails_7.0.gemfile
|
100
|
+
- gemfiles/rails_7.1.gemfile
|
100
101
|
- lib/logcraft.rb
|
101
102
|
- lib/logcraft/log_context_helper.rb
|
102
103
|
- lib/logcraft/log_layout.rb
|
@@ -135,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
136
|
- !ruby/object:Gem::Version
|
136
137
|
version: '0'
|
137
138
|
requirements: []
|
138
|
-
rubygems_version: 3.
|
139
|
+
rubygems_version: 3.4.10
|
139
140
|
signing_key:
|
140
141
|
specification_version: 4
|
141
142
|
summary: A zero-configuration structured logging solution for pure Ruby or Ruby on
|