Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Logger.new

Logger.new(logdev, shift_age = 0, shift_size = 1048576, **options)

With the single argument logdev, returns a new logger with all default options:

Example
Logger.new('t.log') # => #<Logger:0x000001e685dc6ac8>

Argument logdev must be one of:

  • A string filepath: entries are to be written to the file at that path; if the file at that path exists, new entries are appended.

  • An IO stream (typically +$stdout+, +$stderr+. or an open file): entries are to be written to the given stream.

  • nil or File::NULL: no entries are to be written.

Examples:

Example
Logger.new('t.log')
Logger.new($stdout)

The keyword options are:

  • level: sets the log level; default value is Logger::DEBUG. See Log Level:

    Example
    Logger.new('t.log', level: Logger::ERROR)
    
  • progname: sets the default program name; default is nil. See Program Name:

    Example
    Logger.new('t.log', progname: 'mung')
    
  • formatter: sets the entry formatter; default is nil. See formatter=.

  • datetime_format: sets the format for entry timestamp; default is nil. See datetime_format=.

  • binmode: sets whether the logger writes in binary mode; default is false.

  • shift_period_suffix: sets the format for the filename suffix for periodic log file rotation; default is '%Y%m%d'. See Periodic Rotation.

See also #<< #add also in 3.4 · 3.3 · 3.1 · 3.0