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
-
#capturing ⇒ Object
Returns the value of attribute capturing.
-
#format_filename ⇒ Object
readonly
Returns the value of attribute format_filename.
-
#format_timestamp ⇒ Object
readonly
Returns the value of attribute format_timestamp.
-
#format_timestamp_disabled ⇒ Object
readonly
Returns the value of attribute format_timestamp_disabled.
-
#log_postfix ⇒ Object
Returns the value of attribute log_postfix.
-
#logging_enabled ⇒ Object
readonly
Returns the value of attribute logging_enabled.
-
#stream ⇒ Object
Returns the value of attribute stream.
Class Method Summary collapse
Instance Method Summary collapse
- #append_log(str) ⇒ Object
-
#capture(options = {}, &block) ⇒ Object
標準出力($stdout)のバッファリング+取得.
- #copy_instance ⇒ Object
- #create_log_dir ⇒ Object
- #disable_logging ⇒ Object
- #dup_with_disabled_logging ⇒ Object
- #embed_timestamp(str) ⇒ Object
- #error(str) ⇒ Object
- #init_logs ⇒ Object
- #initialize ⇒ Object
- #log_filename ⇒ Object
- #log_filepath ⇒ Object
- #logging? ⇒ Boolean
- #save(path) ⇒ Object
- #silence(&block) ⇒ Object
- #silent=(enable) ⇒ Object
- #silent? ⇒ Boolean
- #strip_color(str) ⇒ Object
- #warn(str) ⇒ Object
- #write_base(str, stream, force_disable_logging = false) ⇒ Object
- #write_console(str, target) ⇒ Object
Instance Attribute Details
#capturing ⇒ Object
Returns the value of attribute capturing.
24 25 26 |
# File 'lib/narou_logger.rb', line 24 def capturing @capturing end |
#format_filename ⇒ Object (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_timestamp ⇒ Object (readonly)
Returns the value of attribute format_timestamp.
25 26 27 |
# File 'lib/narou_logger.rb', line 25 def @format_timestamp end |
#format_timestamp_disabled ⇒ Object (readonly)
Returns the value of attribute format_timestamp_disabled.
25 26 27 |
# File 'lib/narou_logger.rb', line 25 def @format_timestamp_disabled end |
#log_postfix ⇒ Object
Returns the value of attribute log_postfix.
24 25 26 |
# File 'lib/narou_logger.rb', line 24 def log_postfix @log_postfix end |
#logging_enabled ⇒ Object (readonly)
Returns the value of attribute logging_enabled.
25 26 27 |
# File 'lib/narou_logger.rb', line 25 def logging_enabled @logging_enabled end |
#stream ⇒ Object
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((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( = {}, &block) = { quiet: true, ansicolor_strip: true }.merge() raise "#capture block given" unless block temp_stream = $stdout $stdout = (self == $stdout ? copy_instance : self) $stdout.capturing = true if [:quiet] $stdout.silence { yield } else yield end $stdout.capturing = false buffer = $stdout.string $stdout = temp_stream result = [:ansicolor_strip] ? strip_color(buffer) : buffer if $stdout.capturing && ![:quiet] # 多段キャプチャ中かつ quiet: false の場合は外側にも伝播する $stdout.string << result end result end |
#copy_instance ⇒ Object
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_dir ⇒ Object
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_logging ⇒ Object
160 161 162 |
# File 'lib/narou_logger.rb', line 160 def disable_logging @logging_enabled = false end |
#dup_with_disabled_logging ⇒ Object
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 (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 str.gsub("\n", "\n#{Time.now.strftime()} ") 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_logs ⇒ Object
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. || LOG_FORMAT_TIMESTAMP @format_timestamp_disabled = .blank? || .strip == "$none" create_log_dir end |
#initialize ⇒ Object
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_filename ⇒ Object
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_filepath ⇒ Object
169 170 171 |
# File 'lib/narou_logger.rb', line 169 def log_filepath Narou.log_dir.join(log_filename) end |
#logging? ⇒ 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
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 |