Sha256: c5ca3490ab9c1ea15cb6090173d79d79ad72dc3d1dffab702f8d70557e7cb786

Contents?: true

Size: 1.18 KB

Versions: 9

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.

require 'logger'

module Console
	module Compatible
		class Logger < ::Logger
			class LogDevice
				def initialize(subject, output)
					@subject = subject
					@output = output
				end
				
				def write(message)
					@output.call(@subject, message)
				end
				
				def call(*arguments, **options)
					@output.call(*arguments, **options)
				end
				
				def reopen
				end
				
				def close
				end
			end

			def initialize(subject, output)
				super(nil)
				
				@progname = subject
				@logdev = LogDevice.new(subject, output)
			end
			
			def add(severity, message = nil, progname = nil)
				severity ||= UNKNOWN
				
				if @logdev.nil? or severity < level
					return true
				end
				
				if progname.nil?
					progname = @progname
				end
				
				if message.nil?
					if block_given?
						message = yield
					else
						message = progname
						progname = @progname
					end
				end
				
				@logdev.call(
					progname, message,
					severity: format_severity(severity)
				)
				
				return true
			end
			
			def format_severity(value)
				super.downcase.to_sym
			end
		end
	end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
console-1.20.0 lib/console/compatible/logger.rb
console-1.19.0 lib/console/compatible/logger.rb
console-1.18.0 lib/console/compatible/logger.rb
console-1.17.4 lib/console/compatible/logger.rb
console-1.17.3 lib/console/compatible/logger.rb
console-1.17.2 lib/console/compatible/logger.rb
console-1.17.1 lib/console/compatible/logger.rb
console-1.17.0 lib/console/compatible/logger.rb
console-1.16.2 lib/console/compatible/logger.rb