Module: Narou::LoggerModule

Included in:
Logger, LoggerError, NullIO, StreamingLogger
Defined in:
lib/narou_logger.rb

Constant Summary collapse

LOG_FORMAT_FILENAME =
"%Y%m%d.txt"
LOG_FORMAT_TIMESTAMP =
"[%H:%M:%S]"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#capturingObject

Returns the value of attribute capturing.



24
25
26
# File 'lib/narou_logger.rb', line 24

def capturing
  @capturing
end

#format_filenameObject (readonly)

Returns the value of attribute format_filename.



25
26
27
# File 'lib/narou_logger.rb', line 25

def format_filename
  @format_filename
end

#format_timestampObject (readonly)

Returns the value of attribute format_timestamp.



25
26
27
# File 'lib/narou_logger.rb', line 25

def format_timestamp
  @format_timestamp
end

#format_timestamp_disabledObject (readonly)

Returns the value of attribute format_timestamp_disabled.



25
26
27
# File 'lib/narou_logger.rb', line 25

def format_timestamp_disabled
  @format_timestamp_disabled
end

#log_postfixObject

Returns the value of attribute log_postfix.



24
25
26
# File 'lib/narou_logger.rb', line 24

def log_postfix
  @log_postfix
end

#logging_enabledObject (readonly)

Returns the value of attribute logging_enabled.



25
26
27
# File 'lib/narou_logger.rb', line 25

def logging_enabled
  @logging_enabled
end

#streamObject

Returns the value of attribute stream.



24
25
26
# File 'lib/narou_logger.rb', line 24

def stream
  @stream
end

Class Method Details

.included(klass) ⇒ Object



27
28
29
30
31
# File 'lib/narou_logger.rb', line 27

def self.included(klass)
  klass.class_eval do
    alias_method :original_write, :write
  end
end

Instance Method Details

#append_log(str) ⇒ Object



164
165
166
167
# File 'lib/narou_logger.rb', line 164

def append_log(str)
  return unless logging?
  File.write(log_filepath, strip_color(embed_timestamp(str)), mode: "a")
end

#capture(options = {}, &block) ⇒ Object

標準出力($stdout)のバッファリング+取得

キャプチャー用途なので標準エラーはキャプチャーしない

quiet

標準出力に出力をしないかどうか

ansicolor_strip

エスケープシーケンスを除去するか



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/narou_logger.rb', line 88

def capture(options = {}, &block)
  options = {
    quiet: true, ansicolor_strip: true
  }.merge(options)
  raise "#capture block given" unless block
  temp_stream = $stdout
  $stdout = (self == $stdout ? copy_instance : self)
  $stdout.capturing = true
  if options[:quiet]
    $stdout.silence { yield }
  else
    yield
  end
  $stdout.capturing = false
  buffer = $stdout.string
  $stdout = temp_stream
  result = options[:ansicolor_strip] ? strip_color(buffer) : buffer
  if $stdout.capturing && !options[:quiet]
    # 多段キャプチャ中かつ quiet: false の場合は外側にも伝播する
    $stdout.string << result
  end
  result
end

#copy_instanceObject



53
54
55
56
57
# File 'lib/narou_logger.rb', line 53

def copy_instance
  self.class.new.tap do |obj|
    obj.silent = silent?
  end
end

#create_log_dirObject



154
155
156
157
158
# File 'lib/narou_logger.rb', line 154

def create_log_dir
  return unless logging?
  dir = Narou.log_dir
  dir.mkdir unless dir.exist?
end

#disable_loggingObject



160
161
162
# File 'lib/narou_logger.rb', line 160

def disable_logging
  @logging_enabled = false
end

#dup_with_disabled_loggingObject



59
60
61
62
63
# File 'lib/narou_logger.rb', line 59

def dup_with_disabled_logging
  obj = dup
  obj.disable_logging
  obj
end

#embed_timestamp(str) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/narou_logger.rb', line 181

def embed_timestamp(str)
  unless @before_head_ln
    str = "\n#{str}"
    @before_head_ln = true
  end
  if str.end_with?("\n")
    str = str.sub(/\n\z/, "")
    @before_head_ln = false
  end
  return str if format_timestamp_disabled
  str.gsub("\n", "\n#{Time.now.strftime(format_timestamp)} ")
end

#error(str) ⇒ Object



146
147
148
# File 'lib/narou_logger.rb', line 146

def error(str)
  self.puts "<bold><red>[ERROR]</red></bold> ".termcolor + str
end

#init_logsObject



43
44
45
46
47
48
49
50
51
# File 'lib/narou_logger.rb', line 43

def init_logs
  inv = Inventory.load("local_setting")
  inv_logging = inv.group("logging")
  @logging_enabled = inv["logging"]
  @format_filename = inv_logging.format_filename || LOG_FORMAT_FILENAME
  @format_timestamp = inv_logging.format_timestamp || LOG_FORMAT_TIMESTAMP
  @format_timestamp_disabled = format_timestamp.blank? || format_timestamp.strip == "$none"
  create_log_dir
end

#initializeObject



36
37
38
39
40
41
# File 'lib/narou_logger.rb', line 36

def initialize
  super
  self.silent = false
  @capturing = false
  init_logs
end

#log_filenameObject



173
174
175
176
177
178
179
# File 'lib/narou_logger.rb', line 173

def log_filename
  name = Time.now.strftime(format_filename)
  return name unless log_postfix
  ext = File.extname(name)
  basename = File.basename(name, ext)
  "#{basename}#{log_postfix}#{ext}"
end

#log_filepathObject



169
170
171
# File 'lib/narou_logger.rb', line 169

def log_filepath
  Narou.log_dir.join(log_filename)
end

#logging?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/narou_logger.rb', line 150

def logging?
  logging_enabled && ENV["NAROU_ENV"] != "test"
end

#save(path) ⇒ Object



120
121
122
# File 'lib/narou_logger.rb', line 120

def save(path)
  File.write(path, strip_color(string))
end

#silence(&block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/narou_logger.rb', line 73

def silence(&block)
  raise "need a block" unless block
  tmp = self.silent?
  self.silent = true
  yield
  self.silent = tmp
end

#silent=(enable) ⇒ Object



65
66
67
# File 'lib/narou_logger.rb', line 65

def silent=(enable)
  @silent = enable.present?
end

#silent?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/narou_logger.rb', line 69

def silent?
  @silent
end

#strip_color(str) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/narou_logger.rb', line 112

def strip_color(str)
  if $disable_color
    str
  else
    str.gsub(/(?:\e\[\d*[a-zA-Z])+/, "")
  end
end

#warn(str) ⇒ Object



142
143
144
# File 'lib/narou_logger.rb', line 142

def warn(str)
  self.puts str
end

#write_base(str, stream, force_disable_logging = false) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/narou_logger.rb', line 133

def write_base(str, stream, force_disable_logging = false)
  str = str.to_s
  if str.encoding == Encoding::ASCII_8BIT
    str.force_encoding(Encoding::UTF_8)
  end
  write_console(str, stream)
  append_log(str) unless force_disable_logging
end

#write_console(str, target) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/narou_logger.rb', line 124

def write_console(str, target)
  return if silent?
  if $disable_color
    target.write(str)
  else
    write_color(str, target)
  end
end