Class: LogRedux::Logger

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_filename = $stderr, color: true, timestamp: true, filename: true, track: false) ⇒ Logger

Returns a new instance of Logger.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/log_redux/logger.rb', line 8

def initialize(output_filename = $stderr, color: true, timestamp: true, filename: true, track: false)
  if output_filename.nil? || (!output_filename.is_a?(String) && !check_valid_stdio(output_filename))
    raise LoggerError, 'invalid log file'
  end

  @output_filename = output_filename
  @color = color
  @timestamp = timestamp
  @filename = filename
  @track = track
  @history = []

  @log_file = check_valid_stdio(output_filename) ? output_filename : File.open(output_filename, 'a+')
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



6
7
8
# File 'lib/log_redux/logger.rb', line 6

def color
  @color
end

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'lib/log_redux/logger.rb', line 6

def filename
  @filename
end

#historyObject (readonly)

Returns the value of attribute history.



5
6
7
# File 'lib/log_redux/logger.rb', line 5

def history
  @history
end

#line_numberObject

Returns the value of attribute line_number.



6
7
8
# File 'lib/log_redux/logger.rb', line 6

def line_number
  @line_number
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



5
6
7
# File 'lib/log_redux/logger.rb', line 5

def log_file
  @log_file
end

#output_filenameObject (readonly)

Returns the value of attribute output_filename.



5
6
7
# File 'lib/log_redux/logger.rb', line 5

def output_filename
  @output_filename
end

#timestampObject

Returns the value of attribute timestamp.



6
7
8
# File 'lib/log_redux/logger.rb', line 6

def timestamp
  @timestamp
end

#trackObject

Returns the value of attribute track.



6
7
8
# File 'lib/log_redux/logger.rb', line 6

def track
  @track
end

Instance Method Details

#[](index) ⇒ Object

Raises:



79
80
81
82
83
# File 'lib/log_redux/logger.rb', line 79

def [](index)
  raise LoggerError, 'tracking logs is disabled' unless @track

  @history[index][:formatted]
end

#closeObject

Raises:



97
98
99
100
101
# File 'lib/log_redux/logger.rb', line 97

def close
  raise LoggerError, 'cannot close a Standard IO file' if check_valid_stdio(@log_file)

  @log_file.close
end

#debug(msg) ⇒ Object



59
60
61
# File 'lib/log_redux/logger.rb', line 59

def debug(msg)
  log(:DEBUG, msg: msg)
end

#error(msg) ⇒ Object



71
72
73
# File 'lib/log_redux/logger.rb', line 71

def error(msg)
  log(:ERROR, msg: msg)
end

#fatal(msg) ⇒ Object



75
76
77
# File 'lib/log_redux/logger.rb', line 75

def fatal(msg)
  log(:FATAL, msg: msg)
end

#firstObject

Raises:



85
86
87
88
89
# File 'lib/log_redux/logger.rb', line 85

def first
  raise LoggerError, 'tracking logs is disabled' unless @track

  @history.first[:formatted]
end

#info(msg) ⇒ Object



63
64
65
# File 'lib/log_redux/logger.rb', line 63

def info(msg)
  log(:INFO, msg: msg)
end

#lastObject

Raises:



91
92
93
94
95
# File 'lib/log_redux/logger.rb', line 91

def last
  raise LoggerError, 'tracking logs is disabled' unless @track

  @history.last[:formatted]
end

#log(level, msg: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/log_redux/logger.rb', line 23

def log(level, msg: nil)
  assert_log_level(level)

  level_str = level.to_s
  time = Time.now.strftime('%H:%M:%S')
  file_line = caller(1).last.split(':')[0..1]
  filename = file_line[0]
  line_number = file_line[1]

  output = if @color
             [
               @timestamp ? "#{COLORS[:GREY]}#{time}" : nil,
               "#{COLORS[level.to_sym]}#{level_str}#{@filename ? '' : ':'}",
               @filename ? "#{COLORS[:GREY]}#{file_line.join(':')}:" : nil,
               "#{COLORS[:WHITE]}#{msg}#{COLORS[:RESET]}"
             ].compact.join(' ')
           else
             [
               @timestamp ? "#{time}" : nil,
               "#{level_str}#{@filename ? '' : ':'}",
               @filename ? "#{file_line.join(':')}:" : nil,
               "#{msg}"
             ].compact.join(' ')
           end

  @log_file.puts output

  save_entry(level, msg, time, filename, line_number, output) if @track

  output
end

#trace(msg) ⇒ Object



55
56
57
# File 'lib/log_redux/logger.rb', line 55

def trace(msg)
  log(:TRACE, msg: msg)
end

#warn(msg) ⇒ Object



67
68
69
# File 'lib/log_redux/logger.rb', line 67

def warn(msg)
  log(:WARN, msg: msg)
end