Module: ExtLogger::Common

Defined in:
lib/ext_logger/common.rb

Constant Summary collapse

ENV_HASH =
{
    dev: 'development',
    stg: 'staging',
    pro: 'production',
}
TIME_MS_DAY =

date & time

24 * 60 * 60 * 1000
TIME_MS_HOUR =

Day with millisecond

60 * 60 * 1000
TIME_MS_MIDDLE =

Hour with millisecond

60 * 1000
TIME_MS_SECOND =

Middle with millisecond

1000
TIME_UNIT_DAY =

Second with millisecond

'day '
TIME_UNIT_HOUR =
':'
TIME_UNIT_MIDDLE =
':'
TIME_UNIT_SECOND =
' '
TIME_UNIT_MILLISECOND =
'ms'

Instance Method Summary collapse

Instance Method Details

#analysis_exception(e) ⇒ Object

Analysis exception object



50
51
52
53
54
55
56
57
58
59
# File 'lib/ext_logger/common.rb', line 50

def analysis_exception(e)
  func_name = "[common.#{__method__.to_s}]"
  output_debug "#{func_name} e.class: #{e.class}"
  if !e.class.to_s.include?('Error')
    @logger.error("The object 'e:#{e.to_s}' is not exception. e.class: #{e.class}")
    return ''
  end
  str = e.message + "\n" + e.backtrace.join("\n")
  return str
end

#format_sub_time(start_time, end_time) ⇒ Object

Formatting time difference



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ext_logger/common.rb', line 106

def format_sub_time(start_time, end_time)
  use_time = (end_time - start_time).to_i
  time_list = []

  if use_time > TIME_MS_DAY
    remainder_time = use_time % TIME_MS_DAY
    num = (use_time - remainder_time) / TIME_MS_DAY
    time_list << "#{num}#{TIME_UNIT_DAY}"
    use_time = remainder_time
  end

  if use_time > TIME_MS_HOUR
    remainder_time = use_time % TIME_MS_HOUR
    num = (use_time - remainder_time) / TIME_MS_HOUR
    time_list << "#{num}#{TIME_UNIT_HOUR}"
    use_time = remainder_time
  end

  if use_time > TIME_MS_MIDDLE
    remainder_time = use_time % TIME_MS_MIDDLE
    num = (use_time - remainder_time) / TIME_MS_MIDDLE
    time_list << "#{num}#{TIME_UNIT_MIDDLE}"
    use_time = remainder_time
  end

  if use_time > TIME_MS_SECOND
    remainder_time = use_time % TIME_MS_SECOND
    num = (use_time - remainder_time) / TIME_MS_SECOND
    time_list << "#{num}#{TIME_UNIT_SECOND}"
    use_time = remainder_time
  end

  if use_time > 0
    time_list << "#{use_time}#{TIME_UNIT_MILLISECOND}"
  end

  return time_list.join('')
end

#get_class_nameObject

Get class name



6
7
8
# File 'lib/ext_logger/common.rb', line 6

def get_class_name
  self.class.to_s
end

#get_debugObject



20
21
22
# File 'lib/ext_logger/common.rb', line 20

def get_debug
  return @debug || false
end

#get_instance_nameObject

Get instance name



11
12
13
# File 'lib/ext_logger/common.rb', line 11

def get_instance_name
  self.class.to_s.downcase
end

#is_blank?(obj) ⇒ Boolean

String Empty judgement for object

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/ext_logger/common.rb', line 86

def is_blank?(obj)
  func_name = "[common.#{__method__.to_s}]"
  # output_debug "#{func_name} obj.class: #{obj.class}"
  return true if obj.nil?
  return (obj.is_a?(String) || obj.is_a?(Array) || obj.is_a?(Hash)) && obj.empty?
end

#mkdir_more(file_path) ⇒ Object

File operate Check the folder and create it when it is not exist.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ext_logger/common.rb', line 63

def mkdir_more(file_path)
  begin
    require 'FileUtils' if !defined? FileUtils
    FileUtils.mkdir_p(File.dirname(file_path))
  rescue Exception => e
    puts e.message + "\n" + e.backtrace.join("\n")
  end

  # rails_flag = true if path.include?(rails_root)
  # if !::Dir.exist?(_path)
  #   _path_array = _path.split('/').select{|item| !item.nil? && item != ''}
  #   _path = rails_flag ? rails_root : ''
  #
  #   _path_array.each_with_index do |item, idx|
  #     break if idx >= _path_array.length - 1
  #     _path += '/' + item
  #     ::Dir.mkdir(_path) if !::Dir.exist?(_path)
  #   end
  # end
end

#output_debug(str) ⇒ Object

Put content to console by debug=true



25
26
27
# File 'lib/ext_logger/common.rb', line 25

def output_debug(str)
  puts "DEBUG_P: #{str}" if @debug
end

#rails_envObject

Get the env



45
46
47
# File 'lib/ext_logger/common.rb', line 45

def rails_env
  valid_rails && ::Rails.env ? ::Rails.env : ENV_HASH[:dev]
end

#rails_rootObject

Get the path of folder or rails project



35
36
37
# File 'lib/ext_logger/common.rb', line 35

def rails_root
  valid_rails && ::Rails.root ? ::Rails.root.to_s : ::Dir.pwd
end

#set_debug(_bool = true) ⇒ Object

Set debug



16
17
18
# File 'lib/ext_logger/common.rb', line 16

def set_debug(_bool=true)
  @debug = _bool  # Controller debug content output to console
end

#valid_railsObject

Validation the Rails object



30
31
32
# File 'lib/ext_logger/common.rb', line 30

def valid_rails
  defined?(::Rails)
end