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.5"
@@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).



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/spawning_logger.rb', line 109

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



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

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

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

Yields:

  • (_self)

Yield Parameters:



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

def self.configure
  yield self
end

.subdir=(value) ⇒ Object



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

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

Instance Method Details

#self_and_spawn(child_name, method, message) ⇒ Object

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

Parameters:

  • child_name

    the child to spawn and log into

  • method

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

  • message

    the message to send to both loggers



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

def 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:



124
125
126
127
128
129
# File 'lib/spawning_logger.rb', line 124

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