Sha256: f3245cde3599ad8db3ef8d551fe2870589b1fdb739da39a068580c27ca8e012d

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 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'


# The default log formatter class.
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

2 entries across 2 versions & 1 rubygems

Version Path
loggability-0.0.2 lib/loggability/formatter/color.rb
loggability-0.0.1 lib/loggability/formatter/color.rb