Class: TestaLogger::Logger

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

Defined Under Namespace

Modules: Persistence Classes: Dispatcher

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Logger.



30
31
32
33
34
35
36
37
38
39
# File 'lib/testa_logger/logger.rb', line 30

def initialize(app, group, subgroup = nil, options = {})
  @app = app
  @group = group
  @subgroup = subgroup
  handle_options(options)
  create_log_file
  setup_dispatcher
  start_write_thread
  self.extend Persistence if @options.persist
end

Instance Attribute Details

#appString

Returns:

  • (String)


9
10
11
# File 'lib/testa_logger/logger.rb', line 9

def app
  @app
end

#groupString

Returns:

  • (String)


9
10
11
# File 'lib/testa_logger/logger.rb', line 9

def group
  @group
end

#optionsOpenStruct

Returns:

  • (OpenStruct)


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

def options
  @options
end

#subgroupString

Returns:

  • (String)


9
10
11
# File 'lib/testa_logger/logger.rb', line 9

def subgroup
  @subgroup
end

#write_threadThread

Returns:

  • (Thread)


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

def write_thread
  @write_thread
end

Class Method Details

.default_formatterObject



205
206
207
208
209
# File 'lib/testa_logger/logger.rb', line 205

def default_formatter
  proc do |severity, datetime, _, msg|
    "[#{datetime.strftime('%Y-%m-%d %H:%M:%S.%L')}]  #{format('%-5.5s', severity)} -- : #{msg}\n"
  end
end

.default_optionsObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/testa_logger/logger.rb', line 185

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



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

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, *args, &block) ⇒ Object



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

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

#debug!Object

Sets the severity to DEBUG.



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

def debug!
  options.level = DEBUG
end

#debug?Boolean

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

Returns:

  • (Boolean)


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

def debug?
  options.level <= DEBUG
end

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



105
106
107
# File 'lib/testa_logger/logger.rb', line 105

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

#error!Object

Sets the severity to ERROR.



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

def error!
  options.level = ERROR
end

#error?Boolean

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

Returns:

  • (Boolean)


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

def error?
  options.level <= ERROR
end

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



109
110
111
# File 'lib/testa_logger/logger.rb', line 109

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

#fatal!Object

Sets the severity to FATAL.



176
177
178
# File 'lib/testa_logger/logger.rb', line 176

def fatal!
  options.level = FATAL
end

#fatal?Boolean

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

Returns:

  • (Boolean)


171
172
173
# File 'lib/testa_logger/logger.rb', line 171

def fatal?
  options.level <= FATAL
end

#handle_options(options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/testa_logger/logger.rb', line 41

def handle_options(options)
  options.deep_symbolize_keys!
  @options = Logger.default_options
  @options.shift_age = options[:shift_age] unless options[:shift_age].nil?
  unless options[:level].nil?
    options[:level] = SEV_LABEL.index(options[:level].to_s.upcase).to_i unless options[:level].is_a?(Integer)
    @options.level = options[:level]
  end
  @options.formatter = options[:formatter] unless options[:formatter].nil?
  @options.live = options[:live] unless options[:live].nil?
  @options.filepath = File.expand_path(options[:filepath]) unless options[:filepath].nil?
  @options.tag_length = options[:tag_length] unless options[:tag_length].nil?
  @options.persist = options[:persist] unless options[:persist].nil?
  @options.faye_url = options[:faye_url] unless options[:faye_url].nil?
  @options.faye_token = options[:faye_token] unless options[:faye_token].nil?
  @options.s3_creds = options[:s3_creds].deep_symbolize_keys unless options[:s3_creds].blank?
end

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



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

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

#info!Object

Sets the severity to INFO.



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

def info!
  options.level = INFO
end

#info?Boolean

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

Returns:

  • (Boolean)


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

def info?
  options.level <= INFO
end

#levelObject



180
181
182
# File 'lib/testa_logger/logger.rb', line 180

def level
  options.level
end

#level=(severity) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/testa_logger/logger.rb', line 117

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

#setup_dispatcherObject



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

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

#start_write_threadObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/testa_logger/logger.rb', line 81

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, *args, &block) ⇒ Object



113
114
115
# File 'lib/testa_logger/logger.rb', line 113

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

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



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

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

#warn!Object

Sets the severity to WARN.



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

def warn!
  options.level = WARN
end

#warn?Boolean

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

Returns:

  • (Boolean)


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

def warn?
  options.level <= WARN
end