Module: Bitcoin::Logger

Defined in:
lib/bitcoin/logger.rb

Overview

Simple Logger module

Constant Summary collapse

Format =
"%s, [%s#%d #%d] %5s -- %s: %s\n".freeze

Class Method Summary collapse

Class Method Details

.create(name, level = ::Logger::INFO) ⇒ Object

Create a logger with given name.log in $HOME/.bitcoinrb/log.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bitcoin/logger.rb', line 13

def create(name, level = ::Logger::INFO)
  dir = "#{Bitcoin.base_dir}/log"
  FileUtils.mkdir_p(dir)
  logger = ::Logger.new(dir + "/#{name}.log", 10)
  logger.level = level
  logger.formatter =  proc do |severity, datetime, progname, msg|
    Format % [severity[0..0], format_datetime(datetime), $$,
              Thread.current.object_id, severity, progname, msg2str(msg)]
  end
  logger
end

.format_datetime(time) ⇒ Object



37
38
39
# File 'lib/bitcoin/logger.rb', line 37

def format_datetime(time)
  time.strftime(@datetime_format || "%Y-%m-%dT%H:%M:%S.%6N ".freeze)
end

.msg2str(msg) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bitcoin/logger.rb', line 25

def self.msg2str(msg)
  case msg
    when ::String
      msg
    when ::Exception
      "#{ msg.message } (#{ msg.class })\n" <<
          (msg.backtrace || []).join("\n")
    else
      msg.inspect
  end
end