Class: SemanticLogger::Appender::CloudwatchLogs

Inherits:
Subscriber show all
Defined in:
lib/semantic_logger/appender/cloudwatch_logs.rb

Instance Attribute Summary collapse

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#console_output?, #level, #should_log?

Methods inherited from Base

#backtrace, #fast_tag, #level, #level=, #measure, #named_tags, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags

Constructor Details

#initialize(*args, group:, client_kwargs: {}, stream: nil, create_group: false, create_stream: true, force_flush_interval_seconds: 5, max_buffered_events: 4_000, **kwargs, &block) ⇒ CloudwatchLogs

Create CloudWatch Logs Appender

Parameters:

group: [String]
  Log group name

client_kwargs: [Hash]
  A hash to be passed to Aws::CloudWatchLogs::Client.new
  Default: {}

stream: [String]
  Log stream name
  Default: SemanticLogger.host

create_group: [Boolean]
  If the missing log group should be automatically created.
  Default: false

create_stream: [Boolean]
  If the missing log stream should be automatically created.
  Default: true

force_flush_interval_seconds: [Integer]
  Flush buffered logs every X seconds, regardless of the current buffer size.
  Default: 5

max_buffered_events: [Integer]
  Flush buffered logs if they are above the currently set size.
  Note that currently CloudWatch Logs has 10000 hard limit.
  Default: 4000


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 56

def initialize(
  *args,
  group:,
  client_kwargs: {},
  stream: nil,
  create_group: false,
  create_stream: true,
  force_flush_interval_seconds: 5,
  max_buffered_events: 4_000,
  **kwargs,
  &block
)
  @group = group
  @client_kwargs = client_kwargs
  @stream = stream
  @create_group = create_group
  @create_stream = create_stream
  @force_flush_interval_seconds = force_flush_interval_seconds
  @max_buffered_events = max_buffered_events

  super(*args, **kwargs, &block)
  reopen
end

Instance Attribute Details

#buffered_logsObject (readonly)

Returns the value of attribute buffered_logs.



23
24
25
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 23

def buffered_logs
  @buffered_logs
end

#clientObject (readonly)

Returns the value of attribute client.



23
24
25
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 23

def client
  @client
end

#client_kwargsObject (readonly)

Returns the value of attribute client_kwargs.



23
24
25
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 23

def client_kwargs
  @client_kwargs
end

#create_groupObject (readonly)

Returns the value of attribute create_group.



23
24
25
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 23

def create_group
  @create_group
end

#create_streamObject (readonly)

Returns the value of attribute create_stream.



23
24
25
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 23

def create_stream
  @create_stream
end

#force_flush_interval_secondsObject (readonly)

Returns the value of attribute force_flush_interval_seconds.



23
24
25
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 23

def force_flush_interval_seconds
  @force_flush_interval_seconds
end

#groupObject (readonly)

Returns the value of attribute group.



23
24
25
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 23

def group
  @group
end

#max_buffered_eventsObject (readonly)

Returns the value of attribute max_buffered_events.



23
24
25
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 23

def max_buffered_events
  @max_buffered_events
end

#taskObject (readonly)

Returns the value of attribute task.



23
24
25
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 23

def task
  @task
end

Instance Method Details

#closeObject



91
92
93
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 91

def close
  task.shutdown
end

#default_formatterObject

Use JSON Formatter by default



107
108
109
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 107

def default_formatter
  SemanticLogger::Formatters::Json.new
end

#flushObject



87
88
89
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 87

def flush
  task.execute while buffered_logs.size.positive?
end

#log(log) ⇒ Object

Method called to log an event



81
82
83
84
85
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 81

def log(log)
  buffered_logs << log

  put_log_events if buffered_logs.size >= max_buffered_events
end

#reopenObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/semantic_logger/appender/cloudwatch_logs.rb', line 95

def reopen
  @buffered_logs = Concurrent::Array.new
  @client = Aws::CloudWatchLogs::Client.new(client_kwargs)

  @task = Concurrent::TimerTask.new(execution_interval: force_flush_interval_seconds,
                                    interval_type:      :fixed_rate) do
    put_log_events
  end
  @task.execute
end