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.



16
17
18
19
20
21
22
23
# File 'lib/codesake/commons/logging.rb', line 16

def initialize
  super
  @silencer = false
  @verbose  = true
  @syslog   = true
  @filename = nil
  @component = ""
end

Instance Attribute Details

#componentObject (readonly)

Returns the value of attribute component.



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

def component
  @component
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

#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

#bye(pid_file = nil) ⇒ Object



66
67
68
69
70
71
# File 'lib/codesake/commons/logging.rb', line 66

def bye(pid_file = nil)
  STDOUT.print "#{Time.now.strftime("%H:%M:%S")} [*] #{@component} is leaving\n".color(:white)
  send_to_syslog("#{@component} is leaving", :helo)
  send_to_file("#{@component} is leaving", :helo)
  Codesake::Commons::Io.remove_pid_file(pid_file) unless pid_file.nil?
end

#die(msg, pid_file = nil) ⇒ Object



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

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

#err(msg) ⇒ Object



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

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

#helo(component, version, pid_file = nil) ⇒ Object



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

def helo(component, version, pid_file = nil)
  @component = component
  STDOUT.print "#{Time.now.strftime("%H:%M:%S")} [*] #{@component} v#{version} is starting up\n".color(:white)
  send_to_syslog("#{@component} v#{version} is starting up", :helo)
  send_to_file("#{@component} v#{version} is starting up", :helo)
  Codesake::Commons::Io.create_pid_file(pid_file) unless pid_file.nil?
end

#log(msg) ⇒ Object



51
52
53
54
55
56
# File 'lib/codesake/commons/logging.rb', line 51

def log(msg)
  return if @silencer
  STDOUT.print "#{Time.now.strftime("%H:%M:%S")} [$] #{@component}: #{msg}\n".color(:white)
  send_to_syslog(msg, :log)
  send_to_file(msg, :log)
end

#ok(msg) ⇒ Object



45
46
47
48
49
# File 'lib/codesake/commons/logging.rb', line 45

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

#toggle_silenceObject



73
74
75
76
# File 'lib/codesake/commons/logging.rb', line 73

def toggle_silence
  @silencer = ! @silencer
  @verbose  = ! @silencer
end

#toggle_syslogObject



78
79
80
81
82
# File 'lib/codesake/commons/logging.rb', line 78

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



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

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