Class: Loops::Logger

Inherits:
Delegator
  • Object
show all
Defined in:
lib/loops/logger.rb

Defined Under Namespace

Classes: LoggerImplementation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logfile = $stdout, level = ::Logger::INFO, number_of_files = 10, max_file_size = 100 * 1024 * 1024, write_to_console = false) ⇒ Logger

Initializes a new instance of the Loops::Logger class.

Parameters:

  • logfile (String, IO) (defaults to: $stdout)

    The log device. This is a filename (String), 'stdout' or 'stderr' (String), 'default' for default framework’s log file, or IO object (typically STDOUT, STDERR, or an open file).

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

    Logging level. Constants are defined in Logger namespace: DEBUG, INFO, WARN, ERROR, FATAL, or UNKNOWN.

  • number_of_files (Integer) (defaults to: 10)

    A number of files to keep.

  • max_file_size (Integer) (defaults to: 100 * 1024 * 1024)

    A max file size. When file become larger, next one will be created.

  • write_to_console (Boolean) (defaults to: false)

    When true, all logging output will be dumped to the STDOUT also.



33
34
35
36
37
38
39
# File 'lib/loops/logger.rb', line 33

def initialize(logfile = $stdout, level = ::Logger::INFO, number_of_files = 10, max_file_size = 100 * 1024 * 1024,
               write_to_console = false)
  @number_of_files, @level, @max_file_size, @write_to_console =
      number_of_files, level, max_file_size, write_to_console
  self.logfile = logfile
  super(@implementation)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Delegator’s method_missing ignores the &block argument (!!!?)



138
139
140
141
142
143
144
145
# File 'lib/loops/logger.rb', line 138

def method_missing(m, *args, &block)
  target = self.__getobj__
  unless target.respond_to?(m)
    super(m, *args, &block)
  else
    target.__send__(m, *args, &block)
  end
end

Instance Attribute Details

#colorful_logsBoolean

Returns A value inidicating whether critical errors should be highlighted with ANSI colors in the log.

Returns:

  • (Boolean)

    A value inidicating whether critical errors should be highlighted with ANSI colors in the log.



14
15
16
# File 'lib/loops/logger.rb', line 14

def colorful_logs
  @colorful_logs
end

#write_to_consoleBoolean

Returns A value indicating whether all logging output should be also duplicated to the console.

Returns:

  • (Boolean)

    A value indicating whether all logging output should be also duplicated to the console.



9
10
11
# File 'lib/loops/logger.rb', line 9

def write_to_console
  @write_to_console
end

Instance Method Details

#__getobj__Object

Send everything else to @implementation.



128
129
130
# File 'lib/loops/logger.rb', line 128

def __getobj__
  @implementation or raise "Logger implementation not initialized"
end

#__setobj__(obj) ⇒ Object



132
133
134
# File 'lib/loops/logger.rb', line 132

def __setobj__(obj)
  @implementation = obj
end

#default_logfile=(logfile) ⇒ String, IO

Sets the default log file (see #logfile=).

Parameters:

  • logfile (String, IO)

    the log file path or IO.

Returns:

  • (String, IO)

    the log file path or IO.



48
49
50
51
# File 'lib/loops/logger.rb', line 48

def default_logfile=(logfile)
  @default_logfile = logfile
  self.logfile = logfile
end

#level=(level) ⇒ Integer

Remember the level at the proxy level.

Parameters:

  • level (Integer)

    Logging severity.

Returns:

  • (Integer)

    Logging severity.



93
94
95
96
97
# File 'lib/loops/logger.rb', line 93

def level=(level)
  @level = level
  @implementation.level = @level if @implementation
  level
end

#logfile=(logfile) ⇒ String, IO

Sets the log file.

Parameters:

  • logfile (String, IO)

    The log device. This is a filename (String), 'stdout' or 'stderr' (String), 'default' for default framework’s log file, or IO object (typically STDOUT, STDERR, or an open file).

Returns:

  • (String, IO)

    the log device.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/loops/logger.rb', line 63

def logfile=(logfile)
  logfile = @default_logfile || $stdout if logfile == 'default'
  coerced_logfile =
      case logfile
      when 'stdout' then $stdout
      when 'stderr' then $stderr
      when IO, StringIO then logfile
      else
        if Loops.root
          logfile =~ /^\// ? logfile : Loops.root.join(logfile).to_s
        else
          logfile
        end
      end
  # Ensure logging directory does exist
  FileUtils.mkdir_p(File.dirname(coerced_logfile)) if String === coerced_logfile

  # Create a logger implementation.
  @implementation = LoggerImplementation.new(coerced_logfile, @number_of_files, @max_file_size, @write_to_console, @colorful_logs)
  @implementation.level = @level
  logfile
end