Class: Baykit::BayServer::BayLog

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/baykit/bayserver/bay_log.rb

Constant Summary collapse

LOG_LEVEL_TRACE =
0
LOG_LEVEL_DEBUG =
1
LOG_LEVEL_INFO =
2
LOG_LEVEL_WARN =
3
LOG_LEVEL_ERROR =
4
LOG_LEVEL_FATAL =
5
LOG_LEVEL_NAME =
["TRACE", "DEBUG", "INFO ", "WARN ", "ERROR", "FATAL"]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.log_levelObject (readonly)

Returns the value of attribute log_level.



18
19
20
# File 'lib/baykit/bayserver/bay_log.rb', line 18

def log_level
  @log_level
end

Class Method Details

.debug(fmt, *args) ⇒ Object



49
50
51
# File 'lib/baykit/bayserver/bay_log.rb', line 49

def self.debug(fmt, *args)
  log(LOG_LEVEL_DEBUG, 3, nil, fmt, args)
end

.debug_e(err, fmt = nil, *args) ⇒ Object



53
54
55
# File 'lib/baykit/bayserver/bay_log.rb', line 53

def self.debug_e(err, fmt=nil, *args)
  log(LOG_LEVEL_DEBUG, 3, err, fmt, args)
end

.debug_mode?Boolean

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/baykit/bayserver/bay_log.rb', line 121

def self.debug_mode?
  @log_level <= LOG_LEVEL_DEBUG
  #LOG_LEVEL_TRACE < LOG_LEVEL_INFO
end

.error(fmt, *args) ⇒ Object



65
66
67
# File 'lib/baykit/bayserver/bay_log.rb', line 65

def self.error(fmt, *args)
  log(LOG_LEVEL_ERROR, 3, nil , fmt, args)
end

.error_e(err, fmt = nil, *args) ⇒ Object



69
70
71
# File 'lib/baykit/bayserver/bay_log.rb', line 69

def self.error_e(err, fmt=nil, *args)
  log(LOG_LEVEL_ERROR, 3, err, fmt, args)
end

.fatal(fmt, *args) ⇒ Object



73
74
75
# File 'lib/baykit/bayserver/bay_log.rb', line 73

def self.fatal(fmt, *args)
  log(LOG_LEVEL_FATAL, 3, nil , fmt, args)
end

.fatal_e(err, fmt = nil, *args) ⇒ Object



77
78
79
80
# File 'lib/baykit/bayserver/bay_log.rb', line 77

def self.fatal_e(err, fmt=nil, *args)
  log(LOG_LEVEL_FATAL, 3, err, fmt, args)
  exit(1)
end

.info(fmt, *args) ⇒ Object



41
42
43
# File 'lib/baykit/bayserver/bay_log.rb', line 41

def self.info(fmt, *args)
  log(LOG_LEVEL_INFO, 3, nil, fmt, args)
end

.log(lvl, stack_idx, err, fmt, args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/baykit/bayserver/bay_log.rb', line 82

def self.log(lvl, stack_idx, err, fmt, args)
  #pos = caller[1].split("/")[-1]
  apos = parse_caller(caller[1])
  if(!@full_path)
    apos[0] = File.basename(apos[0])
  end
  pos = "#{apos[0]}:#{apos[1]}"
  #pos = caller[1]

  if fmt != nil
    if lvl >= @log_level
      begin
        if args == nil || args.length == 0
          msg = sprintf("%s", fmt)
        else
          msg = sprintf(fmt, *args)
        end
      rescue => e
        puts(e.class)
        puts(e.message + " " + pos)
        print_exception(e)
        msg = fmt
      end

      print("[#{Time.now}] #{LOG_LEVEL_NAME[lvl]}. #{msg} (at #{pos})\n")
    end
  end

  if err != nil
    if debug_mode? || lvl == LOG_LEVEL_FATAL
      puts(err.class)
      puts(err.message + " " + pos)
      print_exception err
    else
      log(lvl, stack_idx + 1, nil, "%s", err.message)
    end
  end
end


130
131
132
133
134
135
136
137
138
139
# File 'lib/baykit/bayserver/bay_log.rb', line 130

def self.print_exception err
  for s in err.backtrace
    puts "\t" + s
  end
  if err.cause
    puts "Caused by:"
    puts err.cause.message
    print_exception err.cause
  end
end

.set_log_level(lvl) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/baykit/bayserver/bay_log.rb', line 23

def self.set_log_level(lvl)
  if lvl.casecmp? "trace"
    @log_level = LOG_LEVEL_TRACE
  elsif lvl.casecmp? "debug"
    @log_level = LOG_LEVEL_DEBUG
  elsif lvl.casecmp? "info"
    @log_level = LOG_LEVEL_INFO
  elsif lvl.casecmp? "warn"
    @log_level = LOG_LEVEL_WARN
  elsif lvl.casecmp? "error"
    @log_level = LOG_LEVEL_ERROR
  elsif lvl.casecmp? "fatal"
    @log_level = LOG_LEVEL_FATAL
  else
    warn(BayMessage.get(:INT_UNKNOWN_LOG_LEVEL, lvl))
  end
end

.trace(fmt, *args) ⇒ Object



45
46
47
# File 'lib/baykit/bayserver/bay_log.rb', line 45

def self.trace(fmt, *args)
  log(LOG_LEVEL_TRACE, 3, nil , fmt, args)
end

.trace_mode?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/baykit/bayserver/bay_log.rb', line 126

def self.trace_mode?
  @log_level == LOG_LEVEL_TRACE
end

.warn(fmt, *args) ⇒ Object



57
58
59
# File 'lib/baykit/bayserver/bay_log.rb', line 57

def self.warn(fmt, *args)
  log(LOG_LEVEL_WARN, 3, nil, fmt, args)
end

.warn_e(err, fmt = nil, *args) ⇒ Object



61
62
63
# File 'lib/baykit/bayserver/bay_log.rb', line 61

def self.warn_e(err, fmt=nil, *args)
  log(LOG_LEVEL_WARN, 3, err, fmt, args)
end