Class: LiooooUtilsLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/lioooo_log.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, log_dir = nil) ⇒ LiooooUtilsLogger

Returns a new instance of LiooooUtilsLogger.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lioooo_log.rb', line 10

def initialize(type, log_dir=nil)
  @log_dir = log_dir.nil? ? LogRoot : log_dir
  init_log_dir
  path = File.expand_path("#{log_dir}/#{type}_log.log", __FILE__)
  @logger = Logging.logger["Lioooo#{type.upcase}Log"]
  @logger.add_appenders(
      Logging.appenders.stdout,
      Logging.appenders.file(path)
  )
  logger.level = :debug
end

Instance Attribute Details

#log_dirObject

Returns the value of attribute log_dir.



8
9
10
# File 'lib/lioooo_log.rb', line 8

def log_dir
  @log_dir
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/lioooo_log.rb', line 8

def logger
  @logger
end

Instance Method Details

#debug(msg) ⇒ Object



57
58
59
60
# File 'lib/lioooo_log.rb', line 57

def debug msg
  msg = "[#{now}] #{msg}"
  @logger.debug msg.yellow
end

#error(msg) ⇒ Object

puts log with error prefix



37
38
39
40
# File 'lib/lioooo_log.rb', line 37

def error(msg)
  msg = "[#{now}] #{msg}"
  @logger.error msg.red
end

#info(msg) ⇒ Object

puts log with info prefix



47
48
49
50
# File 'lib/lioooo_log.rb', line 47

def info(msg)
  msg = "[#{now}] #{msg}"
  @logger.info msg
end

#init_log_dirObject

liooo_link_log_dir require log directory



31
32
33
34
# File 'lib/lioooo_log.rb', line 31

def init_log_dir
  cmd = "mkdir -p #{log_dir}"
  system cmd unless File.exist? log_dir
end

#nowObject



42
43
44
# File 'lib/lioooo_log.rb', line 42

def now
  LiooooUtils::TimeUtils.now_y_m_d_s_m_s
end

#prefix(fix) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/lioooo_log.rb', line 22

def prefix(fix)
  path = File.expand_path("#{LogRoot}/#{fix}_log.log", __FILE__)
  @logger.add_appenders(
      Logging.appenders.stdout,
      Logging.appenders.file(path)
  )
end

#real_time(&block) ⇒ Object

print block exec time



80
81
82
83
84
85
86
87
# File 'lib/lioooo_log.rb', line 80

def real_time(&block)
  if block_given?
    elapsed = Benchmark.realtime do
      yield
    end
    warn "执行完毕: #{(elapsed).round(2)} 秒!"
  end
end

#shell(msg) ⇒ Object



62
63
64
65
# File 'lib/lioooo_log.rb', line 62

def shell(msg)
  msg = "\n [#{now}] #{msg}"
  @logger.debug msg.white_on_black
end

#shell_exec!(cmd) ⇒ Object

———————for call shell command—————-



74
75
76
77
# File 'lib/lioooo_log.rb', line 74

def shell_exec!(cmd)
  shell cmd
  system "source ~/.bash_profile && #{cmd}"
end

#splitObject

formate log timestamp



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

def split
  msg = Time.now.strftime("%Y-%m-%d [ %H:%M:%S ]").to_s.center 100, '-'
  @logger.info msg.blue
end

#warn(msg) ⇒ Object



52
53
54
55
# File 'lib/lioooo_log.rb', line 52

def warn(msg)
  msg = "[#{now}] #{msg}"
  @logger.warn msg.magenta
end