Class: Sappho::AutoFlushLog

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

Direct Known Subclasses

ApplicationAutoFlushLog

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" },
    'test' => proc { |severity, datetime, progname, message| "#{severity} #{message}\n" }
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, level, detail) ⇒ AutoFlushLog

Returns a new instance of AutoFlushLog.



26
27
28
29
30
31
# File 'lib/sappho-basics/auto_flush_log.rb', line 26

def initialize file, level, detail
  @mutex = Mutex.new
  @log = Logger.new file
  @log.level = LOG_LEVELS.has_key?(level) ? LOG_LEVELS[level] : Logger::INFO
  @log.formatter = LOG_DETAIL[detail] if LOG_DETAIL.has_key?(detail)
end

Class Method Details

.file(file) ⇒ Object



82
83
84
# File 'lib/sappho-basics/auto_flush_log.rb', line 82

def AutoFlushLog.file file
  @@file = file
end

Instance Method Details

#debug(message) ⇒ Object



33
34
35
36
37
38
# File 'lib/sappho-basics/auto_flush_log.rb', line 33

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

#debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  @log.debug?
end

#error(error) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/sappho-basics/auto_flush_log.rb', line 54

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



62
63
64
65
66
67
68
# File 'lib/sappho-basics/auto_flush_log.rb', line 62

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

#info(message) ⇒ Object



40
41
42
43
44
45
# File 'lib/sappho-basics/auto_flush_log.rb', line 40

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

#warn(message) ⇒ Object



47
48
49
50
51
52
# File 'lib/sappho-basics/auto_flush_log.rb', line 47

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