Class: Loggerx::Loggerxcm0

Inherits:
Object
  • Object
show all
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.

Examples:

Logging a debug message

Loggerx::Loggerxcm0.debug("This is a debug message")

Logging an error message

Loggerx::Loggerxcm0.error("This is an error message")

Direct Known Subclasses

Loggerxcm

Class Method Summary collapse

Class Method Details

.closeObject



208
209
210
211
212
213
214
215
216
217
# File 'lib/loggerx/loggerxcm.rb', line 208

def close
  retrun unless @valid

  @log_file&.close
  @log_file = nil
  @log_stdout = nil

  @valid = false
  # @log_stdout&.close
end

.debug(value) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/loggerx/loggerxcm.rb', line 171

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



166
167
168
169
# File 'lib/loggerx/loggerxcm.rb', line 166

def error(value)
  error_sub(value)
  true
end

.error_sub(value) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/loggerx/loggerxcm.rb', line 157

def error_sub(value)
  str = to_string(value)
  if @valid
    @log_file&.error(str)
    @log_stdout&.error(str)
  end
  str
end

.fatal(value) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/loggerx/loggerxcm.rb', line 199

def fatal(value)
  str = to_string(value)
  if @valid
    @log_file&.fatal(str)
    @log_stdout&.fatal(str)
  end
  true
end

.info(value) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/loggerx/loggerxcm.rb', line 181

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
# 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]
  @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



113
114
115
116
# File 'lib/loggerx/loggerxcm.rb', line 113

def register_log_format(obj)
  @log_file&.formatter = obj
  @log_stdout&.formatter = obj
end

.register_log_level(log_level) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/loggerx/loggerxcm.rb', line 118

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/loggerx/loggerxcm.rb', line 98

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



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/loggerx/loggerxcm.rb', line 86

def setup_logger_stdout(log_stdout)
  return log_stdout unless log_stdout.nil?

  begin
    log_stdout = Logger.new($stdout)
  rescue StandardError => exc
    pust exc.message
    @error_count += 1
  end
  log_stdout
end

.show(value) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/loggerx/loggerxcm.rb', line 141

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



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

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

Returns:

  • (Boolean)


35
36
37
# File 'lib/loggerx/loggerxcm.rb', line 35

def valid?
  @valid
end

.warn(value) ⇒ Object



190
191
192
193
194
195
196
197
# File 'lib/loggerx/loggerxcm.rb', line 190

def warn(value)
  str = to_string(value)
  if @valid
    @log_file&.warn(str)
    @log_stdout&.warn(str)
  end
  true
end