Sha256: d2c6f2a6302724ce301a1c9db420c44be7e643f4c6fb176e33ab3e89212a2af5

Contents?: true

Size: 1.33 KB

Versions: 29

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

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

    def enable_prefix
      @formatter = proc do |_, _, _, msg|
        "\e[32m[Bridgetown]\e[0m #{msg}"
      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

29 entries across 29 versions & 1 rubygems

Version Path
bridgetown-core-1.0.0.alpha10 lib/bridgetown-core/log_writer.rb
bridgetown-core-1.0.0.alpha9 lib/bridgetown-core/log_writer.rb
bridgetown-core-1.0.0.alpha8 lib/bridgetown-core/log_writer.rb
bridgetown-core-1.0.0.alpha7 lib/bridgetown-core/log_writer.rb
bridgetown-core-1.0.0.alpha6 lib/bridgetown-core/log_writer.rb
bridgetown-core-1.0.0.alpha5 lib/bridgetown-core/log_writer.rb
bridgetown-core-1.0.0.alpha4 lib/bridgetown-core/log_writer.rb
bridgetown-core-1.0.0.alpha3 lib/bridgetown-core/log_writer.rb
bridgetown-core-1.0.0.alpha2 lib/bridgetown-core/log_writer.rb