Class: ToolsLog

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/lib/log.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ToolsLog

Returns a new instance of ToolsLog.



5
# File 'lib/lib/log.rb', line 5

def initialize(options = {}); end

Class Method Details

.create_log_file(log_name, log_file) ⇒ Object

Create a Log file in work area

Sample

ToolsLog.create_log_file 'sample', '/myhome/tools/sample.log'
log = ToolsUtil.get_variable "sample_logger" => Logger Object

ToolsLog.create_log_file 'xyko', '/home/francisco/2018/xykotools/tools/xyko.log'
ToolsLog.xyko_info 'teste do methodo1', :light_yellow
ToolsLog.xyko_warn 'teste do methodo2'
ToolsLog.xyko_error 'teste do methodo3'
ToolsLog.xyko_debug 'teste do methodo4'

Parameters:

  • log_name

    LogName to create

  • log_file

    LogFile path

Returns:



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lib/log.rb', line 23

def self.create_log_file(log_name, log_file)
  if File.exist? log_file
    File.delete(log_file) unless File.ctime(log_file).to_date == Time.now.to_date
  end
  ToolsUtil.set_variable "#{log_name}_log_file", log_file
  ToolsUtil.set_variable "#{log_name}_logger", Logger.new(log_file, 'daily')
  (ToolsUtil.get_variable "#{log_name}_logger").formatter = proc do |severity, datetime, _progname, msg|
    "#{severity.chars.first} #{datetime.strftime '%H:%M:%S'}: #{msg}\n"
  end
  # ToolsUtil.purge_files Tools.home+'/.tools/backup', '*', 14*24*60*60
end

.method_missing(method, *args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lib/log.rb', line 35

def self.method_missing(method, *args)
  # expected call format =>    STRING_LOGGER_TYPE + '_' + LOGGER_TYPE
  # Ex.:  tools_info
  logger_name   = method.to_s.split('_').first
  logger_method = method.to_s.split('_').last
  logger        = ToolsUtil.get_variable "#{logger_name}_logger"
  color         = args.extract_color
  if color.eql? :default
    case logger_method
    when 'info'
      color = :cyan
    when 'warn'
      color = :yellow
    when 'debug'
      color = :green
    when 'error'
      color = :light_red
    when 'ucolor'
      color = nil
      logger_method = 'info'
    else
      return false
    end
  end
  text_to_print = if color.nil?
                    args.first
                  else
                    args.first.colorize(color)
                  end
  logger.method(logger_method).call text_to_print
end