Class: Bovem::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/bovem/logger.rb

Overview

A custom logger.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logdev, shift_age = 0, shift_size = 1048576) ⇒ Logger

Creates a new logger.

Parameters:

  • logdev (String|IO)

    The log device. This is a filename (String) or IO object (typically STDOUT, STDERR, or an open file).

  • shift_age (Fixnum) (defaults to: 0)

    Number of old log files to keep, or frequency of rotation (daily, weekly or monthly).

  • shift_size (Fixnum) (defaults to: 1048576)

    Maximum logfile size (only applies when shift_age is a number).

See Also:



28
29
30
31
# File 'lib/bovem/logger.rb', line 28

def initialize(logdev, shift_age = 0, shift_size = 1048576)
  @device = logdev
  super(logdev, shift_age, shift_size)
end

Class Attribute Details

.start_timeObject

Returns the value of attribute start_time.



16
17
18
# File 'lib/bovem/logger.rb', line 16

def start_time
  @start_time
end

Instance Attribute Details

#deviceIO|String (readonly)

Returns The file or device to log messages to.

Returns:

  • (IO|String)

    The file or device to log messages to.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bovem/logger.rb', line 12

class Logger < ::Logger
  # @attribute start_time
  #   @return [Time] The start time of first line. This allows to show a `T+0.1234` information into the log.
  class << self
    attr_accessor :start_time
  end

  attr_reader :device

  # Creates a new logger.
  #
  # @see http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html
  #
  # @param logdev [String|IO] The log device. This is a filename (String) or IO object (typically STDOUT, STDERR, or an open file).
  # @param shift_age [Fixnum]  Number of old log files to keep, or frequency of rotation (daily, weekly or monthly).
  # @param shift_size [Fixnum] Maximum logfile size (only applies when shift_age is a number).
  def initialize(logdev, shift_age = 0, shift_size = 1048576)
    @device = logdev
    super(logdev, shift_age, shift_size)
  end

  # Creates a new logger.
  #
  # @param file [String|IO] The log device. This is a filename (String) or IO object (typically STDOUT, STDERR, or an open file).
  # @param level [Fixnum] The minimum severity to log. See http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html for valid levels.
  # @param formatter [Proc] The formatter to use for logging.
  # @return [Logger] The new logger.
  def self.create(file = nil, level = Logger::INFO, formatter = nil)
    begin
      rv = new(get_real_file(file || default_file))
      rv.level = level.to_integer
      rv.formatter = formatter || default_formatter
      rv
    rescue
      raise Bovem::Errors::InvalidLogger
    end
  end

  # Translates a file to standard input or standard output in some special cases.
  #
  # @param file [String] The string to translate.
  # @return [String|IO] The translated file name.
  def self.get_real_file(file)
    case file
      when "STDOUT" then $stdout
      when "STDERR" then $stderr
      else file
    end
  end

  # The default file for logging.
  # @return [String|IO] The default file for logging.
  def self.default_file
    @default_file ||= $stdout
  end

  # The default formatter for logging.
  # @return [Proc] The default formatter for logging.
  def self.default_formatter
    @default_formatter ||= ::Proc.new {|severity, datetime, _, msg|
      color = case severity
        when "DEBUG" then :cyan
        when "INFO" then :green
        when "WARN" then :yellow
        when "ERROR" then :red
        when "FATAL" then :magenta
        else :white
      end

      header = ::Bovem::Console.replace_markers("{mark=bright-#{color}}[%s T+%0.5f] %s:{/mark}" %[datetime.strftime("%Y/%b/%d %H:%M:%S"), [datetime.to_f - start_time.to_f, 0].max, severity.rjust(5)])
      "%s %s\n" % [header, msg]
    }
  end

  # The log time of the first logger. This allows to show a `T+0.1234` information into the log.
  # @return [Time] The log time of the first logger.
  def start_time
    @start_time ||= ::Time.now
  end
end

Class Method Details

.create(file = nil, level = Logger::INFO, formatter = nil) ⇒ Logger

Creates a new logger.

Parameters:

  • file (String|IO) (defaults to: nil)

    The log device. This is a filename (String) or IO object (typically STDOUT, STDERR, or an open file).

  • level (Fixnum) (defaults to: Logger::INFO)

    The minimum severity to log. See http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html for valid levels.

  • formatter (Proc) (defaults to: nil)

    The formatter to use for logging.

Returns:

  • (Logger)

    The new logger.



39
40
41
42
43
44
45
46
47
48
# File 'lib/bovem/logger.rb', line 39

def self.create(file = nil, level = Logger::INFO, formatter = nil)
  begin
    rv = new(get_real_file(file || default_file))
    rv.level = level.to_integer
    rv.formatter = formatter || default_formatter
    rv
  rescue
    raise Bovem::Errors::InvalidLogger
  end
end

.default_fileString|IO

The default file for logging.

Returns:

  • (String|IO)

    The default file for logging.



64
65
66
# File 'lib/bovem/logger.rb', line 64

def self.default_file
  @default_file ||= $stdout
end

.default_formatterProc

The default formatter for logging.

Returns:

  • (Proc)

    The default formatter for logging.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bovem/logger.rb', line 70

def self.default_formatter
  @default_formatter ||= ::Proc.new {|severity, datetime, _, msg|
    color = case severity
      when "DEBUG" then :cyan
      when "INFO" then :green
      when "WARN" then :yellow
      when "ERROR" then :red
      when "FATAL" then :magenta
      else :white
    end

    header = ::Bovem::Console.replace_markers("{mark=bright-#{color}}[%s T+%0.5f] %s:{/mark}" %[datetime.strftime("%Y/%b/%d %H:%M:%S"), [datetime.to_f - start_time.to_f, 0].max, severity.rjust(5)])
    "%s %s\n" % [header, msg]
  }
end

.get_real_file(file) ⇒ String|IO

Translates a file to standard input or standard output in some special cases.

Parameters:

  • file (String)

    The string to translate.

Returns:

  • (String|IO)

    The translated file name.



54
55
56
57
58
59
60
# File 'lib/bovem/logger.rb', line 54

def self.get_real_file(file)
  case file
    when "STDOUT" then $stdout
    when "STDERR" then $stderr
    else file
  end
end

Instance Method Details

#start_timeTime

The log time of the first logger. This allows to show a T+0.1234 information into the log.

Returns:

  • (Time)

    The log time of the first logger.



88
89
90
# File 'lib/bovem/logger.rb', line 88

def start_time
  @start_time ||= ::Time.now
end