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



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

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/testa_logger/logger.rb', line 118

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

#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

#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

#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

#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