loggun 0.4.1 → 0.5.0

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: cfd41231567df2c15cca3c6ba500fc8228dcf03ac808b2adb452385be59d4217
4
- data.tar.gz: 86c26424200b7c443a4160770d7b3f0d22490db5e0ac3c41fffdd2dadd795d9e
3
+ metadata.gz: 2596dacc5a1bea907afccdabec538190ba574a623c19221189798a1a9f320c74
4
+ data.tar.gz: 2ac4b17d0113f10c3742bf929165c5a6bf95c7273a9e8a2027fe48c170dfef3c
5
5
  SHA512:
6
- metadata.gz: '058933cfb04111fe6f0f106e97efde068932b575bfc2be6bdfee0f419db586b07e0a11025eba01b65d12f71a9ef8121dc88e4c53dc39037cd95d5ee6d1ccd90b'
7
- data.tar.gz: ebd06ae4867bb4b8cc03fa0ef1815ca73f58af294fab484f3f1207c01d3b4c305cfaa442a6b82771524a909fdb88cb68a8421c760ba8099731957a158c910858
6
+ metadata.gz: a279ad6a07b077bf7af95aaeba39cae3085808e3e9ab77798a950d192d8e8c9f406b7f49c97f46d80bd7b8b12c5fe77bc38c0fff47c2ee0b055363862d8a45eb
7
+ data.tar.gz: 410fc2d3b572a729f5e7c0ecde19426384b401dd8f2d87b89b20731c4dbcba20b89231b463e3e49c76220bdfafd7d058e75dfa3af88faec1b6645001485bb330
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.0 - 2020-09-22
4
+ * features
5
+ * `config.log_format` - for control full log rows format
6
+ * `config.exclude_keys`, `config.include_keys` - for include or exclude keys from `:json` log_format
7
+
3
8
  ## 0.4.1 - 2020-07-28
4
9
  * fixes
5
10
  * fill type with class name for empty `log_entity_name`
data/README.md CHANGED
@@ -92,6 +92,10 @@ end
92
92
  - `parent_transaction_to_message` — если `true`, то значение `parent_transaction` будет добавлено в тело логируемого сообщения.
93
93
 
94
94
  Ключ `parent_transaction` в шаблоне `pattern` можно использовать вне зависимости от значения этой настройки.
95
+
96
+ - `force_utc` — если `true`, то значение `time` будет переведено в UTC.
97
+
98
+ По умолчанию `false`.
95
99
 
96
100
  - `message_format` — формат переменной `message` в шаблоне `pattern`.
97
101
 
@@ -99,6 +103,23 @@ end
99
103
 
100
104
  - `:json` — `message` логируется как JSON-строка;
101
105
  - `:key_value` — `message` логируется в формате `key1=value1 key2=value2`.
106
+
107
+ - `log_format` — формат лога.
108
+
109
+ Доступные значения:
110
+
111
+ - `:json` — лог формируется как JSON-строка игнорируя шаблон `pattern`;
112
+ - `:plain` — лог формируется по шаблону `pattern`.
113
+
114
+ По умолчанию `:plain`.
115
+
116
+ - `exclude_keys` — список ключей, которые будут исключены из лога.
117
+
118
+ Используется, если `log_format` имеет значение `:json` и список `only_keys` пуст.
119
+
120
+ - `only_keys` — список ключей, которые будут включены в JSON-строку.
121
+
122
+ Используется, если `log_format` имеет значение `:json`.
102
123
 
103
124
  - `modifiers` — модификаторы для переопределения формата логирования указанного компонента. См. «[Модификаторы](#модификаторы)».
104
125
 
@@ -9,6 +9,8 @@ module Loggun
9
9
  pattern: '%{time} - %{pid} %{severity} %{type} %{tags_text} %{message}',
10
10
  parent_transaction_to_message: true,
11
11
  message_format: :json,
12
+ log_format: :plain,
13
+ force_utc: false,
12
14
  precision: :milliseconds,
13
15
  incoming_http: {
14
16
  controllers: %w[ApplicationController],
@@ -22,15 +24,20 @@ module Loggun
22
24
  }.freeze
23
25
  DEFAULT_MODIFIERS = %i[rails active_record sidekiq clockwork outgoing_http].freeze
24
26
  MESSAGE_FORMATS = %i[json key_value].freeze
27
+ LOG_FORMATS = %i[json plain].freeze
25
28
 
26
29
  attr_accessor(
27
30
  :formatter,
28
31
  :pattern,
29
32
  :parent_transaction_to_message,
30
33
  :message_format,
34
+ :log_format,
35
+ :force_utc,
31
36
  :precision,
32
37
  :modifiers,
33
- :custom_modifiers
38
+ :custom_modifiers,
39
+ :exclude_keys,
40
+ :only_keys
34
41
  )
35
42
 
36
43
  def initialize
@@ -39,8 +46,12 @@ module Loggun
39
46
  @pattern = DEFAULTS[:pattern]
40
47
  @parent_transaction_to_message = DEFAULTS[:parent_transaction_to_message]
41
48
  @message_format = DEFAULTS[:message_format]
49
+ @log_format = DEFAULTS[:log_format]
50
+ @force_utc = DEFAULTS[:force_utc]
42
51
  @modifiers = Loggun::OrderedOptions.new
43
52
  @custom_modifiers = []
53
+ @exclude_keys = []
54
+ @only_keys = []
44
55
  set_default_modifiers
45
56
  end
46
57
 
@@ -65,9 +76,13 @@ module Loggun
65
76
  end
66
77
 
67
78
  def check_config
68
- return if MESSAGE_FORMATS.include? instance.message_format
79
+ unless MESSAGE_FORMATS.include? instance.message_format
80
+ raise FailureConfiguration, 'Unknown value for message_format'
81
+ end
69
82
 
70
- raise FailureConfiguration, 'Unknown value for message_format'
83
+ unless LOG_FORMATS.include? instance.log_format
84
+ raise FailureConfiguration, 'Unknown value for log_format'
85
+ end
71
86
  end
72
87
 
73
88
  def setup_formatter(app, formatter = nil)
@@ -7,17 +7,13 @@ module Loggun
7
7
 
8
8
  def call(severity, time, _program_name, message, loggun_type: nil)
9
9
  data = Hash.new(DEFAULT_VALUE)
10
- data[:time] = time.iso8601(config.timestamp_precision)
11
- data[:pid] = Process.pid
12
-
13
- if message.is_a?(Hash)
14
- if config.parent_transaction_to_message && parent_transaction
15
- message[:parent_transaction] = parent_transaction
16
- end
17
- message = format_message(message)
18
- end
10
+ time = time.utc if config.force_utc
11
+ message = prepare_message(message)
19
12
 
20
- data[:message] = message.to_s.tr("\r\n", ' ').strip
13
+ data[:timestamp] = time.iso8601(config.timestamp_precision)
14
+ data[:time] = data[:timestamp] if config.log_format == :plain
15
+ data[:pid] = Process.pid
16
+ data[:message] = message
21
17
  data[:severity] = severity&.to_s || 'INFO'
22
18
  data[:tags_text] = tags_text
23
19
  data[:type] = loggun_type || Loggun.type || DEFAULT_VALUE.dup
@@ -29,7 +25,13 @@ module Loggun
29
25
  data[:type] = "#{data[:type]}##{data[:transaction_id]}"
30
26
  end
31
27
 
32
- format(config.pattern + "\n", data)
28
+ if config.log_format == :json
29
+ data.except!(*config.exclude_keys) if config.only_keys.empty?
30
+ data.slice!(*config.only_keys) if config.only_keys.any?
31
+ JSON.generate(data) + "\n"
32
+ else
33
+ format(config.pattern + "\n", data)
34
+ end
33
35
  end
34
36
 
35
37
  def tagged(*tags)
@@ -69,6 +71,21 @@ module Loggun
69
71
 
70
72
  private
71
73
 
74
+ def prepare_message(message)
75
+ if message.is_a?(Hash)
76
+ if config.parent_transaction_to_message && parent_transaction
77
+ message[:parent_transaction] = parent_transaction
78
+ end
79
+ message = format_message(message) if config.log_format == :plain
80
+ end
81
+
82
+ if config.log_format == :plain
83
+ message.to_s.tr("\r\n", ' ').strip
84
+ else
85
+ message
86
+ end
87
+ end
88
+
72
89
  def parent_transaction
73
90
  return unless Loggun.parent_type && Loggun.parent_transaction_id
74
91
 
@@ -86,7 +86,10 @@ module Loggun
86
86
  attrs[:hidden] = { error: { backtrace: error.backtrace } }
87
87
  end
88
88
  end
89
- attrs[:message] = args unless args.empty?
89
+ unless args.empty?
90
+ attrs[:message] = args.first if args.size == 1
91
+ attrs[:message] ||= args
92
+ end
90
93
 
91
94
  with_log_type(type) do
92
95
  logger.send(method_name, **attrs, &block)
@@ -1,3 +1,3 @@
1
1
  module Loggun
2
- VERSION = '0.4.1'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loggun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksandr Noskov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-07-28 00:00:00.000000000 Z
12
+ date: 2020-09-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler