Class: Sappho::AutoFlushLog

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sappho-basics/auto_flush_log.rb

Constant Summary collapse

LOG_LEVELS =
{
    'debug' => Logger::DEBUG,
    'info' => Logger::INFO,
    'warn' => Logger::WARN,
    'error' => Logger::ERROR,
    'fatal' => Logger::FATAL
}
LOG_DETAIL =
{
    'message' => proc { |severity, datetime, progname, message| "#{message}\n" }
}

Instance Method Summary collapse

Constructor Details

#initializeAutoFlushLog

Returns a new instance of AutoFlushLog.



27
28
29
30
31
32
33
34
35
# File 'lib/sappho-basics/auto_flush_log.rb', line 27

def initialize
  @mutex = Mutex.new
  filename = ENV['application.log.filename']
  @log = Logger.new(filename ? File.open(filename, 'a') : $stdout)
  level = ENV['application.log.level']
  @log.level = LOG_LEVELS.has_key?(level) ? LOG_LEVELS[level] : Logger::INFO
  detail = ENV['application.log.detail']
  @log.formatter = LOG_DETAIL[detail] if LOG_DETAIL.has_key?(detail)
end

Instance Method Details

#debug(message) ⇒ Object



37
38
39
40
41
42
# File 'lib/sappho-basics/auto_flush_log.rb', line 37

def debug message
  @mutex.synchronize do
    @log.debug message
    $stdout.flush
  end if @log.debug?
end

#debug?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/sappho-basics/auto_flush_log.rb', line 74

def debug?
  @log.debug?
end

#error(error) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/sappho-basics/auto_flush_log.rb', line 58

def error error
  @mutex.synchronize do
    @log.error "error! #{error.message}"
    error.backtrace.each { |error| @log.error error }
    $stdout.flush
  end if @log.error?
end

#fatal(error) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/sappho-basics/auto_flush_log.rb', line 66

def fatal error
  @mutex.synchronize do
    @log.fatal "fatal error! #{error.message}"
    error.backtrace.each { |error| @log.fatal error }
    $stdout.flush
  end if @log.fatal?
end

#info(message) ⇒ Object



44
45
46
47
48
49
# File 'lib/sappho-basics/auto_flush_log.rb', line 44

def info message
  @mutex.synchronize do
    @log.info message
    $stdout.flush
  end if @log.info?
end

#warn(message) ⇒ Object



51
52
53
54
55
56
# File 'lib/sappho-basics/auto_flush_log.rb', line 51

def warn message
  @mutex.synchronize do
    @log.warn message
    $stdout.flush
  end if @log.warn?
end