module CustomLogger

Constants

LOOP_LIMIT
SECTION_HTML

Public Class Methods

auto_clear() click to toggle source
# File lib/customlogger.rb, line 78
def auto_clear
  @auto_clear
end
auto_clear=(value) click to toggle source
# File lib/customlogger.rb, line 74
def auto_clear=(value)
  @auto_clear = value
end
clear() click to toggle source
# File lib/customlogger.rb, line 69
def clear
  new_log_file
  'Wiped All Old Logs'
end
debug(value, title=nil) click to toggle source
# File lib/customlogger.rb, line 94
def debug(value, title=nil)
  clear if @auto_clear
  log(:debug, value, title)
  value
end
demo() click to toggle source
# File lib/customlogger.rb, line 117
def demo
  clear
  error 'This is an error message...', 'Error'
  warning 'This is a warning message...', 'Warning'
  info 'This is an info message...', 'Info'
  debug 'This is an debug message...', 'Debug'
  raw 'This is an raw message...', 'Raw'
  [ 'Demo complete for file: ', file ].join
end
error(value, title=nil) click to toggle source
# File lib/customlogger.rb, line 82
def error(value, title=nil)
  clear if @auto_clear
  log(:error, value, title)
  value
end
error_color(state, color) click to toggle source
# File lib/customlogger.rb, line 56
def error_color(state, color)
  @error_colors[state] = color
end
error_colors() click to toggle source
# File lib/customlogger.rb, line 60
def error_colors
  @error_colors
end
file() click to toggle source
# File lib/customlogger.rb, line 40
def file
  if @file_name.nil? || @file_name == ''
    'custom_logger.html' 
  else
    @file_name
  end
end
file=(value) click to toggle source
# File lib/customlogger.rb, line 36
def file=(value)
  @file_name = value
end
info(value, title=nil) click to toggle source
# File lib/customlogger.rb, line 100
def info(value, title=nil)
  clear if @auto_clear
  log(:info, value, title)
  value
end
new() click to toggle source
# File lib/customlogger.rb, line 64
def new
  new_log_file
  'Wiped All Old Logs'
end
new_line() click to toggle source
# File lib/customlogger.rb, line 112
def new_line
  log(:section, nil, nil)
  SECTION_HTML
end
path() click to toggle source
# File lib/customlogger.rb, line 28
def path
  if @log_path.nil? || @log_path == ''
    [ Dir.pwd, '/log' ].join
  else
    @log_path
  end
end
path=(value) click to toggle source
# File lib/customlogger.rb, line 24
def path=(value)
  @log_path = value
end
raw(value, title=nil) click to toggle source
# File lib/customlogger.rb, line 106
def raw(value, title=nil)
  clear if @auto_clear
  log(:raw, value, title)
  value
end
title() click to toggle source
# File lib/customlogger.rb, line 52
def title
  @title
end
title=(value) click to toggle source
# File lib/customlogger.rb, line 48
def title=(value)
  @title = value
end
warning(value, title=nil) click to toggle source
# File lib/customlogger.rb, line 88
def warning(value, title=nil)
  clear if @auto_clear
  log(:warning, value, title)
  value
end

Private Class Methods

build_log(file_path_name, state, message, title) click to toggle source
# File lib/customlogger.rb, line 180
def build_log(file_path_name, state, message, title)
  written_logs = File.read(file_path_name)
  written_logs = written_logs[0..-15].to_s
  written_logs = written_logs[0...-1] if written_logs[-1] == '<'
  written_logs.concat(to_html(state, message, title))
  written_logs.concat('</body></html>')
end
format_message(message) click to toggle source
# File lib/customlogger.rb, line 155
def format_message(message)
  unless %w(Array Hash String).include?(message.class.name)
    if defined?(Module::ActiveRecord).nil?
      message = message.inspect
    else
      if message.is_a?(ActiveRecord::Base)
        message = JSON.pretty_generate(message.as_json)
        message = message.gsub!(/\n/,'<br>')
      end
    end
  end
  message
end
format_title(title) click to toggle source
# File lib/customlogger.rb, line 148
def format_title(title)
  if !title.nil? && !title.is_a?(String)
    title = title.inspect
  end
  title
end
html() click to toggle source
# File lib/customlogger.rb, line 188
def html
  error_colors
  HTMLTemplate.html(@title, @error_colors)
end
log(state, message, title = nil) click to toggle source
# File lib/customlogger.rb, line 134
def log(state, message, title = nil)
  title = format_title(title)
  message = format_message(message)

  abort('Infinite loop possibility!!') if @logged_total > LOOP_LIMIT

  file_path_name = [ path, file ].join('/')
  new_log_file unless File.exist?(file_path_name)

  written_logs = build_log(file_path_name, state, message, title)
  File.open(file_path_name, 'w') { |file| file.write(written_logs) }
  @logged_total += 1
end
new_log_file() click to toggle source
# File lib/customlogger.rb, line 128
def new_log_file
  file_path_name = [ path, file ].join('/')
  system "mkdir -p #{path}"
  File.open(file_path_name, 'w') { |file| file.write(html) }
end
to_html(state, message, title) click to toggle source
# File lib/customlogger.rb, line 169
def to_html(state, message, title)
  case state
    when :raw
      HTMLTemplate.raw_to_html(message, title)
    when :section
      SECTION_HTML
    else
      HTMLTemplate.log_to_html(state, message, title)
  end
end