logcraft 2.0.1 → 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: 1553a9ab5e60ffb3beab51543cefcca10632c655fafa559ed18fe123dad9243a
4
- data.tar.gz: 673ea0771ff8846f9b191f1ab745233cc6002dc2b454b92b97a4e5a8088dea33
3
+ metadata.gz: e77bb5b3fd57a71991d2d62ad60b405d3aec4c204e9aca74d5b8aad73938fbce
4
+ data.tar.gz: ef7f87bcb528b05c9d2a0499a50d00184a949f1d018091fe25e7b5fcacea535d
5
5
  SHA512:
6
- metadata.gz: d440d1fb9a136f36e30911912ec79fff375ad4086b87697399e6530422d92e8749307ac043acdec270a04d507281e009d5f2bf86f5c01ab00192e40de34149d8
7
- data.tar.gz: 5a207a0a5ed398a1ecd95db4252cd0455c265f8a93b4b3b22ab7a60f852d2b8b072ee59366117665476fd35730c134aac958b16d2f1c3670bcf538f08773ea97
6
+ metadata.gz: eae1d4bfb0b539d0850fb306562244e73da2ff4a1745bdd378b9d10cb00dbedfa87938209327a8188bb9ca775827ba09ba1091f2a7a097417d85378cecabe2d0
7
+ data.tar.gz: beb08ae116067d4feed050d8049cbbf97579fa00e905e669709ca0eb433ebfd1a13bef5dbdfbbed5ac85937704f5aa0b5671038f818a45165f29b7d51379393a
data/CHANGELOG.md CHANGED
@@ -4,6 +4,10 @@ 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.1] - 2023-06-13
8
+ ### Added
9
+ - Added support for custom log formatters.
10
+
7
11
  ## [2.0.1] - 2022-10-07
8
12
  ### Fixed
9
13
  - Fixed a bug where request log tracing didn't work with the DataDog integration. Had to move up logging
data/README.md CHANGED
@@ -27,17 +27,17 @@ structured logging solution.
27
27
  ## Table of contents
28
28
 
29
29
  * [Installation](#installation)
30
- * [Rails](#rails)
31
- * [Non-Rails applications](#non-rails-applications)
30
+ * [Rails](#rails)
31
+ * [Non-Rails applications](#non-rails-applications)
32
32
  * [Usage](#usage)
33
- * [Structured logging](#structured-logging)
34
- * [Adding context information to log messages](#adding-context-information-to-log-messages)
35
- * [Rails logging](#rails-logging)
36
- * [The log level](#the-log-level)
37
- * [JSON serialization](#json-serialization)
33
+ * [Structured logging](#structured-logging)
34
+ * [Adding context information to log messages](#adding-context-information-to-log-messages)
35
+ * [Rails logging](#rails-logging)
36
+ * [The log level](#the-log-level)
37
+ * [JSON serialization](#json-serialization)
38
38
  * [Configuration options](#configuration-options)
39
- * [Rails configuration](#rails-configuration)
40
- * [Non-Rails configuration](#non-rails-configuration)
39
+ * [Rails configuration](#rails-configuration)
40
+ * [Non-Rails configuration](#non-rails-configuration)
41
41
  * [Integration with DataDog](#integration-with-datadog)
42
42
  * [RSpec support](#rspec-support)
43
43
  * [Development](#development)
@@ -190,7 +190,7 @@ In more detail:
190
190
 
191
191
  * The `Rails.logger` is set up to be a Logcraft logger with the name `Application`.
192
192
  * Rails's default logging of uncaught errors is modified and instead of spreading the error message across several
193
- lines, Logcraft log every uncaught error in 1 line (per error), including the error's name and context (stack trace,
193
+ lines, Logcraft logs every uncaught error in 1 line (per error), including the error's name and context (stack trace,
194
194
  etc.).
195
195
  * Most importantly, Rails's default request logging - which logs several lines per event during the processing of an
196
196
  action - is replaced by Logcraft's own access log middleware. The end result is an access log that
@@ -215,7 +215,7 @@ Processing by PagesController#welcome as HTML
215
215
  Completed 200 OK in 31ms (Views: 27.3ms | ActiveRecord: 0.0ms)
216
216
 
217
217
  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":13,"duration_sec":0.013}
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":31,"duration_sec":0.031}
219
219
 
220
220
  Formatted for readability:
221
221
  {
@@ -235,8 +235,8 @@ Formatted for readability:
235
235
  "action": "welcome"
236
236
  },
237
237
  "response_status_code": 200,
238
- "duration": 13,
239
- "duration_sec": 0.013
238
+ "duration": 31,
239
+ "duration_sec": 0.031
240
240
  }
241
241
  ```
242
242
 
@@ -275,6 +275,7 @@ I highly recommend using the [Oj](https://github.com/ohler55/oj) gem which - if
275
275
  picked up by Logcraft, as it is significantly faster and will serialize your messages as you would expect.
276
276
 
277
277
  In a nutshell:
278
+
278
279
  ```ruby
279
280
  # With default ActiveSupport serialization
280
281
  Rails.logger.info 'foo > bar'
@@ -293,8 +294,9 @@ Logcraft provides the following configuration options for Rails:
293
294
 
294
295
  | Option | Default value | Description |
295
296
  |-------------------------------------------------------|--------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
296
- | logcraft.global_context | `{}` | A global log context that will be included in every log message. Must be either a Hash or a lambda/Proc returning a Hash. |
297
- | logcraft.layout_options | `{}` | Custom options for the log layout. Currently only the `level_formatter` option is supported (see examples). |
297
+ | 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. |
298
+ | logcraft.layout_options.formatter | `lambda \| proc` | A custom formatter for the entire log event. Must return a single string (see examples for usage). |
299
+ | logcraft.layout_options.level_formatter | `lambda \| proc` | A custom formatter for the log level specifically (see examples for usage). |
298
300
  | logcraft.access_log.logger_name | `'AccessLog'` | The name of the logger emitting access log messages. |
299
301
  | logcraft.access_log.exclude_paths | `[]` | A list of paths (array of strings or RegExps) not to include in the access log. |
300
302
  | logcraft.access_log.log_only_whitelisted_params | `false` | If `true`, the access log will only contain whitelisted parameters. |
@@ -305,7 +307,7 @@ Logcraft provides the following configuration options for Rails:
305
307
  Examples:
306
308
 
307
309
  ```ruby
308
- # Use these options in your Rails configuration files (e.g. application.rb)
310
+ # Use these options in your Rails configuration files (e.g. config/application.rb or config/environments/*.rb)
309
311
 
310
312
  # Set up a global context you want to see in every log message
311
313
  config.logcraft.global_context = -> do
@@ -315,10 +317,13 @@ config.logcraft.global_context = -> do
315
317
  }
316
318
  end
317
319
 
320
+ # Set up a custom log formatter (e.g. output logs in YAML format in the development environment - config/environments/development.rb)
321
+ config.logcraft.layout_options.formatter = ->(event) { YAML.dump event }
322
+ # or just make the JSON more readable
323
+ config.logcraft.layout_options.formatter = ->(event) { JSON.pretty_generate(event) + "\n----------------\n" }
324
+
318
325
  # 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
- }
326
+ config.logcraft.layout_options.level_formatter = ->(level_number) { (level_number + 2) * 10 }
322
327
  Rails.logger.error('Boom!')
323
328
  # => {...,"level":50,"message":"Boom!"}
324
329
 
@@ -356,7 +361,7 @@ config.logcraft.global_context = -> do
356
361
  service: correlation.service.to_s,
357
362
  version: correlation.version.to_s
358
363
  },
359
- ddsource: ['ruby']
364
+ ddsource: 'ruby'
360
365
  }
361
366
  end
362
367
  ```
@@ -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
- @level_formatter = options.fetch :level_formatter, ->(level) { Logging::LNAMES[level] }
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
- MultiJson.dump(log_entry) + "\n"
20
+ @formatter.call log_entry
17
21
  end
18
22
 
19
23
  private
@@ -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'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Logcraft
4
- VERSION = "2.0.1"
4
+ VERSION = "2.1"
5
5
  end
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.0.1
4
+ version: '2.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zoltan Ormandi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-07 00:00:00.000000000 Z
11
+ date: 2023-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logging
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
- description:
83
+ description:
84
84
  email:
85
85
  - zoltan.ormandi@gmail.com
86
86
  executables: []
@@ -120,7 +120,7 @@ metadata:
120
120
  homepage_uri: https://github.com/zormandi/logcraft
121
121
  source_code_uri: https://github.com/zormandi/logcraft
122
122
  changelog_uri: https://github.com/zormandi/logcraft/blob/main/CHANGELOG.md
123
- post_install_message:
123
+ post_install_message:
124
124
  rdoc_options: []
125
125
  require_paths:
126
126
  - lib
@@ -135,8 +135,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  - !ruby/object:Gem::Version
136
136
  version: '0'
137
137
  requirements: []
138
- rubygems_version: 3.3.7
139
- signing_key:
138
+ rubygems_version: 3.1.6
139
+ signing_key:
140
140
  specification_version: 4
141
141
  summary: A zero-configuration structured logging solution for pure Ruby or Ruby on
142
142
  Rails projects.