Sha256: 9bbb942e16fb5b8372d1ee25d090b0800ca66067cda630df7996a604600189b7

Contents?: true

Size: 1.23 KB

Versions: 24

Compression:

Stored size: 1.23 KB

Contents

#!/usr/bin/env ruby
# vim: set nosta noet ts=4 sw=4:
# encoding: utf-8

require 'loggability' unless defined?( Loggability )
require 'loggability/formatter' unless defined?( Loggability::Formatter )
require 'loggability/formatter/default'


# ANSI color log formatter. Outputs log messages color-coded by their
# severity if the current terminal appears to support it.
class Loggability::Formatter::Color < Loggability::Formatter::Default

	# ANSI reset
	RESET = "\e[0m"

	# ANSI color escapes keyed by severity
	LEVEL_CODES = {
		:debug => "\e[1;30m", # bold black
		:info  =>  "\e[37m",   # white
		:warn  =>  "\e[1;33m", # bold yellow
		:error => "\e[31m",   # red
		:fatal => "\e[1;31m", # bold red
	}

	# Pattern for matching color terminals
	COLOR_TERMINAL_NAMES = /(?:vt10[03]|xterm(?:-color)?|linux|screen)/i


	### Create a new formatter.
	def initialize( * )
		super
		@color_enabled = COLOR_TERMINAL_NAMES.match( ENV['TERM'] ) ? true : false
	end


	######
	public
	######

	### Format the specified +msg+ for output to the log.
	def msg2str( msg, severity )
		msg = super
		if @color_enabled
			color = severity.downcase.to_sym
			msg = [ LEVEL_CODES[color], msg, RESET ].join( '' )
		end

		return msg
	end

end # class Loggability::Formatter::Color

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
loggability-0.14.0 lib/loggability/formatter/color.rb
loggability-0.13.0 lib/loggability/formatter/color.rb
loggability-0.12.0 lib/loggability/formatter/color.rb
loggability-0.12.0.pre20161214121603 lib/loggability/formatter/color.rb
loggability-0.12.0.pre20161212115530 lib/loggability/formatter/color.rb
loggability-0.11.0 lib/loggability/formatter/color.rb
loggability-0.10.1 lib/loggability/formatter/color.rb
loggability-0.10.0 lib/loggability/formatter/color.rb
loggability-0.9.0 lib/loggability/formatter/color.rb
loggability-0.9.0.pre.73 lib/loggability/formatter/color.rb
loggability-0.8.1 lib/loggability/formatter/color.rb
loggability-0.8.0 lib/loggability/formatter/color.rb
loggability-0.8.0.pre.65 lib/loggability/formatter/color.rb
loggability-0.7.0 lib/loggability/formatter/color.rb
loggability-0.6.1 lib/loggability/formatter/color.rb
loggability-0.6.0 lib/loggability/formatter/color.rb
loggability-0.5.0 lib/loggability/formatter/color.rb
loggability-0.4.0 lib/loggability/formatter/color.rb
loggability-0.3.0 lib/loggability/formatter/color.rb
loggability-0.2.3 lib/loggability/formatter/color.rb