Module: ExtLogger::Common

Defined in:
lib/ext_logger/common.rb

Defined Under Namespace

Modules: ClassMethods

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'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ext_logger/common.rb', line 11

def self.included(base)
  # base.send(:include, InstanceMethods)
  base.extend ClassMethods
  # base.class_eval do
  #   scope :active, where(is_active: true)
  # end
end

Instance Method Details

#analysis_exception(e) ⇒ Object

Analysis exception object



60
61
62
63
64
65
66
67
68
69
# File 'lib/ext_logger/common.rb', line 60

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



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
144
145
146
147
148
149
150
151
152
153
# File 'lib/ext_logger/common.rb', line 116

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



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

def get_class_name
  self.class.to_s
end

#get_debugObject



50
51
52
# File 'lib/ext_logger/common.rb', line 50

def get_debug
  return @debug || false
end

#get_instance_nameObject

Get instance name



41
42
43
# File 'lib/ext_logger/common.rb', line 41

def get_instance_name
  self.class.to_s.downcase
end

#is_blank?(obj) ⇒ Boolean

String Empty judgement for object

Returns:

  • (Boolean)


96
97
98
99
100
101
# File 'lib/ext_logger/common.rb', line 96

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ext_logger/common.rb', line 73

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



55
56
57
# File 'lib/ext_logger/common.rb', line 55

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

#set_debug(_bool = true) ⇒ Object

Set debug



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

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