Module: Logging

Includes:
File_Checking
Included in:
ArgParser, Download, DownloadHistory, DownloadHistory, DownloadProgress, Network, Rget, TClass, TClass
Defined in:
lib/logging.rb

Overview

of the object. Example for a class-level logger: # ——————– class TClass self.extend(Logging) @@log = init_logger(STDOUT) def test_log @@log.info('class-level logger called from instance: ' << @@log.to_s) @log = @@log @log.info('AGAIN: class-level logger called from instance: ' << @log.to_s) end def self::test_log @log.info('class-level logger called from class: ' << @log.to_s) @@log.info('AGAIN: class-level logger called from class: ' << @@log.to_s) end end #——————— Example for a object-level logger: ATTN! This means 1 logger per object. # ——————– class TClass include Logging def initialize init_logger(STDOUT, Logger::DEBUG) end def test_log @log.debug('called test_log() ') end end

Constant Summary collapse

@@have_log =
false
@@LOG_CONF =
File.dirname(File.absolute_path(__FILE__)) << File::Separator << 'log.conf'

Instance Method Summary collapse

Methods included from File_Checking

#file_check, file_check

Instance Method Details

#init_logger(target = STDOUT, level = Logger::INFO) ⇒ Object

Call this method in an instance-method (e.g. initialize() ) to define the object-level logger; i.e. an object-specific member @log. Call this method within the class-definition for a class-level logger; i.e. a member @log for class-level acces. The method returns the logger, so you can actually do what you want with it.


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/logging.rb', line 67

def init_logger(target = STDOUT, level = Logger::INFO)
	# Prepare for a class-level logger. This is actually quite cool.
	
	# ---> Ingeniuous code starts here
	cn = (self.class == Class ? name : self.class.name)
	# <--- Ingeniuous code ends here
	
	# allow to override the set log-levels with an
	# external configuration (log.conf).
	log_conf(cn)
	# Or use the defaults as set here or elsewhere...

	@level ||= level 
	@target ||= target

	@log = Logger.new(@target)
	@log.level = @level

	@log.formatter = proc do |severity, datetime, progname, msg|
		t = Time.now
		"#{cn}: #{severity} #{t.hour}-#{t.min}-#{t.sec}: #{msg}\n"
	end
	if ! @@have_log
		@log.debug cn.dup << ' reading logging-configuration from ' << @@LOG_CONF	
		@@have_log = true
		@log.debug('level is ' << level.to_s)
	end
	return @log
end

#log_level=(level) ⇒ Object

set the log-level


105
106
107
108
# File 'lib/logging.rb', line 105

def log_level=(level)
	@level = level
	@log.level = @level
end

#log_target=(target) ⇒ Object

Set the log-target to an IO object.


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

def log_target=(target)
	@target = target
	@log = Logger.new(@@target)
	@log.level = @level
end