Class: Jinx::Log
Overview
Wraps a standard global Logger.
Constant Summary collapse
- FORMAT =
Stream-lined log format.
%{%s [%s] %5s %s\n}
- LINUX_LOG_DIR =
The default log file.
'/var/log'
Instance Method Summary collapse
-
#close ⇒ Object
Closes and releases the #logger.
-
#default_linux_log_file(app) ⇒ String?
private
The default file name.
-
#default_log_file(app = nil) ⇒ String
private
Returns the log file, as described in #open.
-
#default_windows_log_file(app) ⇒ String?
private
The default file name.
-
#file ⇒ String?
The log file, or nil if the log was opened on an IO rather than a String.
-
#logger ⇒ MultilineLogger
The global logger.
-
#open(dev = nil, opts = nil) ⇒ MultilineLogger
Opens the log.
-
#open? ⇒ Boolean
Whether the logger is open.
Instance Method Details
#close ⇒ Object
Closes and releases the #logger.
97 98 99 100 |
# File 'lib/jinx/helpers/log.rb', line 97 def close @logger.close @logger = nil end |
#default_linux_log_file(app) ⇒ String? (private)
Returns the default file name.
138 139 140 141 142 143 144 |
# File 'lib/jinx/helpers/log.rb', line 138 def default_linux_log_file(app) return unless File.exists?(LINUX_LOG_DIR) base = app.underscore.gsub(' ', '_') file = File.("#{base}.log", LINUX_LOG_DIR) log = file if File.exists?(file) ? File.writable?(file) : File.writable?(LINUX_LOG_DIR) log || '/dev/null' end |
#default_log_file(app = nil) ⇒ String (private)
Returns the log file, as described in #open.
If the standard Linux log location exists, then try that. Otherwise, try the conventional Windows app data location. If all else fails, use the working directory.
The file must be creatable or writable.
131 132 133 134 |
# File 'lib/jinx/helpers/log.rb', line 131 def default_log_file(app=nil) app ||= 'Jinx' default_linux_log_file(app) || default_windows_log_file(app) || "log/#{app}.log" end |
#default_windows_log_file(app) ⇒ String? (private)
Returns the default file name.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/jinx/helpers/log.rb', line 148 def default_windows_log_file(app) # the conventional Windows app data location app_dir = ENV['LOCALAPPDATA'] || return dir = app_dir + "/#{app}/log" file = File.("#{app}.log", dir) if File.exists?(file) ? File.writable?(file) : (File.directory?(dir) ? File.writable?(dir) : File.writable?(app_dir)) then file else 'NUL' end end |
#file ⇒ String?
Returns the log file, or nil if the log was opened on an IO rather than a String.
109 110 111 |
# File 'lib/jinx/helpers/log.rb', line 109 def file @dev if String === @dev end |
#logger ⇒ MultilineLogger
Returns the global logger.
103 104 105 |
# File 'lib/jinx/helpers/log.rb', line 103 def logger @logger ||= open end |
#open(dev = nil, opts = nil) ⇒ MultilineLogger
Opens the log. The default log location is determined from the application name. The application name is the value of the :app
option, or Jinx
by default. For an application MyApp
, the log location is determined as follows:
-
/var/log/my_app.log
for Linux -
%LOCALAPPDATA%MyApplogMyApp.log for Windows
-
./log/MyApp.log
otherwise
The default file must be creatable or writable. If the device argument is not provided and there is no suitable default log file, then logging is disabled.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/jinx/helpers/log.rb', line 68 def open(dev=nil, opts=nil) if open? then raise RuntimeError.new("The logger has already opened the log#{' file ' + @dev if String === @dev}") end dev, opts = nil, dev if Hash === dev dev ||= default_log_file(Options.get(:app, opts)) FileUtils.mkdir_p(File.dirname(dev)) if String === dev # default is 4-file rotation @ 16MB each shift_age = Options.get(:shift_age, opts, 4) shift_size = Options.get(:shift_size, opts, 16 * 1048576) @logger = MultilineLogger.new(dev, shift_age, shift_size) @logger.level = Options.get(:debug, opts) ? Logger::DEBUG : Logger::INFO @logger.formatter = lambda do |severity, time, progname, msg| FORMAT % [ progname || 'I', DateTime.now.strftime("%d/%b/%Y %H:%M:%S"), severity, msg] end @dev = dev @logger end |
#open? ⇒ Boolean
Returns whether the logger is open.
92 93 94 |
# File 'lib/jinx/helpers/log.rb', line 92 def open? !!@logger end |