Class: Google::Cloud::Logging::Logger

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

Overview

Logger

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

Examples:

require "google/cloud"

gcloud = Google::Cloud.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 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.

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.



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

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.



74
75
76
77
78
79
80
# File 'lib/google/cloud/logging/logger.rb', line 74

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)


207
208
209
# File 'lib/google/cloud/logging/logger.rb', line 207

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.



128
129
130
131
132
133
134
# File 'lib/google/cloud/logging/logger.rb', line 128

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)


228
229
230
# File 'lib/google/cloud/logging/logger.rb', line 228

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.



146
147
148
149
150
151
152
# File 'lib/google/cloud/logging/logger.rb', line 146

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)


235
236
237
# File 'lib/google/cloud/logging/logger.rb', line 235

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.



92
93
94
95
96
97
98
# File 'lib/google/cloud/logging/logger.rb', line 92

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)


214
215
216
# File 'lib/google/cloud/logging/logger.rb', line 214

def info?
  @level <= 1
end

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

Sets the logging severity level.

Examples:

require "google/cloud"

gcloud = Google::Cloud.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



260
261
262
263
264
# File 'lib/google/cloud/logging/logger.rb', line 260

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.



165
166
167
168
169
170
171
# File 'lib/google/cloud/logging/logger.rb', line 165

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.



110
111
112
113
114
115
116
# File 'lib/google/cloud/logging/logger.rb', line 110

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)


221
222
223
# File 'lib/google/cloud/logging/logger.rb', line 221

def warn?
  @level <= 2
end