Class: Yell::Logger

Inherits:
Object
  • Object
show all
Includes:
Helpers::Adapter, Helpers::Base, Helpers::Formatter, Helpers::Level, Helpers::Silencer, Helpers::Tracer
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

Instance Method Summary collapse

Methods included from Helpers::Silencer

#silence, #silencer

Methods included from Helpers::Tracer

#trace, #trace=

Methods included from Helpers::Adapter

#adapter, #adapters

Methods included from Helpers::Formatter

#formatter, #formatter=

Methods included from Helpers::Level

#level, #level=

Constructor Details

#initialize(*args, &block) ⇒ 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


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
68
69
70
71
72
# File 'lib/yell/logger.rb', line 41

def initialize( *args, &block )
  # extract options
  @options = args.last.is_a?(Hash) ? args.pop : {}

  # 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

  reset!

  # FIXME: :format is deprecated in future versions --R
  self.formatter = Yell.__fetch__(@options, :format, :formatter)
  self.level = Yell.__fetch__(@options, :level, :default => 0)
  self.name = Yell.__fetch__(@options, :name)
  self.trace = Yell.__fetch__(@options, :trace, default: :error)

  # silencer
  self.silence(*Yell.__fetch__(@options, :silence, default: []))

  # adapters may be passed in the options
  extract!(*Yell.__fetch__(@options, :adapters, default: []))

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

  # eval the given block
  block.arity > 0 ? block.call(self) : instance_eval(&block) 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



17
18
19
# File 'lib/yell/logger.rb', line 17

def name
  @name
end

Instance Method Details

#add(options, *messages, &block) ⇒ Object

Somewhat backwards compatible method (not fully though)



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

def add( options, *messages, &block )
  return false unless level.at?(options)

  messages = messages
  messages << block.call unless block.nil?
  messages = silencer.call(*messages)
  return false if messages.empty?

  event = Yell::Event.new(self, options, *messages)
  write(event)
end

#closeObject



125
126
127
# File 'lib/yell/logger.rb', line 125

def close
  adapters.close
end

#inspectObject

Get a pretty string representation of the logger.



119
120
121
122
# File 'lib/yell/logger.rb', line 119

def inspect
  inspection = inspectables.map { |m| "#{m}: #{send(m).inspect}" }
  "#<#{self.class.name} #{inspection * ', '}>"
end

#write(event) ⇒ Object



130
131
132
# File 'lib/yell/logger.rb', line 130

def write( event )
  adapters.write(event)
end