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

Returns a new instance of Xlogger.



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

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

  @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



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

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



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

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



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

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



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

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

#log(type, text, tags) ⇒ Object



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

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



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

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