Class: Mode::Logger

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

Constant Summary collapse

DEBUG =

For Convenience

::Logger::DEBUG
WARN =
::Logger::WARN
INFO =
::Logger::INFO
ERROR =
::Logger::ERROR
FATAL =
::Logger::FATAL

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Logger

Returns a new instance of Logger.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mode/logger.rb', line 39

def initialize(options = {})
  @enabled = true
  @mutex = Mutex.new # Keep it secret, keep it safe

  path = options[:path] || self.class.default_path
  level = options[:level] || self.class.default_level
  formatter = options[:formatter] || self.class.default_formatter

  @logger = ::Logger.new(path, rotate_freq)

  set_log_level(level)
  set_log_formatter(formatter)
end

Class Attribute Details

.instance(logger = nil) ⇒ Object (readonly)

Returns the value of attribute instance.



103
104
105
# File 'lib/mode/logger.rb', line 103

def instance
  @instance
end

.mutexObject (readonly)

Returns the value of attribute mutex.



102
103
104
# File 'lib/mode/logger.rb', line 102

def mutex
  @mutex
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

#loggerObject (readonly)

Returns the value of attribute logger.



34
35
36
# File 'lib/mode/logger.rb', line 34

def logger
  @logger
end

#mutexObject (readonly)

Returns the value of attribute mutex.



35
36
37
# File 'lib/mode/logger.rb', line 35

def mutex
  @mutex
end

Class Method Details

.default_formatterObject



129
130
131
132
133
# File 'lib/mode/logger.rb', line 129

def default_formatter
  @default_formatter ||= Proc.new do |severity, datetime, progname, message|
    "#{severity} [#{format_time(datetime)}]: #{message}\n"
  end
end

.default_levelObject



117
118
119
# File 'lib/mode/logger.rb', line 117

def default_level
  Logger::INFO
end

.default_pathObject



121
122
123
# File 'lib/mode/logger.rb', line 121

def default_path
  File.join(Mode::Config.log_dir, 'mode.log')
end

.default_rotate_freqObject



125
126
127
# File 'lib/mode/logger.rb', line 125

def default_rotate_freq
  'weekly'
end

.format_time(time) ⇒ Object



135
136
137
# File 'lib/mode/logger.rb', line 135

def format_time(time)
  time_to_datetime(time).new_offset(0).iso8601
end

.time_to_datetime(time) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/mode/logger.rb', line 139

def time_to_datetime(time)
  #
  # http://stackoverflow.com/questions/279769/convert-to-from-datetime-and-time-in-ruby
  #
  seconds = time.sec + Rational(time.usec, 10**6)
  offset = Rational(time.utc_offset, 60 * 60 * 24)
  DateTime.new(time.year, time.month, time.day, time.hour, time.min, seconds, offset)
end

Instance Method Details

#debug(component, message, detail = []) ⇒ Object



61
62
63
# File 'lib/mode/logger.rb', line 61

def debug(component, message, detail = [])
  log_with_formatting(Logger::DEBUG, component, message, detail)
end

#debug!Object



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

def debug!
  set_log_level(Logger::DEBUG)
end

#disable!Object



53
54
55
# File 'lib/mode/logger.rb', line 53

def disable!
  mutex.synchronize { @enabled = false }
end

#enable!Object



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

def enable!
  mutex.synchronize { @enabled = true }
end

#error(component, message, detail = []) ⇒ Object



85
86
87
# File 'lib/mode/logger.rb', line 85

def error(component, message, detail = [])
  log_with_formatting(Logger::ERROR, component, message, detail)
end

#error!Object



89
90
91
# File 'lib/mode/logger.rb', line 89

def error!
  set_log_level(Logger::ERROR)
end

#fatal(component, message, detail = []) ⇒ Object



93
94
95
# File 'lib/mode/logger.rb', line 93

def fatal(component, message, detail = [])
  log_with_formatting(Logger::FATAL, component, message, detail)
end

#fatal!Object



97
98
99
# File 'lib/mode/logger.rb', line 97

def fatal!
  set_log_level(Logger::FATAL)
end

#info(component, message, detail = []) ⇒ Object



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

def info(component, message, detail = [])
  log_with_formatting(Logger::INFO, component, message, detail)
end

#info!Object



73
74
75
# File 'lib/mode/logger.rb', line 73

def info!
  set_log_level(Logger::INFO)
end

#warn(component, message, detail = []) ⇒ Object



77
78
79
# File 'lib/mode/logger.rb', line 77

def warn(component, message, detail = [])
  log_with_formatting(Logger::WARN, component, message, detail)
end

#warn!Object



81
82
83
# File 'lib/mode/logger.rb', line 81

def warn!
  set_log_level(Logger::WARN)
end