Module: Output

Defined in:
lib/output/output.rb,
lib/output/writer.rb,
lib/output/devices.rb,
lib/output/build_logger.rb,
lib/output/writer_macro.rb

Defined Under Namespace

Modules: ClassMethods, Devices Classes: Writer, WriterMacro

Constant Summary collapse

DEFAULT_LOGGER_LEVEL =
:info
DEFAULT_PATTERN =
'%m\n'
DEFAULT_DEVICE =
:stdout

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#add_device(device) ⇒ Object



177
178
179
180
181
182
# File 'lib/output/output.rb', line 177

def add_device(device)
  each_writer do |writer|
    writer.add_device device
  end
  device
end

#build_writer(name, level, device_options = nil, message_transformer = nil) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/output/output.rb', line 44

def build_writer(name, level, device_options = nil, message_transformer=nil)
  device_options ||= {}
  device_options = self.class.device_options.merge device_options
  logger_name = Writer::Naming.fully_qualified(self.class, name)
  writer = Writer.build name, level, message_transformer, self.level, logger_name, device_options
  writer
end

#disableObject



12
13
14
15
16
17
18
# File 'lib/output/output.rb', line 12

def disable
  each_writer { |w| w.disable }
  if block_given?
    yield
    enable
  end
end

#each_writerObject



52
53
54
55
56
57
# File 'lib/output/output.rb', line 52

def each_writer
  self.class.writer_names.each do |name|
    writer = send self.class.writer_attribute(name)
    yield writer
  end
end

#enableObject



20
21
22
# File 'lib/output/output.rb', line 20

def enable
  each_writer { |w| w.enable }
end

#initial_level?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/output/output.rb', line 69

def initial_level?
  level == self.class.logger_level
end

#last_methodObject



32
33
34
# File 'lib/output/output.rb', line 32

def last_method
  @last_method
end

#last_method=(val) ⇒ Object



36
37
38
# File 'lib/output/output.rb', line 36

def last_method=(val)
  @last_method = val
end

#last_method?(*methods) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/output/output.rb', line 40

def last_method?(*methods)
  methods.include? last_method
end

#levelObject



59
60
61
# File 'lib/output/output.rb', line 59

def level
  @level ||= self.class.logger_level
end

#level=(level) ⇒ Object



63
64
65
66
67
# File 'lib/output/output.rb', line 63

def level=(level)
  @level = level
  each_writer { |w| w.logger_level = level }
  level
end

#levelsObject



73
74
75
# File 'lib/output/output.rb', line 73

def levels
  @levels ||= []
end

#pop_deviceObject



169
170
171
172
173
174
# File 'lib/output/output.rb', line 169

def pop_device
  each_writer do |writer|
    writer.pop_device
  end
  nil
end

#pop_levelObject



97
98
99
100
101
# File 'lib/output/output.rb', line 97

def pop_level
  level = levels.shift unless levels.empty?
  self.level = level
  level
end

#push_device(device, options = {}, &block) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/output/output.rb', line 144

def push_device(device, options = {}, &block)
  return device if device.nil?

  return push_device__obj(device, &block) if device.is_a? Logging::Appender

  push_device__opts(type = device, options, &block)
end

#push_device__obj(device, &block) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/output/output.rb', line 158

def push_device__obj(device, &block)
  each_writer do |writer|
    writer.push_device device
  end
  if block_given?
    yield
    pop_device
  end
  device
end

#push_device__opts(type, options = {}, &block) ⇒ Object



152
153
154
155
156
# File 'lib/output/output.rb', line 152

def push_device__opts(type, options = {}, &block)
  options = self.class.device_options.merge(options)
  device = Output::Devices.build_device(type, options)
  push_device__obj device, &block
end

#push_level(level) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/output/output.rb', line 85

def push_level(level)
  levels.unshift self.level
  self.level = level

  if block_given?
    yield
    self.level = pop_level
  end
  
  level
end

#reset_levelObject



77
78
79
80
81
82
83
# File 'lib/output/output.rb', line 77

def reset_level
  @level = self.class.logger_level

  each_writer do |writer|
    writer.reset_level
  end
end

#suspend_devices(device, &block) ⇒ Object



103
104
105
106
# File 'lib/output/output.rb', line 103

def suspend_devices(device, &block)
  return suspend_devices__obj(device, &block) if device.is_a? Logging::Appender
  suspend_devices__name device, &block
end

#suspend_devices__device_selector(device_selector, &block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/output/output.rb', line 119

def suspend_devices__device_selector(device_selector, &block)
  suspensions = []

  each_writer do |writer|
    device = device_selector.call writer
    suspension = Writer::DeviceSuspension.new writer, device
    suspensions << suspension
    suspension.suspend
  end

  yield

  suspensions.each { |suspension| suspension.restore }
end

#suspend_devices__name(name, &block) ⇒ Object



114
115
116
117
# File 'lib/output/output.rb', line 114

def suspend_devices__name(name, &block)
  device_selector = ->(writer) { writer.device name }
  suspend_devices__device_selector device_selector, &block
end

#suspend_devices__obj(device, &block) ⇒ Object



108
109
110
111
# File 'lib/output/output.rb', line 108

def suspend_devices__obj(device, &block)
  device_selector = ->(writer) { device }
  suspend_devices__device_selector device_selector, &block
end

#to_sObject



134
135
136
137
138
139
140
141
142
# File 'lib/output/output.rb', line 134

def to_s
  details = "Output : #{self.class.name} - Object Id:#{self.object_id}\n"
  details << "Level: #{self.level}\n"
  details << "Writer Details: \n"
  each_writer do |writer|
    details << "#{writer.to_s}\n"
  end
  details
end

#write(method, message) ⇒ Object



24
25
26
# File 'lib/output/output.rb', line 24

def write(method, message)
  send method, message
end

#writer(name) ⇒ Object



28
29
30
# File 'lib/output/output.rb', line 28

def writer(name)
  send self.class.writer_attribute(name)
end