Class: Yell::Logger

Inherits:
Object
  • Object
show all
Includes:
Yell::Level::Helpers
Defined in:
lib/yell/logger.rb

Overview

The Yell::Logger is your entrypoint. Anything onwards is derived from here.

A Yell::Logger instance holds all your adapters and sends the log events to them if applicable. There are multiple ways of how to create a new logger.

Instance Attribute Summary collapse

Attributes included from Yell::Level::Helpers

#level

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|_self| ... } ⇒ Logger

Initialize a new Logger

Examples:

A standard file logger

Yell::Logger.new 'development.log'

A standard datefile logger

Yell::Logger.new :datefile
Yell::Logger.new :datefile, 'development.log'

Setting the log level

Yell::Logger.new :level => :warn

Yell::Logger.new do |l|
  l.level = :warn
end

Combined settings

Yell::Logger.new 'development.log', :level => :warn

Yell::Logger.new :datefile, 'development.log' do |l|
  l.level = :info
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Yell::Logger)

    the object that the method was called on



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yell/logger.rb', line 39

def initialize( *args )
  @adapters = []

  # extract options
  @options = args.last.is_a?(Hash) ? args.pop : {}

  # adapters may be passed in the options
  _extract_adapters!( @options )

  # check if filename was given as argument and put it into the @options
  if [String, Pathname].include?(args.last.class)
    @options[:filename] = args.pop unless @options[:filename]
  end

  # set the log level when given
  self.level = @options[:level]

  # set the loggeer's name
  self.name = @options[:name] if @options[:name]

  # extract adapter
  self.adapter args.pop if args.any?

  # eval the given block
  yield(self) if block_given?

  # default adapter when none defined
  self.adapter :file if @adapters.empty?
end

Instance Attribute Details

#nameObject

The name of the logger instance



15
16
17
# File 'lib/yell/logger.rb', line 15

def name
  @name
end

Instance Method Details

#adapter(type = :file, *args, &block) ⇒ Yell::Adapter

Define an adapter to be used for logging.

Examples:

Standard adapter

adapter :file

Standard adapter with filename

adapter :file, 'development.log'

# Alternative notation for filename in options
adapter :file, :filename => 'developent.log'

Standard adapter with filename and additional options

adapter :file, 'development.log', :level => :warn

Set the adapter directly from an adapter instance

adapter( Yell::Adapter::File.new )

Parameters:

  • type (Symbol) (defaults to: :file)

    The type of the adapter, may be ‘:file` or `:datefile` (default `:file`)

Returns:

  • (Yell::Adapter)

    The instance

Raises:

  • (Yell::NoSuchAdapter)

    Will be thrown when the adapter is not defined



91
92
93
94
95
96
97
# File 'lib/yell/logger.rb', line 91

def adapter( type = :file, *args, &block )
  options = [@options, *args].inject( Hash.new ) do |h, c|
    h.merge( [String, Pathname].include?(c.class) ? {:filename => c} : c  )
  end

  @adapters << Yell::Adapters.new( type, options, &block )
end

#closeObject

Convenience method for resetting all adapters of the Logger.



104
105
106
# File 'lib/yell/logger.rb', line 104

def close
  @adapters.each(&:close)
end

#inspectString

Get a pretty string representation of the logger.

Examples:

Inspect the logger

logger.inspect

Returns:

  • (String)

    The inspection string.



135
136
137
138
# File 'lib/yell/logger.rb', line 135

def inspect
  inspection = inspectables.inject( [] ) { |r, c| r << "#{c}: #{send(c).inspect}" }
  "#<#{self.class.name} #{inspection * ', '}, adapters: #{@adapters.map(&:inspect) * ', '}>"
end