Class: SpawningLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/version.rb,
lib/spawning_logger.rb

Defined Under Namespace

Classes: ArgumentError

Constant Summary collapse

VERSION =
"0.0.3"
@@child_prefix =

cattr_accessor :child_prefix cattr_accessor :subdir

nil
@@subdir =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, subdir = nil) ⇒ SpawningLogger

creates the logfile inside a subdir (optional).



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/spawning_logger.rb', line 105

def initialize(file_path, subdir = nil)
  file_path = File.expand_path(file_path)

  @log_dir = File.dirname(file_path)
  @log_dir = File.join(@log_dir, @@subdir) unless @@subdir.nil?
  FileUtils.mkdir_p(@log_dir) if !Dir.exist?(@log_dir)

  @file_name = File.basename(file_path)
  @child_loggers = {} # these are the special sub-loggers

  super(File.join(@log_dir, @file_name)) # this creates the main logger
end

Class Method Details

.child_prefix=(value) ⇒ Object



96
97
98
# File 'lib/spawning_logger.rb', line 96

def self.child_prefix=(value)
  @@child_prefix = value
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



92
93
94
# File 'lib/spawning_logger.rb', line 92

def self.configure
  yield self
end

.subdir=(value) ⇒ Object



100
101
102
# File 'lib/spawning_logger.rb', line 100

def self.subdir=(value)
  @@subdir = value
end

Instance Method Details

#send_self_and_spawn(child_name, method, message) ⇒ Object

logs into the main logfile and also logs into a spawned logfile.

Parameters:

  • method

    The method name to call, like :error, :info, :debug, …

  • message

    the message to send to both loggers



130
131
132
133
# File 'lib/spawning_logger.rb', line 130

def send_self_and_spawn(child_name, method, message)
  self.send(method, message)
  self.spawn(child_name).send(method, message)
end

#spawn(child_name) ⇒ Object

creates a sub logger with filename <orig_file>_<child_prefix>_<child_name>.log example: see class docstring or README.md

Raises:



120
121
122
123
124
125
# File 'lib/spawning_logger.rb', line 120

def spawn(child_name)
  raise ArgumentError.new("empty child_name") if child_name.to_s.empty?

  @child_loggers[child_name] ||= create_child_logger(child_name)
  @child_loggers[child_name]
end