Class: Smplkit::Logging::SmplLogGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/logging/models.rb

Overview

A log group resource — a hierarchical bag of loggers with a shared configured level.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil, key:, id: nil, name: nil, level: nil, description: nil, group: nil, environments: nil, created_at: nil, updated_at: nil) ⇒ SmplLogGroup

Returns a new instance of SmplLogGroup.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/smplkit/logging/models.rb', line 184

def initialize(client = nil, key:, id: nil, name: nil, level: nil,
               description: nil, group: nil, environments: nil,
               created_at: nil, updated_at: nil)
  @client = client
  @id = id
  @key = key
  @name = name
  @level = level
  @description = description
  @group = group
  @environments = Logging.convert_environments(environments)
  @created_at = created_at
  @updated_at = updated_at
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



181
182
183
# File 'lib/smplkit/logging/models.rb', line 181

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



181
182
183
# File 'lib/smplkit/logging/models.rb', line 181

def description
  @description
end

#groupObject

Returns the value of attribute group.



181
182
183
# File 'lib/smplkit/logging/models.rb', line 181

def group
  @group
end

#idObject

Returns the value of attribute id.



181
182
183
# File 'lib/smplkit/logging/models.rb', line 181

def id
  @id
end

#keyObject

Returns the value of attribute key.



181
182
183
# File 'lib/smplkit/logging/models.rb', line 181

def key
  @key
end

#levelObject

Returns the value of attribute level.



181
182
183
# File 'lib/smplkit/logging/models.rb', line 181

def level
  @level
end

#nameObject

Returns the value of attribute name.



181
182
183
# File 'lib/smplkit/logging/models.rb', line 181

def name
  @name
end

#updated_atObject

Returns the value of attribute updated_at.



181
182
183
# File 'lib/smplkit/logging/models.rb', line 181

def updated_at
  @updated_at
end

Instance Method Details

#_apply(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/smplkit/logging/models.rb', line 268

def _apply(other)
  @id = other.id
  @key = other.key
  @name = other.name
  @level = other.level
  @description = other.description
  @group = other.group
  @environments = other.environments
  @created_at = other.created_at
  @updated_at = other.updated_at
end

#clear_all_environment_levelsObject

Remove all per-environment level overrides.



233
234
235
# File 'lib/smplkit/logging/models.rb', line 233

def clear_all_environment_levels
  @environments = {}
end

#clear_level(environment: nil) ⇒ Object

Remove a log level.

With environment: nil (the default), removes the base log level (the group then inherits from its parent group / ancestor / system default). With environment: “…”, removes the per-environment override only. Changes are local until save.



224
225
226
227
228
229
230
# File 'lib/smplkit/logging/models.rb', line 224

def clear_level(environment: nil)
  if environment.nil?
    @level = nil
  else
    @environments.delete(environment)
  end
end

#deletevoid Also known as: delete!

This method returns an undefined value.

Delete this group from the server.

Raises:

  • (RuntimeError)

    If the group was constructed without a client.

  • (Smplkit::NotFoundError)

    If the group no longer exists.



260
261
262
263
264
# File 'lib/smplkit/logging/models.rb', line 260

def delete
  raise "SmplLogGroup was constructed without a client; cannot delete" if @client.nil?

  @client.delete(@key)
end

#environmentsObject

Read-only view of per-environment level overrides. Mutate via set_level / clear_level (with environment: “…”).



201
202
203
# File 'lib/smplkit/logging/models.rb', line 201

def environments
  @environments.dup
end

#saveself Also known as: save!

Persist this group to the server (create or update).

Returns:

  • (self)

    this group, refreshed from the server response.

Raises:

  • (RuntimeError)

    If the group was constructed without a client.



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/smplkit/logging/models.rb', line 241

def save
  raise "SmplLogGroup was constructed without a client; cannot save" if @client.nil?

  updated =
    if @created_at.nil?
      @client._create_log_group(self)
    else
      @client._update_log_group(self)
    end
  _apply(updated)
  self
end

#set_level(level, environment: nil) ⇒ Object

Set the log level.

With environment: nil (the default), sets the base log level used when no environment-specific override applies. With environment: “…”, sets the per-environment override. Changes are local until save.



210
211
212
213
214
215
216
# File 'lib/smplkit/logging/models.rb', line 210

def set_level(level, environment: nil)
  if environment.nil?
    @level = level
  else
    @environments[environment] = LoggerEnvironment.new(level: level)
  end
end