Class: Codesake::Commons::Logging

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/codesake/commons/logging.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogging

Returns a new instance of Logging.



14
15
16
17
18
19
# File 'lib/codesake/commons/logging.rb', line 14

def initialize
  super
  @silencer = false
  @verbose  = true
  @syslog   = true
end

Instance Attribute Details

#silencerObject (readonly)

Returns the value of attribute silencer.



10
11
12
# File 'lib/codesake/commons/logging.rb', line 10

def silencer
  @silencer
end

#syslogObject (readonly)

Returns the value of attribute syslog.



12
13
14
# File 'lib/codesake/commons/logging.rb', line 12

def syslog
  @syslog
end

#verboseObject (readonly)

Returns the value of attribute verbose.



11
12
13
# File 'lib/codesake/commons/logging.rb', line 11

def verbose
  @verbose
end

Instance Method Details

#die(msg, pid_file = nil) ⇒ Object



21
22
23
24
25
26
# File 'lib/codesake/commons/logging.rb', line 21

def die(msg, pid_file=nil)
  printf "#{Time.now.strftime("%H:%M:%S")} [!] #{msg}\n".color(:red)
  send_to_syslog(msg, :helo)
  Codesake::Commons::Io.remove_pid_file(pid_file) unless pid_file.nil?
  Kernel.exit(-1)
end

#err(msg) ⇒ Object



28
29
30
31
# File 'lib/codesake/commons/logging.rb', line 28

def err(msg)
  printf "#{Time.now.strftime("%H:%M:%S")} [!] #{msg}\n".color(:red)
  send_to_syslog(msg, :err)
end

#helo(msg, pid_file = nil) ⇒ Object



49
50
51
52
53
# File 'lib/codesake/commons/logging.rb', line 49

def helo(msg, pid_file = nil)
  printf "[*] #{msg} at #{Time.now.strftime("%H:%M:%S")}\n".color(:white)
  send_to_syslog(msg, :helo)
  create_pid_file(pid_file) unless pid_file.nil?
end

#log(msg) ⇒ Object



43
44
45
46
47
# File 'lib/codesake/commons/logging.rb', line 43

def log(msg)
  return if @silencer
  printf "#{Time.now.strftime("%H:%M:%S")}: #{msg}\n".color(:white)
  send_to_syslog(msg, :debug)
end

#ok(msg) ⇒ Object



38
39
40
41
# File 'lib/codesake/commons/logging.rb', line 38

def ok(msg)
  printf "#{Time.now.strftime("%H:%M:%S")} [*] #{msg}\n".color(:green)
  send_to_syslog(msg, :log)
end

#toggle_silenceObject



55
56
57
58
59
# File 'lib/codesake/commons/logging.rb', line 55

def toggle_silence
  @silencer = ! @silencer
  @verbose  = ! @silencer
  warn("silenced. Logs are disabled") if @silencer
end

#toggle_syslogObject



61
62
63
64
65
# File 'lib/codesake/commons/logging.rb', line 61

def toggle_syslog
  @syslog   = ! @syslog
  warn("codesake messages to syslog are now disabled") unless @syslog
  ok("codesake messages to syslog are now enabled") if @syslog
end

#warn(msg) ⇒ Object



33
34
35
36
# File 'lib/codesake/commons/logging.rb', line 33

def warn(msg)
  printf "#{Time.now.strftime("%H:%M:%S")} [!] #{msg}\n".color(:yellow)
  send_to_syslog(msg, :warn)
end