Class: D3::Log
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
-
#level ⇒ Symbol
The current severity level of logging.
-
#log_file ⇒ Pathname
readonly
The logfile being written.
-
#progname ⇒ String
The program name associated with a log entry.
-
#timestamp_format ⇒ String
The strftime format of the timestamps in the log.
Class Method Summary collapse
-
.check_level(level) ⇒ integer
Return the numeric value of a log level or raise an exception if the level is invalid.
Instance Method Summary collapse
-
#initialize ⇒ Log
constructor
Constructor #################.
-
#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.
Constructor Details
#initialize ⇒ Log
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. if D3::CONFIG. # 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
#level ⇒ Symbol
Returns the current severity level of logging.
148 149 150 |
# File 'lib/d3/log.rb', line 148 def level @level end |
#log_file ⇒ Pathname (readonly)
Returns the logfile being written.
145 146 147 |
# File 'lib/d3/log.rb', line 145 def log_file @log_file end |
#progname ⇒ String
Returns the program name associated with a log entry.
154 155 156 |
# File 'lib/d3/log.rb', line 154 def progname @progname end |
#timestamp_format ⇒ String
Returns the strftime format of the timestamps in the log.
151 152 153 |
# File 'lib/d3/log.rb', line 151 def @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.
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.
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 |