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.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging

resource = logging.resource "gae_app",
                            module_id: "1",
                            version_id: "20150925t173233"

logger = logging.logger "my_app_log", resource, env: :production
logger.info "Job started."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logging, log_name, resource, labels = nil) ⇒ Logger

Returns a new instance of Logger.



55
56
57
58
59
60
61
# File 'lib/gcloud/logging/logger.rb', line 55

def initialize logging, log_name, resource, labels = nil
  @logging = logging
  @log_name = log_name
  @resource = resource
  @labels = labels
  @level = 0 # DEBUG is the default behavior
end

Instance Attribute Details

#labelsObject (readonly)



51
52
53
# File 'lib/gcloud/logging/logger.rb', line 51

def labels
  @labels
end

#log_nameObject (readonly)



43
44
45
# File 'lib/gcloud/logging/logger.rb', line 43

def log_name
  @log_name
end

#loggingObject



39
40
41
# File 'lib/gcloud/logging/logger.rb', line 39

def logging
  @logging
end

#resourceObject (readonly)



47
48
49
# File 'lib/gcloud/logging/logger.rb', line 47

def resource
  @resource
end

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.

Parameters:

  • severity (Integer, String, Symbol)

    the integer code for or the name of the severity level

  • message (String, Hash) (defaults to: nil)

    The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).

Yields:

  • Evaluates to the message to log. This is not evaluated unless the logger’s level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/gcloud/logging/logger.rb', line 186

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) { ... } ⇒ Object

Log a ‘DEBUG` entry.

Parameters:

  • message (String, Hash) (defaults to: nil)

    The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).

Yields:

  • Evaluates to the message to log. This is not evaluated unless the logger’s level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.



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

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)


206
207
208
# File 'lib/gcloud/logging/logger.rb', line 206

def debug?
  @level <= 0
end

#error(message = nil) { ... } ⇒ Object

Log an ‘ERROR` entry.

Parameters:

  • message (String, Hash) (defaults to: nil)

    The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).

Yields:

  • Evaluates to the message to log. This is not evaluated unless the logger’s level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.



127
128
129
130
131
132
133
# File 'lib/gcloud/logging/logger.rb', line 127

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)


227
228
229
# File 'lib/gcloud/logging/logger.rb', line 227

def error?
  @level <= 3
end

#fatal(message = nil) { ... } ⇒ Object

Log a ‘FATAL` entry.

Parameters:

  • message (String, Hash) (defaults to: nil)

    The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).

Yields:

  • Evaluates to the message to log. This is not evaluated unless the logger’s level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.



145
146
147
148
149
150
151
# File 'lib/gcloud/logging/logger.rb', line 145

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)


234
235
236
# File 'lib/gcloud/logging/logger.rb', line 234

def fatal?
  @level <= 4
end

#info(message = nil) { ... } ⇒ Object

Log an ‘INFO` entry.

Parameters:

  • message (String, Hash) (defaults to: nil)

    The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).

Yields:

  • Evaluates to the message to log. This is not evaluated unless the logger’s level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.



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

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)


213
214
215
# File 'lib/gcloud/logging/logger.rb', line 213

def info?
  @level <= 1
end

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

Sets the logging severity level.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging

resource = logging.resource "gae_app",
                            module_id: "1",
                            version_id: "20150925t173233"

logger = logging.logger "my_app_log", resource, env: :production

logger.level = "INFO"
logger.debug "Job started." # No log entry written

Parameters:

  • severity (Integer, String, Symbol)

    the integer code for or the name of the severity level



259
260
261
262
263
# File 'lib/gcloud/logging/logger.rb', line 259

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) { ... } ⇒ Object

Log an ‘UNKNOWN` entry. This will be printed no matter what the logger’s current severity level is.

Parameters:

  • message (String, Hash) (defaults to: nil)

    The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).

Yields:

  • Evaluates to the message to log. This is not evaluated unless the logger’s level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.



164
165
166
167
168
169
170
# File 'lib/gcloud/logging/logger.rb', line 164

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

#warn(message = nil) { ... } ⇒ Object

Log a ‘WARN` entry.

Parameters:

  • message (String, Hash) (defaults to: nil)

    The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).

Yields:

  • Evaluates to the message to log. This is not evaluated unless the logger’s level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.



109
110
111
112
113
114
115
# File 'lib/gcloud/logging/logger.rb', line 109

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)


220
221
222
# File 'lib/gcloud/logging/logger.rb', line 220

def warn?
  @level <= 2
end