Sha256: f122ce8724cd230997f4b19ca14f0be4ec7642b1fd6bec521beae8d63ca02e7e

Contents?: true

Size: 1.18 KB

Versions: 22

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Bridgetown
  class LogWriter < ::Logger
    def initialize
      @progname = nil
      @level = DEBUG
      @default_formatter = Formatter.new
      @logdev = $stdout
      @formatter = proc do |_, _, _, msg|
        msg.to_s
      end
    end

    def add(severity, message = nil, progname = nil)
      severity ||= UNKNOWN
      @logdev = logdevice(severity)

      return true if @logdev.nil? || severity < @level

      progname ||= @progname
      if message.nil?
        if block_given?
          message = yield
        else
          message = progname
          progname = @progname
        end
      end
      @logdev.puts(
        format_message(format_severity(severity), Time.now, progname, message)
      )
      true
    end

    # Log a `WARN` message
    def warn(progname = nil, &block)
      add(WARN, nil, progname.yellow, &block)
    end

    # Log an `ERROR` message
    def error(progname = nil, &block)
      add(ERROR, nil, progname.red, &block)
    end

    def close
      # No LogDevice in use
    end

    private

    def logdevice(severity)
      if severity > INFO
        $stderr
      else
        $stdout
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
bridgetown-core-0.21.5 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.21.4 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.21.3 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.21.2 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.21.1 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.21.0 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.21.0.beta4 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.21.0.beta3 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.21.0.beta2 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.21.0.beta1 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.20.0 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.19.3 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.19.2 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.19.1 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.19.0 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.18.6 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.18.5 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.18.4 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.18.3 lib/bridgetown-core/log_writer.rb
bridgetown-core-0.18.2 lib/bridgetown-core/log_writer.rb