Exemplo n.º 1
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.
    """
        client = self.context['logging_client']
        messages = self.context['logging_messages']
        project = properties.VALUES.core.project.Get(required=True)

        severity_value = getattr(
            messages.LogEntryMetadata.SeverityValueValuesEnum,
            args.severity.upper())

        labels = messages.LogEntryMetadata.LabelsValue()
        labels.additionalProperties = [
            messages.LogEntryMetadata.LabelsValue.AdditionalProperty(
                key='compute.googleapis.com/resource_type', value='instance'),
            messages.LogEntryMetadata.LabelsValue.AdditionalProperty(
                key='compute.googleapis.com/resource_id',
                value='sent with gcloud'),
        ]
        # Cloud Logging uses RFC 3339 time format.
        rfc3339_format = '%Y-%m-%dT%H:%M:%SZ'
        meta = messages.LogEntryMetadata(
            timestamp=datetime.datetime.utcnow().strftime(rfc3339_format),
            severity=severity_value,
            serviceName='compute.googleapis.com',
            labels=labels)
        entry = messages.LogEntry(metadata=meta)

        if args.payload_type == 'struct':
            json_object = util.ConvertToJsonObject(args.message)
            struct = messages.LogEntry.StructPayloadValue()
            # Protobufs in Python do strict type-checking. We have to change the
            # type from JsonObject.Property to StructPayloadValue.AdditionalProperty
            # even though both types have the same fields (key, value).
            struct.additionalProperties = [
                messages.LogEntry.StructPayloadValue.AdditionalProperty(
                    key=json_property.key, value=json_property.value)
                for json_property in json_object.properties
            ]
            entry.structPayload = struct
        else:
            entry.textPayload = args.message

        request = messages.WriteLogEntriesRequest(entries=[entry])
        try:
            unused_result = client.projects_logs_entries.Write(
                messages.LoggingProjectsLogsEntriesWriteRequest(
                    projectsId=project,
                    logsId=args.log_name,
                    writeLogEntriesRequest=request))
            log.status.write('Created log entry.\n')
        except apitools_base.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
Exemplo n.º 2
0
  def Run(self, args):
    """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.
    """
    client = self.context['logging_client_v2beta1']
    messages = self.context['logging_messages_v2beta1']
    project = properties.VALUES.core.project.Get(required=True)

    severity_value = getattr(messages.LogEntry.SeverityValueValuesEnum,
                             args.severity.upper())

    entry = messages.LogEntry(
        logName=util.CreateLogResourceName('projects/{0}'.format(project),
                                           args.log_name),
        resource=messages.MonitoredResource(type='global'),
        severity=severity_value)

    if args.payload_type == 'json' or args.payload_type == 'struct':
      json_object = util.ConvertToJsonObject(args.message)
      struct = messages.LogEntry.JsonPayloadValue()
      # Protobufs in Python do strict type-checking. We have to change the
      # type from JsonObject.Property to JsonPayloadValue.AdditionalProperty
      # even though both types have the same fields (key, value).
      struct.additionalProperties = [
          messages.LogEntry.JsonPayloadValue.AdditionalProperty(
              key=json_property.key,
              value=json_property.value)
          for json_property in json_object.properties
      ]
      entry.jsonPayload = struct
    else:
      entry.textPayload = args.message

    client.entries.Write(
        messages.WriteLogEntriesRequest(entries=[entry]))
    log.status.write('Created log entry.\n')