Class: EnhancedLogger::Logger

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

Constant Summary collapse

LEVELS =
{ debug:0, info:1, warn:2, error:3, fatal:4, unknown:5 }.freeze
MAX_UUID_LENGTH =
64

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = 1, options = nil) ⇒ Logger

Returns a new instance of Logger.



8
9
10
11
12
13
14
15
16
17
# File 'lib/enhanced_logger/logger.rb', line 8

def initialize level=1, options=nil
  options ||= {}
  @options = {}

  @level = level
  @options[ :exclude_files   ] = Array( options[ :exclude_files ]).push filename
  @options[ :uuid_log_length ] = options[ :uuid_log_length ]

  $stdout.sync = true
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

Instance Method Details

#clear_envObject



24
25
26
27
# File 'lib/enhanced_logger/logger.rb', line 24

def clear_env
  @request_id = nil
  @remote_request_id = nil
end

#debug(msg = nil) ⇒ Object



49
50
51
52
# File 'lib/enhanced_logger/logger.rb', line 49

def debug msg=nil
  return if is_not_a_message?( msg )
  puts formatted( msg ) if debug?
end

#debug?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/enhanced_logger/logger.rb', line 29

def debug?
  @level <= LEVELS[ :debug ]
end

#error(msg = nil) ⇒ Object



64
65
66
67
# File 'lib/enhanced_logger/logger.rb', line 64

def error msg=nil
  return if is_not_a_message?( msg )
  puts formatted( msg ) if error?
end

#error?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/enhanced_logger/logger.rb', line 41

def error?
  @level <= LEVELS[ :error ]
end

#fatal(msg = nil) ⇒ Object



69
70
71
72
# File 'lib/enhanced_logger/logger.rb', line 69

def fatal msg=nil
  return if is_not_a_message?( msg )
  puts formatted( msg ) if fatal?
end

#fatal?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/enhanced_logger/logger.rb', line 45

def fatal?
  @level <= LEVELS[ :fatal ]
end

#formatterObject



74
75
76
# File 'lib/enhanced_logger/logger.rb', line 74

def formatter
  ''
end

#info(msg = nil) ⇒ Object



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

def info msg=nil
  return if is_not_a_message?( msg )
  puts formatted( msg ) if info?
end

#info?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/enhanced_logger/logger.rb', line 33

def info?
  @level <= LEVELS[ :info ]
end

#most_recent_callerObject



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

def most_recent_caller
  regex_str = '[' + @options[ :exclude_files ].join( '|' ) + ']'

  filtered = caller.find{| c | c !~ %r+#{ regex_str }\.rb+ }
  filtered.nil? ? '' : filtered.split( '/' ).last
end

#set_env(env) ⇒ Object



19
20
21
22
# File 'lib/enhanced_logger/logger.rb', line 19

def set_env env
  @request_id = env[ 'HTTP_X_REQUEST_ID' ]
  @remote_request_id = env[ 'HTTP_X_REMOTE_REQUEST_ID' ]
end

#warn(msg = nil) ⇒ Object



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

def warn msg=nil
  return if is_not_a_message?( msg )
  puts formatted( msg ) if warn?
end

#warn?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/enhanced_logger/logger.rb', line 37

def warn?
  @level <= LEVELS[ :warn ]
end