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



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/spawning_logger.rb', line 82

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



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

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

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

Yields:

  • (_self)

Yield Parameters:



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

def self.configure
  yield self
end

.subdir=(value) ⇒ Object



77
78
79
# File 'lib/spawning_logger.rb', line 77

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

Instance Method Details

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



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

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