Search code examples
bashshellgoogle-cloud-platformgoogle-cloud-logginggoogle-cloud-shell

gcp cli error =(gcloud.logging.write) unrecognized arguments


I am writing logs in gcp logs using below command:

gcloud logging write my-test-log '{ "ServiceName": "$Service1", "SubsystemName": "$subsystem1") --payload-type=json

it is giving below error:

(gcloud.logging.write) unrecognized arguments.

Please suggest


Solution

  • You are receiving that error because you have some syntax errors.

    As LundinCast said, the correct command should be:

    gcloud logging write my-test-log '{ "ServiceName": "$Service1", "SubsystemName": "$subsystem1"}' --payload-type=json
    

    You only will receive the following output:

    Created log entry.
    

    Please check the following documentation for further information: gcloud logging write

    And you could verify if it works listing your project's logs:

    gcloud logging logs list 
    
    NAME
    projects/<<PROJECT-ID>>/logs/my-test-log
    

    or reading the log entry:

    gcloud logging read my-test-log
    
    ---
    insertId: 1abcd235657fghi
    jsonPayload:
      ServiceName: $Service1
      SubsystemName: $subsystem1
    logName: projects/<<PROJECT-ID>>/logs/my-test-log
    receiveTimestamp: '2020-11-26T21:10:25.757859880Z'
    resource:
      labels:
        project_id: <<PROJECT-ID>>
      type: global
    timestamp: '2020-11-26T21:10:25.757859880Z'
    
    

    Edit 1

    I found the problem in my original answer. To get the value of a variable in a json format it is necessary to add "'" so the command should be something like:

    gcloud logging write --payload-type=json my-test-log '{ "message": "'"$variable"'" , "weather": "partly cloudy"}'
    

    Where the content of $variable is my-variable-content

    insertId: 1y7abcdefghijk
    jsonPayload:
      message: my-variable-content
      weather: partly cloudy
    logName: projects/<<PROJECT-ID>>/logs/my-test-log
    receiveTimestamp: '2020-12-04T14:03:45.850928566Z'
    resource:
      labels:
        project_id: <<PROJECT-ID>>
      type: global
    timestamp: '2020-12-04T14:03:45.850928566Z'