Class: Dynamosaurus::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamosaurus/logger.rb

Constant Summary collapse

Levels =
{
  :fatal => 7,
  :error => 6,
  :warn  => 4,
  :info  => 3,
  :debug => 0
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Logger

To initialize the logger you create a new object, proxies to set_log.

Parameters

*args

Arguments to create the log from. See set_logs for specifics.



48
49
50
51
52
53
# File 'lib/dynamosaurus/logger.rb', line 48

def initialize(*args)
  @init_args = args
  set_log(*args)
  self.auto_flush = true
  Dynamosaurus.logger = self
end

Instance Attribute Details

#auto_flushObject

Returns the value of attribute auto_flush.



6
7
8
# File 'lib/dynamosaurus/logger.rb', line 6

def auto_flush
  @auto_flush
end

#bufferObject (readonly)

Returns the value of attribute buffer.



7
8
9
# File 'lib/dynamosaurus/logger.rb', line 7

def buffer
  @buffer
end

#delimiterObject

Returns the value of attribute delimiter.



5
6
7
# File 'lib/dynamosaurus/logger.rb', line 5

def delimiter
  @delimiter
end

#init_argsObject (readonly)

Returns the value of attribute init_args.



9
10
11
# File 'lib/dynamosaurus/logger.rb', line 9

def init_args
  @init_args
end

#levelObject

Returns the value of attribute level.



4
5
6
# File 'lib/dynamosaurus/logger.rb', line 4

def level
  @level
end

#logObject (readonly)

Returns the value of attribute log.



8
9
10
# File 'lib/dynamosaurus/logger.rb', line 8

def log
  @log
end

Instance Method Details

#<<(string = nil) ⇒ Object Also known as: push

Appends a message to the log. The methods yield to an optional block and the output of this block will be appended to the message.

Parameters

string<String>

The message to be logged. Defaults to nil.

Returns

String

The resulting message added to the log file.



101
102
103
104
105
106
107
108
109
110
# File 'lib/dynamosaurus/logger.rb', line 101

def <<(string = nil)
  message = ""
  message << delimiter
  message << string if string
  message << "\n" unless message[-1] == ?\n
  @buffer << message
  flush if @auto_flush

  message
end

#closeObject

Close and remove the current log object.



87
88
89
90
91
# File 'lib/dynamosaurus/logger.rb', line 87

def close
  flush
  @log.close if @log.respond_to?(:close) && !@log.tty?
  @log = nil
end

#flushObject

Flush the entire buffer to the log object.



81
82
83
84
# File 'lib/dynamosaurus/logger.rb', line 81

def flush
  return unless @buffer.size > 0
  @log.write(@buffer.slice!(0..-1).join)
end

#set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false) ⇒ Object

Replaces an existing logger with a new one.

Parameters

log<IO, String>

Either an IO object or a name of a logfile.

log_level<~to_sym>

The log level from, e.g. :fatal or :info. Defaults to :error in the production environment and :debug otherwise.

delimiter<String>

Delimiter to use between message sections. Defaults to “ ~ ”.

auto_flush<Boolean>

Whether the log should automatically flush after new messages are added. Defaults to false.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dynamosaurus/logger.rb', line 67

def set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false)
  if log_level && Levels[log_level.to_sym]
    @level = Levels[log_level.to_sym]
  else
    @level = Levels[:debug]
  end
  @buffer     = []
  @delimiter  = delimiter
  @auto_flush = auto_flush

  initialize_log(log)
end