Class: CertificateDepot::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/certificate_depot/log.rb

Overview

Simple thead-safe logger implementation.

log = Log.new('/var/log/depot.log', :level => Log::INFO)
log.fatal('I am completely operational, and all my circuits are functioning perfectly.')
log.close

Constant Summary collapse

DEBUG =
0
INFO =
1
WARN =
2
ERROR =
3
FATAL =
4
UNKNOWN =
5
SILENT =

Used to stop logging altogether

9

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Log

Creates a new Log instance.


23
24
25
26
# File 'lib/certificate_depot/log.rb', line 23

def initialize(file, options={})
  @file  = file
  @level = options[:level] || DEBUG
end

Instance Attribute Details

#fileObject

Holds the current log file


18
19
20
# File 'lib/certificate_depot/log.rb', line 18

def file
  @file
end

#levelObject

Holds the current log level


20
21
22
# File 'lib/certificate_depot/log.rb', line 20

def level
  @level
end

Class Method Details

.format(*args) ⇒ Object

Format the log message


57
58
59
# File 'lib/certificate_depot/log.rb', line 57

def self.format(*args)
  ["[#{Process.pid.to_s.rjust(5)}] ", Time.now.strftime("%Y-%m-%d %H:%M:%S"), '| ', args.first, "\n"].join
end

Instance Method Details

#closeObject

Close the logger


52
53
54
# File 'lib/certificate_depot/log.rb', line 52

def close
  @file.close
end

#debug(*args) ⇒ Object

Log if the error level is debug or lower.


29
# File 'lib/certificate_depot/log.rb', line 29

def debug(*args); log(DEBUG, *args); end

#error(*args) ⇒ Object

Log if the error level is error or lower.


35
# File 'lib/certificate_depot/log.rb', line 35

def error(*args); log(ERROR, *args); end

#fatal(*args) ⇒ Object

Log if the error level is fatal or lower.


37
# File 'lib/certificate_depot/log.rb', line 37

def fatal(*args); log(FATAL, *args); end

#info(*args) ⇒ Object

Log if the error level is info or lower.


31
# File 'lib/certificate_depot/log.rb', line 31

def info(*args); log(INFO, *args); end

#log(message_level, *args) ⇒ Object

Writes a message to the log is the current loglevel is equal or greater than the message_level.

log.log(Log::DEBUG, "This is a debug message")

44
45
46
47
48
49
# File 'lib/certificate_depot/log.rb', line 44

def log(message_level, *args)
  @file.flock(File::LOCK_EX)
  @file.write(self.class.format(*args)) if message_level >= level
  @file.flock(File::LOCK_UN)
rescue IOError
end

#unknown(*args) ⇒ Object

Log if the error level is unknown or lower.


39
# File 'lib/certificate_depot/log.rb', line 39

def unknown(*args); log(UNKNOWN, *args); end

#warn(*args) ⇒ Object

Log if the error level is warn or lower.


33
# File 'lib/certificate_depot/log.rb', line 33

def warn(*args); log(WARN, *args); end