Class: ExtLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/ext_logger/logger.rb

Overview

require ‘rails’

Constant Summary collapse

VERSION =
"0.2.2"
DATE_FORMAT_MEMBER_LIST =
%w(%Y %y %m %d %wday %H %M %S)
LOG_NAME =

Log name pseudo-code

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ExtLogger

Initialize, next is parameters: 1rst: log path (Default parameter) 2end: option (Influencing log size, file name format, etc.)

:age            -- Extention by Logger(shift_age), split file with time format;
:size           -- Extention by Logger(shift_size), split file with fix size;
:split_format   -- Rename logs according to ruby's time conversion format;
:log_max_num    -- Keep the latest number of log files according to the parameter values, and delete the redundancy;
:debug          -- Debug for puts


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ext_logger/logger.rb', line 32

def initialize(*args)
  func_name = "[#{self.to_s}.initialize]"
  output_debug "#{func_name} args.size: #{args.size}"
  init(args[0]) if args.size == 1

  if args.size == 2
    opt = args[1].clone
    if opt.is_a?(Hash)
      init(args[0], opt)
      args = [args[0]]

      shift_age = is_blank?(opt[:age]) ? opt[:age] : 0
      args << shift_age

      shift_size = is_blank?(opt[:size]) ? opt[:size] : 0
      args << shift_size
    else
      init(args[0])
    end
  end

  @logger = Logger.new(*args)
end

Instance Method Details

#debug(*args) ⇒ Object

Output debug



74
75
76
77
78
# File 'lib/ext_logger/logger.rb', line 74

def debug(*args)
  output_normal('DEBUG', *args)

  @logger.debug(*args)
end

#error(*args) ⇒ Object

Output error



81
82
83
84
85
# File 'lib/ext_logger/logger.rb', line 81

def error(*args)
  output_normal('ERROR', *args)

  @logger.error(*args)
end

#exception(*args) ⇒ Object

Output exception



102
103
104
105
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
# File 'lib/ext_logger/logger.rb', line 102

def exception(*args)
  func_name = "[@logger.EXCEPTION]"

  if args.size == 1
    output_debug "#{func_name} args[0].class: #{args[0].class}"
    str = args[0].message + args[0].backtrace.join("\n")
    args = [str]
  end
  output_normal('EXCEPTION', *args)

  _output_type = 'error'
  if args.size == 2
    output_debug "#{func_name} args[1].class: #{args[1].class}"
    if args[1].is_a?(Hash)
      _output_type = args[1][:type] if !args[1][:type].blank?
    end
  end

  # Change logger ouput type
  case _output_type
    when 'info'
      @logger.info(*args)
    when 'debug'
      @logger.debug(*args)
    when 'warn'
      @logger.warn(*args)
    when 'fatal'
      @logger.fatal(*args)
    else
      @logger.error(*args)
  end
end

#fatal(*args) ⇒ Object

Output fatal



95
96
97
98
99
# File 'lib/ext_logger/logger.rb', line 95

def fatal(*args)
  output_normal('FATAL', *args)

  @logger.fatal(*args)
end

#info(*args) ⇒ Object

Output info



67
68
69
70
71
# File 'lib/ext_logger/logger.rb', line 67

def info(*args)
  output_normal('INFO', *args)

  @logger.info(*args)
end

#record_memory(type = 'start') ⇒ Object

Output the memory usage Example: record_memory() Example: record_memory(‘end’)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ext_logger/logger.rb', line 138

def record_memory(type='start')
  func_name = "[@logger.record_memory]"
  begin
    type_list = %w(start end)
    @record_memory_error ||= 0
    error_message = 'RECORD MEMORY ERROR:'
    message = ''
    if !type_list.include?(type) # Judging whether the parameters are valid
      self.info error_message + ' params error!' if @record_memory_error == 0
      @record_memory_error = 1
      return
    end

    # Get process memory
    if File.exist?("/proc/#{Process.pid}/status")
      process_status = File.open("/proc/#{Process.pid}/status")

      15.times { process_status.gets }

      if type != type_list[1]
        @rss_before_action = process_status.gets.split[1].to_f/1000
        message = "START MEMORY: #{@rss_before_action}MB "
        #@rss_before_action = 0
      else
        @rss_after_action = process_status.gets.split[1].to_f/1000
        message = "END MEMORY: #{@rss_after_action}MB  CONSUME MEMORY: #{@rss_after_action - @rss_before_action}MB "
        #@rss_after_action = 0
      end

      process_status.close
    else
      error_message += " Non Linux system, file [/proc/#{Process.pid}/status] not exist!"
    end

    # Export internal consumption
    self.info message if !message.nil? && message != ''
    if !message.nil? && message != '' && @record_memory_error == 0
      self.info error_message
      @record_memory_error = 1
    end
  rescue Exception=>e
    self.exception e
  end
end

#set_debug(_bool = true) ⇒ Object

Set debug



57
58
59
# File 'lib/ext_logger/logger.rb', line 57

def set_debug(_bool=true)
  @debug = _bool
end

#set_output_flag(type = true) ⇒ Object

Set output flag



62
63
64
# File 'lib/ext_logger/logger.rb', line 62

def set_output_flag(type=true)
  @output_flag = type
end

#warn(*args) ⇒ Object

output warning



88
89
90
91
92
# File 'lib/ext_logger/logger.rb', line 88

def warn(*args)
  output_normal('WARN', *args)

  @logger.warn(*args)
end