Class: D3::Log

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/d3/log.rb

Constant Summary collapse

DFT_LOG_FILE =

The default log file

"/var/log/d3.log"
LOG_LEVELS =

the possible log severity levels

{
  :debug => Logger::DEBUG,
  :info => Logger::INFO,
  :warn => Logger::WARN,
  :error => Logger::ERROR,
  :fatal => Logger::FATAL
}
DFT_LOG_LEVEL =

the default log level

LOG_LEVELS[:info]
DFT_VERBOSITY =

the default verbosity level (logs to stderr)

LOG_LEVELS[:warn]
DFT_LOG_PROGNAME =

THe “program name” that appears in the logs. see #progname=

"d3-module"
DFT_LOG_TIME_FMT =

timestamp format, this is 2015-02-15 13:02:34

"%Y-%m-%d %H:%M:%S"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

Constructor #################



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/d3/log.rb', line 158

def initialize

  # Set default values
  @log_file = Pathname.new DFT_LOG_FILE
  @level = DFT_LOG_LEVEL
  @timestamp_format = DFT_LOG_TIME_FMT
  @progname = DFT_LOG_PROGNAME


  # Set values from config if available.
  # @note: progname needs to be set by the prog using the module,
  #   e.g. d3, d3admin, d3helper
  #   not by the config
  @log_file = Pathname.new(D3::CONFIG.log_file) if D3::CONFIG.log_file
  @level = D3::CONFIG.log_level if D3::CONFIG.log_level
  @timestamp_format = D3::CONFIG.log_timestamp_format if D3::CONFIG.log_timestamp_format

  # the logger will be created if the file is writable
  writable =  if @log_file.file?
                @log_file.writable?
              else
                @log_file.parent.writable?
              end

  if writable
    @logger = Logger.new @log_file
    @logger.level = D3::Log.check_level(@level)
    set_format
  else
    @logger = nil
  end

end

Instance Attribute Details

#levelSymbol

Returns the current severity level of logging.

Returns:

  • (Symbol)

    the current severity level of logging



148
149
150
# File 'lib/d3/log.rb', line 148

def level
  @level
end

#log_filePathname (readonly)

Returns the logfile being written.

Returns:

  • (Pathname)

    the logfile being written



145
146
147
# File 'lib/d3/log.rb', line 145

def log_file
  @log_file
end

#prognameString

Returns the program name associated with a log entry.

Returns:

  • (String)

    the program name associated with a log entry



154
155
156
# File 'lib/d3/log.rb', line 154

def progname
  @progname
end

#timestamp_formatString

Returns the strftime format of the timestamps in the log.

Returns:

  • (String)

    the strftime format of the timestamps in the log



151
152
153
# File 'lib/d3/log.rb', line 151

def timestamp_format
  @timestamp_format
end

Class Method Details

.check_level(level) ⇒ integer

Return the numeric value of a log level or raise an exception if the level is invalid.

Parameters:

  • level (Symbol, Integer)

    the level to check, should be one of the keys of LOG_LEVELS

Returns:

  • (integer)

    the numeric value of the log level

Raises:

  • (JSS::InvalidDataError)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/d3/log.rb', line 126

def self.check_level(level)
  valid = true
  case level
  when Symbol
    valid = false unless LOG_LEVELS.keys.include? level
    value = LOG_LEVELS[level]
  when Fixnum
    valid = false unless LOG_LEVELS.values.include? level
    value = level
  else
    valid = false
  end #case
  raise JSS::InvalidDataError, "Severity level must be one of :#{LOG_LEVELS.keys.join ', :'} OR #{LOG_LEVELS.values.join ', '}" unless valid
  return value
end

Instance Method Details

#log(msg, severity = DFT_LOG_LEVEL) ⇒ Boolean

Send a message to be logged If the severity is less severe than the current level, the message won’t be written to the log.

Parameters:

  • msg (String)

    the message to write to the log

  • severity (Symbol) (defaults to: DFT_LOG_LEVEL)

    the severity of this message. If below the current log_level the message won’t be written. Must be one of the keys of LOG_LEVELS. Defaults to :info.

  • progname (String)

    the name of the program creating this msg. Defaults to the currently-set log_progname (see #log_progname=) or DFT_LOG_PROGNAME.

Returns:

  • (Boolean)

    the message was handled appropriately, or not



210
211
212
213
# File 'lib/d3/log.rb', line 210

def log (msg, severity = DFT_LOG_LEVEL)
  return nil unless @logger
  @logger.add(D3::Log.check_level(severity), msg, @progname)
end