Class: Loggerx::Loggerxcm0
- Inherits:
-
Object
- Object
- Loggerx::Loggerxcm0
- Defined in:
- lib/loggerx/loggerxcm.rb
Overview
The ‘Loggerx::Loggerxcm0` class is designed to provide logging functionality specific to the Mkspec framework. It encapsulates the logging mechanism, offering a unified interface for recording various levels of messages, such as debug, info, warning, and error. This class aims to facilitate debugging and tracking of the application’s flow by providing detailed and structured log messages. It can be configured to log messages to different outputs, including standard output, files, or external logging services, depending on the needs of the framework.
This class may also support log rotation, filtering of log messages based on severity, and formatting of log messages to include timestamps, source identifiers, and other relevant information.
Direct Known Subclasses
Class Method Summary collapse
- .close ⇒ Object
- .debug(value) ⇒ Object
- .ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix) ⇒ Object
- .error(value) ⇒ Object
- .error_sub(value) ⇒ Object
- .fatal(value) ⇒ Object
- .info(value) ⇒ Object
- .init(prefix, fname, log_dir, stdout_flag, level: :info) ⇒ Object
- .register_log_format(obj) ⇒ Object
- .register_log_level(log_level) ⇒ Object
- .setup_logger_file(log_file, log_dir, fname) ⇒ Object
- .setup_logger_stdout(log_stdout) ⇒ Object
- .show(value) ⇒ Object
- .to_string(value) ⇒ Object
- .valid? ⇒ Boolean
- .warn(value) ⇒ Object
Class Method Details
.close ⇒ Object
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/loggerx/loggerxcm.rb', line 213 def close retrun unless @valid @log_file&.close @log_file = nil @log_stdout = nil @valid = false # @log_stdout&.close end |
.debug(value) ⇒ Object
176 177 178 179 180 181 182 183 184 |
# File 'lib/loggerx/loggerxcm.rb', line 176 def debug(value) str = to_string(value) if @valid @log_file&.debug(str) @log_stdout&.debug(str) end true end |
.ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix) ⇒ Object
39 40 41 42 43 |
# File 'lib/loggerx/loggerxcm.rb', line 39 def ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix) list = log_dir_pn.children.select { |item| item.basename.to_s.match?("^#{prefix}") }.sort_by(&:mtime) latest_index = list.size - limit_of_num_of_files list[0, latest_index].map(&:unlink) if latest_index.positive? end |
.error(value) ⇒ Object
171 172 173 174 |
# File 'lib/loggerx/loggerxcm.rb', line 171 def error(value) error_sub(value) true end |
.error_sub(value) ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'lib/loggerx/loggerxcm.rb', line 162 def error_sub(value) str = to_string(value) if @valid @log_file&.error(str) @log_stdout&.error(str) end str end |
.fatal(value) ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/loggerx/loggerxcm.rb', line 204 def fatal(value) str = to_string(value) if @valid @log_file&.fatal(str) @log_stdout&.fatal(str) end true end |
.info(value) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/loggerx/loggerxcm.rb', line 186 def info(value) str = to_string(value) if @valid @log_file&.info(str) @log_stdout&.info(str) end true end |
.init(prefix, fname, log_dir, stdout_flag, level: :info) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/loggerx/loggerxcm.rb', line 45 def init(prefix, fname, log_dir, stdout_flag, level: :info) return if @log_file @error_count = 0 level_hs = { debug: Logger::DEBUG, info: Logger::INFO, warn: Logger::WARN, error: Logger::ERROR, fatal: Logger::FATAL, unknown: Logger::UNKNOWN } @log_level = level_hs[level] if @log_level.nil? p "level=#{level} level.class=#{level.class}" return unless @log_level end @log_dir_pn = Pathname.new(log_dir) @limit_of_num_of_files ||= 5 ensure_quantum_log_files(@log_dir_pn, @limit_of_num_of_files, prefix) @log_stdout = setup_logger_stdout(@log_stdout) if stdout_flag fname = nil if fname == false fname = prefix + LOG_FILENAME_BASE if fname == :default @log_file = setup_logger_file(@log_file, log_dir, fname) if fname obj = proc do |_, _, _, msg| "#{msg}\n" end register_log_format(obj) register_log_level(@log_level) @valid = true Loggerxcm.fatal('fatal') Loggerxcm.debug('debug') Loggerxcm.info('info') Loggerxcm.warn('warn') Loggerxcm.error('error') # Loggerxcm.unknown("unknown") end |
.register_log_format(obj) ⇒ Object
118 119 120 121 |
# File 'lib/loggerx/loggerxcm.rb', line 118 def register_log_format(obj) @log_file&.formatter = obj @log_stdout&.formatter = obj end |
.register_log_level(log_level) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/loggerx/loggerxcm.rb', line 123 def register_log_level(log_level) @log_file&.level = log_level @log_stdout&.level = log_level # # Log4r互換インターフェイス # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN end |
.setup_logger_file(log_file, log_dir, fname) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/loggerx/loggerxcm.rb', line 103 def setup_logger_file(log_file, log_dir, fname) filepath = Pathname.new(log_dir).join(fname) if log_file.nil? begin log_file = Logger.new(filepath.to_s) rescue Errno::EACCES @error_count += 1 rescue StandardError => exc puts exc @error_count += 1 end end log_file end |
.setup_logger_stdout(log_stdout) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/loggerx/loggerxcm.rb', line 91 def setup_logger_stdout(log_stdout) return log_stdout unless log_stdout.nil? begin log_stdout = Logger.new($stdout) rescue StandardError => exc pust exc. @error_count += 1 end log_stdout end |
.show(value) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/loggerx/loggerxcm.rb', line 146 def show(value) if @valid if value.instance_of?(Array) value.map do |v| str = v.to_s Util.puts_valid_str(str) end else Util.puts_valid_str(str) end str = error_sub(value) Util.puts_valid_str(str) end true end |
.to_string(value) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/loggerx/loggerxcm.rb', line 131 def to_string(value) if value.instance_of?(Array) @stdout_backup ||= $stdout @stringio ||= StringIO.new(+'', 'w+') $stdout = @stringio @stringio.rewind str = @stringio.read @stringio.truncate(0) $stdout = @stdout_backup str else value end end |
.valid? ⇒ Boolean
35 36 37 |
# File 'lib/loggerx/loggerxcm.rb', line 35 def valid? @valid end |
.warn(value) ⇒ Object
195 196 197 198 199 200 201 202 |
# File 'lib/loggerx/loggerxcm.rb', line 195 def warn(value) str = to_string(value) if @valid @log_file&.warn(str) @log_stdout&.warn(str) end true end |