Class: Xlog::Xlogger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/xlog/xlogger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXlogger



9
10
11
12
13
14
15
# File 'lib/xlog/xlogger.rb', line 9

def initialize
  @base_logger = ActiveSupport::TaggedLogging.new(Logger.new("log/xlog_#{Rails.env}.log", 'weekly'))

  @app_name = Rails.application.class.to_s.split('::')[0].underscore
  @app_root = Rails.root.to_s
  @folder_names_to_remove = Dir.glob('app/*').map { |f| f.gsub('app/', '') }
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



7
8
9
# File 'lib/xlog/xlogger.rb', line 7

def app_name
  @app_name
end

#app_rootObject

Returns the value of attribute app_root.



7
8
9
# File 'lib/xlog/xlogger.rb', line 7

def app_root
  @app_root
end

#base_loggerObject

Returns the value of attribute base_logger.



7
8
9
# File 'lib/xlog/xlogger.rb', line 7

def base_logger
  @base_logger
end

Instance Method Details

#and_raise_error(e, message, data, tags) ⇒ Object



36
37
38
39
# File 'lib/xlog/xlogger.rb', line 36

def and_raise_error(e, message, data, tags)
  log(:error, "#{e.class}: #{e.try(:message)}. #{newline} #{compose_log(message, data)} #{newline} Error backtrace: #{newline} #{backtrace(e)}", tags)
  message.present? ? raise(e, message) : raise(e)
end

#custom_logger=(logger) ⇒ Object



41
42
43
# File 'lib/xlog/xlogger.rb', line 41

def custom_logger=(logger)
  @base_logger = ActiveSupport::TaggedLogging.new(logger)
end

#error(e, message, data, tags) ⇒ Object

do NOT refactor error and and_raise_error



31
32
33
34
# File 'lib/xlog/xlogger.rb', line 31

def error(e, message, data, tags)
  # they MUST BE NOT DRY in order to log correct backtrace
  log(:error, "#{e.class}: #{e.try(:message)}. \n #{compose_log(message, data)} \n Error backtrace: \n#{backtrace(e)}", tags)
end

#info(message, data, tags) ⇒ Object



22
23
24
# File 'lib/xlog/xlogger.rb', line 22

def info(message, data, tags)
  log(:info, compose_log(message, data), tags)
end

#log(type, text, tags) ⇒ Object



17
18
19
20
# File 'lib/xlog/xlogger.rb', line 17

def log(type, text, tags)
  tags = [time_stamp, called_from(type), type] + Array.wrap(tags)
  @base_logger.tagged(tags.compact) { @base_logger.send(type, text) }
end

#warn(message, data, tags) ⇒ Object



26
27
28
# File 'lib/xlog/xlogger.rb', line 26

def warn(message, data, tags)
  log(:warn, compose_log(message, data), tags)
end