Sha256: b33e73ba4cb2179103b167793dc9ce7c21df2e1f77cadcef9e29cb218f657d2e

Contents?: true

Size: 1.91 KB

Versions: 65

Compression:

Stored size: 1.91 KB

Contents

require 'logger'

require 'pp'

#== MonoLogger
# A subclass of Ruby's stdlib Logger with all the mutex and logrotation stuff
# ripped out.
class MonoLogger < Logger

  #
  # === Synopsis
  #
  #   Logger.new(name, shift_age = 7, shift_size = 1048576)
  #   Logger.new(name, shift_age = 'weekly')
  #
  # === Args
  #
  # +logdev+::
  #   The log device.  This is a filename (String) or IO object (typically
  #   +STDOUT+, +STDERR+, or an open file).
  # +shift_age+::
  #   Number of old log files to keep, *or* frequency of rotation (+daily+,
  #   +weekly+ or +monthly+).
  # +shift_size+::
  #   Maximum logfile size (only applies when +shift_age+ is a number).
  #
  # === Description
  #
  # Create an instance.
  #
  def initialize(logdev)
    @progname = nil
    @level = DEBUG
    @default_formatter = Formatter.new
    @formatter = nil
    @logdev = nil
    if logdev
      @logdev = LocklessLogDevice.new(logdev)
    end
  end


  class LocklessLogDevice < LogDevice

    def initialize(log = nil)
      @dev = @filename = @shift_age = @shift_size = nil
      if log.respond_to?(:write) and log.respond_to?(:close)
        @dev = log
      else
        @dev = open_logfile(log)
        @filename = log
      end
      @dev.sync = true
    end

    def write(message)
      @dev.write(message)
    rescue Exception => ignored
      warn("log writing failed. #{ignored}")
    end

    def close
      @dev.close rescue nil
    end

  private

    def open_logfile(filename)
      if (FileTest.exist?(filename))
        open(filename, (File::WRONLY | File::APPEND))
      else
        create_logfile(filename)
      end
    end

    def create_logfile(filename)
      logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
      add_log_header(logdev)
      logdev
    end

    def add_log_header(file)
      file.write(
        "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]
      )
    end

  end


end

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
chef-11.14.0.alpha.2 lib/chef/monologger.rb
chef-11.12.4-x86-mingw32 lib/chef/monologger.rb
chef-11.12.4 lib/chef/monologger.rb
chef-11.12.4.rc.2-x86-mingw32 lib/chef/monologger.rb
chef-11.12.4.rc.2 lib/chef/monologger.rb
chef-11.12.2-x86-mingw32 lib/chef/monologger.rb
chef-11.12.2 lib/chef/monologger.rb
chef-11.12.0-x86-mingw32 lib/chef/monologger.rb
chef-11.12.0 lib/chef/monologger.rb
chef-11.12.0.rc.1-x86-mingw32 lib/chef/monologger.rb
chef-11.12.0.rc.1 lib/chef/monologger.rb
chef-11.12.0.alpha.1-x86-mingw32 lib/chef/monologger.rb
chef-11.12.0.alpha.1 lib/chef/monologger.rb
chef-11.10.4.ohai7.0-x86-mingw32 lib/chef/monologger.rb
chef-11.10.4.ohai7.0 lib/chef/monologger.rb
chef-11.10.4 lib/chef/monologger.rb
chef-11.10.4-x86-mingw32 lib/chef/monologger.rb
chef-11.10.2-x86-mingw32 lib/chef/monologger.rb
chef-11.10.2 lib/chef/monologger.rb
chef-11.10.0-x86-mingw32 lib/chef/monologger.rb