Class: TestaLogger::Logger

Inherits:
Object
  • Object
show all
Includes:
Persistence
Defined in:
lib/testa_logger/logger.rb,
lib/testa_logger/logger/options.rb,
lib/testa_logger/logger/dispatcher.rb,
lib/testa_logger/logger/persistence.rb

Overview

noinspection RubyTooManyMethodsInspection

Defined Under Namespace

Modules: Persistence Classes: Dispatcher, Options

Constant Summary collapse

TAG =
"TestaLogger"
DEBUG =

Low-level information, mostly for developers.

0
INFO =

Generic (useful) information about system operation.

1
WARN =

A warning.

2
ERROR =

A handleable error condition.

3
FATAL =

An unhandleable error that results in a program crash.

4
UNKNOWN =

An unknown message that should always be logged.

5
NO_TAG =
"NO-TAG"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#init_s3_client, #persist, #persist_with_record

Constructor Details

#initialize(app, group, subgroup = nil, options = {}) ⇒ Logger

Returns a new instance of Logger.



36
37
38
39
40
41
42
43
44
45
# File 'lib/testa_logger/logger.rb', line 36

def initialize(app, group, subgroup = nil, options = {})
  @app = app
  @group = group
  @subgroup = subgroup
  @options = Options.new(app, group, subgroup, options)
  create_log_file
  setup_dispatcher
  start_write_thread
  init_s3_client if @options.persist
end

Instance Attribute Details

#appString

Returns:

  • (String)


13
14
15
# File 'lib/testa_logger/logger.rb', line 13

def app
  @app
end

#groupString

Returns:

  • (String)


13
14
15
# File 'lib/testa_logger/logger.rb', line 13

def group
  @group
end

#optionsOptions

Returns:



16
17
18
# File 'lib/testa_logger/logger.rb', line 16

def options
  @options
end

#subgroupString

Returns:

  • (String)


13
14
15
# File 'lib/testa_logger/logger.rb', line 13

def subgroup
  @subgroup
end

#write_threadThread

Returns:

  • (Thread)


19
20
21
# File 'lib/testa_logger/logger.rb', line 19

def write_thread
  @write_thread
end

Class Method Details

.default_optionsObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/testa_logger/logger.rb', line 181

def default_options
  OpenStruct.new(
    shift_age: "daily",
    level: DEBUG,
    formatter: default_formatter,
    live: false,
    filepath: nil,
    tag_length: 13,
    persist: true, # requires aws credentials set in env variables
    faye_url: ENV["WEB_SOCKET_URL"],
    faye_token: ENV["FAYE_TOKEN"],
    s3_creds: {
      region: ENV["AWS_REGION"],
      access_key_id: ENV["AWS_ACCESS_KEY_ID"],
      secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
      bucket_name: ENV["S3_BUCKET_NAME"],
    }
  )
end

Instance Method Details

#create_log_fileObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/testa_logger/logger.rb', line 47

def create_log_file
  options.filepath = "/tmp/#{SecureRandom.uuid}.log" if options.filepath.nil?
  FileUtils.mkdir_p(File.dirname(options.filepath))
  before_shift = options.persist ? proc { persist } : nil
  @log_device = LogDevice.new(options.filepath,
                              shift_age: options.shift_age,
                              shift_size: 1_048_576,
                              shift_period_suffix: "%d%m%Y",
                              binmode: false,
                              before_shift: before_shift)
end

#debug(tag = NO_TAG, *args, &block) ⇒ Object



81
82
83
# File 'lib/testa_logger/logger.rb', line 81

def debug(tag = NO_TAG, *args, &block)
  add_log_to_queue(DEBUG, tag, args, &block)
end

#debug!Object

Sets the severity to DEBUG.



120
121
122
# File 'lib/testa_logger/logger.rb', line 120

def debug!
  options.level = DEBUG
end

#debug?Boolean

Returns true iff the current severity level allows for the printing of DEBUG messages.

Returns:

  • (Boolean)


115
116
117
# File 'lib/testa_logger/logger.rb', line 115

def debug?
  options.level <= DEBUG
end

#error(tag = NO_TAG, *args, &block) ⇒ Object



93
94
95
# File 'lib/testa_logger/logger.rb', line 93

def error(tag = NO_TAG, *args, &block)
  add_log_to_queue(ERROR, tag, args, &block)
end

#error!Object

Sets the severity to ERROR.



153
154
155
# File 'lib/testa_logger/logger.rb', line 153

def error!
  options.level = ERROR
end

#error?Boolean

Returns true iff the current severity level allows for the printing of ERROR messages.

Returns:

  • (Boolean)


148
149
150
# File 'lib/testa_logger/logger.rb', line 148

def error?
  options.level <= ERROR
end

#fatal(tag = NO_TAG, *args, &block) ⇒ Object



97
98
99
# File 'lib/testa_logger/logger.rb', line 97

def fatal(tag = NO_TAG, *args, &block)
  add_log_to_queue(FATAL, tag, args, &block)
end

#fatal!Object

Sets the severity to FATAL.



164
165
166
# File 'lib/testa_logger/logger.rb', line 164

def fatal!
  options.level = FATAL
end

#fatal?Boolean

Returns true iff the current severity level allows for the printing of FATAL messages.

Returns:

  • (Boolean)


159
160
161
# File 'lib/testa_logger/logger.rb', line 159

def fatal?
  options.level <= FATAL
end

#info(tag = NO_TAG, *args, &block) ⇒ Object



85
86
87
# File 'lib/testa_logger/logger.rb', line 85

def info(tag = NO_TAG, *args, &block)
  add_log_to_queue(INFO, tag, args, &block)
end

#info!Object

Sets the severity to INFO.



131
132
133
# File 'lib/testa_logger/logger.rb', line 131

def info!
  options.level = INFO
end

#info?Boolean

Returns true iff the current severity level allows for the printing of INFO messages.

Returns:

  • (Boolean)


126
127
128
# File 'lib/testa_logger/logger.rb', line 126

def info?
  options.level <= INFO
end

#levelObject



168
169
170
# File 'lib/testa_logger/logger.rb', line 168

def level
  options.level
end

#level=(severity) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/testa_logger/logger.rb', line 105

def level=(severity)
  if severity.is_a?(Integer)
    options.level = severity
  else
    SEV_LABEL.index(severity.to_s.downcase.upcase).to_i
  end
end

#setup_dispatcherObject



59
60
61
62
63
64
65
66
67
# File 'lib/testa_logger/logger.rb', line 59

def setup_dispatcher
  @dispatcher = Dispatcher.new(
    options.faye_url,
    options.faye_token,
    app,
    group,
    subgroup
  )
end

#silence(severity = ERROR, &block) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/testa_logger/logger.rb', line 172

def silence(severity = ERROR, &block)
  old_level = options.level
  options.level = severity
  result = block&.call
  options.level = old_level
  result
end

#start_write_threadObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/testa_logger/logger.rb', line 69

def start_write_thread
  # we must use this queue in order to be able to collect logs in trap context
  @queue = Queue.new
  @write_thread = Thread.new do
    loop do
      text = @queue.pop
      Thread.stop if Thread.current["stop"]
      @log_device.write(text)
    end
  end
end

#unknown(tag = NO_TAG, *args, &block) ⇒ Object



101
102
103
# File 'lib/testa_logger/logger.rb', line 101

def unknown(tag = NO_TAG, *args, &block)
  add_log_to_queue(UNKNOWN, tag, args, &block)
end

#warn(tag = NO_TAG, *args, &block) ⇒ Object



89
90
91
# File 'lib/testa_logger/logger.rb', line 89

def warn(tag = NO_TAG, *args, &block)
  add_log_to_queue(WARN, tag, args, &block)
end

#warn!Object

Sets the severity to WARN.



142
143
144
# File 'lib/testa_logger/logger.rb', line 142

def warn!
  options.level = WARN
end

#warn?Boolean

Returns true iff the current severity level allows for the printing of WARN messages.

Returns:

  • (Boolean)


137
138
139
# File 'lib/testa_logger/logger.rb', line 137

def warn?
  options.level <= WARN
end