Class: Gcloud::Logging::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/logging/logger.rb

Overview

Logger

A (mostly) API compatible logger for ruby's Logger.

Instance Method Summary collapse

Instance Method Details

#add(severity, message = nil, progname = nil) ⇒ Object Also known as: log

Log a message if the given severity is high enough. This is the generic logging method. Users will be more inclined to use #debug, #info, #warn, #error, and #fatal.


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gcloud/logging/logger.rb', line 114

def add severity, message = nil, progname = nil
  severity = derive_severity(severity) || 5 # 5 is UNKNOWN/DEFAULT
  return true if severity < @level

  if message.nil?
    if block_given?
      message = yield
    else
      message = progname
      # progname = nil # TODO: Figure out what to do with the progname
    end
  end

  write_entry severity, message
end

#debug(message = nil, &block) ⇒ Object

Log a DEBUG message.


51
52
53
54
55
56
57
# File 'lib/gcloud/logging/logger.rb', line 51

def debug message = nil, &block
  if block_given?
    add 0, nil, message, &block
  else
    add 0, message, nil, &block
  end
end

#debug?Boolean

Returns true if the current severity level allows for sending DEBUG messages.

Returns:

  • (Boolean)

134
135
136
# File 'lib/gcloud/logging/logger.rb', line 134

def debug?
  @level <= 0
end

#error(message = nil, &block) ⇒ Object

Log an ERROR message.


81
82
83
84
85
86
87
# File 'lib/gcloud/logging/logger.rb', line 81

def error message = nil, &block
  if block_given?
    add 3, nil, message, &block
  else
    add 3, message, nil, &block
  end
end

#error?Boolean

Returns true if the current severity level allows for sending ERROR messages.

Returns:

  • (Boolean)

155
156
157
# File 'lib/gcloud/logging/logger.rb', line 155

def error?
  @level <= 3
end

#fatal(message = nil, &block) ⇒ Object

Log a FATAL message.


91
92
93
94
95
96
97
# File 'lib/gcloud/logging/logger.rb', line 91

def fatal message = nil, &block
  if block_given?
    add 4, nil, message, &block
  else
    add 4, message, nil, &block
  end
end

#fatal?Boolean

Returns true if the current severity level allows for sending FATAL messages.

Returns:

  • (Boolean)

162
163
164
# File 'lib/gcloud/logging/logger.rb', line 162

def fatal?
  @level <= 4
end

#info(message = nil, &block) ⇒ Object

Log an INFO message.


61
62
63
64
65
66
67
# File 'lib/gcloud/logging/logger.rb', line 61

def info message = nil, &block
  if block_given?
    add 1, nil, message, &block
  else
    add 1, message, nil, &block
  end
end

#info?Boolean

Returns true if the current severity level allows for sending INFO messages.

Returns:

  • (Boolean)

141
142
143
# File 'lib/gcloud/logging/logger.rb', line 141

def info?
  @level <= 1
end

#level=(severity) ⇒ Object Also known as: sev_threshold=

Set logging severity threshold.


168
169
170
171
172
# File 'lib/gcloud/logging/logger.rb', line 168

def level= severity
  new_level = derive_severity severity
  fail ArgumentError, "invalid log level: #{severity}" if new_level.nil?
  @level = new_level
end

#unknown(message = nil, &block) ⇒ Object

Log an UNKNOWN message. This will be printed no matter what the logger's level is.


102
103
104
105
106
107
108
# File 'lib/gcloud/logging/logger.rb', line 102

def unknown message = nil, &block
  if block_given?
    add 5, nil, message, &block
  else
    add 5, message, nil, &block
  end
end

#warn(message = nil, &block) ⇒ Object

Log a WARN message.


71
72
73
74
75
76
77
# File 'lib/gcloud/logging/logger.rb', line 71

def warn message = nil, &block
  if block_given?
    add 2, nil, message, &block
  else
    add 2, message, nil, &block
  end
end

#warn?Boolean

Returns true if the current severity level allows for sending WARN messages.

Returns:

  • (Boolean)

148
149
150
# File 'lib/gcloud/logging/logger.rb', line 148

def warn?
  @level <= 2
end