Class: Fluent::Auditify::Log

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Log

Returns a new instance of Log.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluent/auditify/log.rb', line 7

def initialize(options={})
  @logger = Logger.new(STDOUT)
  @logger.level = options[:log_level] || Logger::INFO
  @logger.formatter = proc do |severity, datetime, progname, msg|
    if options[:color]
      case severity
      when "FATAL"
        "#{Term::ANSIColor.on_black{ Term::ANSIColor.red { severity }}}: #{msg}\n"
      when "ERROR"
        "#{Term::ANSIColor.on_black{ Term::ANSIColor.bright_red { severity }}}: #{msg}\n"
      when "WARN"
        "#{Term::ANSIColor.on_black{ Term::ANSIColor.bright_yellow { severity }}}: #{msg}\n"
      when "INFO"
        "#{Term::ANSIColor.on_black{ Term::ANSIColor.green { severity }}}: #{msg}\n"
      when "DEBUG"
        "#{Term::ANSIColor.on_black{ Term::ANSIColor.magenta { severity }}}: #{msg}\n"
      end
    else
      "#{severity}: #{msg}\n"
    end
  end
  @color = if options[:color] == :auto
             true
           else
             options[:color]
           end
end

Class Method Details

.to_logger_level(level) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fluent/auditify/log.rb', line 35

def self.to_logger_level(level)
  case level
  when "debug" then Logger::DEBUG
  when "info" then Logger::INFO
  when "warn" then Logger::WARN
  when "error" then Logger::ERROR
  when "fatal" then Logger::FATAL
  else
    Logger::INFO
  end
end

Instance Method Details

#debug(*args, &block) ⇒ Object



51
52
53
54
# File 'lib/fluent/auditify/log.rb', line 51

def debug(*args, &block)
  args << block.call if block
  @logger.debug(args.join.to_s)
end

#enable_color?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/fluent/auditify/log.rb', line 47

def enable_color?
  @color
end

#error(*args, &block) ⇒ Object



66
67
68
69
# File 'lib/fluent/auditify/log.rb', line 66

def error(*args, &block)
  args << block.call if block
  @logger.error(args.join.to_s)
end

#fatal(*args, &block) ⇒ Object



71
72
73
74
# File 'lib/fluent/auditify/log.rb', line 71

def fatal(*args, &block)
  args << block.call if block
  @logger.fatal(args.join.to_s)
end

#info(*args, &block) ⇒ Object



56
57
58
59
# File 'lib/fluent/auditify/log.rb', line 56

def info(*args, &block)
  args << block.call if block
  @logger.info(args.join.to_s)
end

#warn(*args, &block) ⇒ Object



61
62
63
64
# File 'lib/fluent/auditify/log.rb', line 61

def warn(*args, &block)
  args << block.call if block
  @logger.warn(args.join.to_s)
end