Class: Dima::AppLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/dima_app_logger/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AppLogger

Returns a new instance of AppLogger.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dima_app_logger/logger.rb', line 8

def initialize(options = {})
	self._options = {}
	self._options[:root] = options[:root]
	self._options[:env]  = options[:env]
	self._options[:name] = options[:name] || ENV["APP_LOGGER_NAME"]
	if defined?(Rails)
		self._options[:root] ||= Rails.root
		self._options[:env]  ||= Rails.env
	end
	self._options[:env]  ||= "development"
	self._options[:root] ||= Dir.pwd
	_expire!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



79
80
81
82
# File 'lib/dima_app_logger/logger.rb', line 79

def method_missing(method, *args, &block)
	_expire! if _expired?
	_logger.__send__(method, *args, &block)
end

Instance Attribute Details

#_optionsObject

Returns the value of attribute _options.



6
7
8
# File 'lib/dima_app_logger/logger.rb', line 6

def _options
  @_options
end

Instance Method Details

#_create_app_loggerObject



35
36
37
38
39
40
# File 'lib/dima_app_logger/logger.rb', line 35

def _create_app_logger
	name = _options[:env] + "." + (_options[:name] && "#{_options[:name]}.").to_s + _options[:pid].to_s + ".log"
	name = File.join(self._options[:root], "log", _date_string, name)
	_mkdir(name)
	AppLoggerInstance.new(name)
end

#_create_stdout_loggerObject



48
49
50
# File 'lib/dima_app_logger/logger.rb', line 48

def _create_stdout_logger
	AppStdoutLoggerInstance.new
end

#_create_test_loggerObject



42
43
44
45
46
# File 'lib/dima_app_logger/logger.rb', line 42

def _create_test_logger
	name = File.join(_options[:root], "log", "test.log")
	_mkdir(name)
	AppLoggerInstance.new(name)
end

#_date_stringObject



71
72
73
# File 'lib/dima_app_logger/logger.rb', line 71

def _date_string
	_options[:time].strftime("%Y%m%d")
end

#_expire!Object



62
63
64
65
66
67
68
69
# File 'lib/dima_app_logger/logger.rb', line 62

def _expire!
	self._options[:pid]  = Process.pid
	self._options[:time] = Time.now.utc
	if @logger
		@logger.close if @logger.respond_to?(:close)
		@logger = nil
	end
end

#_expired?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/dima_app_logger/logger.rb', line 52

def _expired?
	tm = Time.now.utc
	rs = _options[:pid]         != Process.pid || 
				_options[:time].day   != tm.day      ||
				_options[:time].month != tm.month    ||
				_options[:time].year  != tm.year
	tm = nil
	rs
end

#_loggerObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dima_app_logger/logger.rb', line 22

def _logger
	unless @logger
		@logger = if _options[:env] == "test"
								_create_test_logger
							elsif (ENV["_"] =~ /irb/ || ENV["APP_LOGGER_STDOUT"]) && !ENV["APP_LOGGER_NOSTDOUT"]
								_create_stdout_logger
							else
								_create_app_logger
							end
	end
	@logger
end

#_mkdir(path) ⇒ Object



75
76
77
# File 'lib/dima_app_logger/logger.rb', line 75

def _mkdir(path)
	FileUtils.mkdir_p(File.dirname(path))
end