2

I want to log using Stackdriver Logging to App Engine using Redis queue. So I'm using Redis Server, Redis Queue and Python logging to do this. Here's my code:

import logging
from redis import Redis
from rq import Queue
import time

class SomeClass():

def log_using_redis(self,text):
    log_text = logging.warn(text)
    f=open("stack_log.txt","a+")
    f.write(str(text))
    return "logged Successfully using redis"


def get(self):
    text = 'Hello, Logged Successfully!'+time.strftime('%a, %d %b %Y %H:%M:%S %Z(%z)')
    redis_conn = Redis()
    q = Queue(connection=redis_conn)
    job = q.enqueue(self.log_using_redis,text)
    print job.result

When I run RQ worker I'm getting some output on terminal but couldn't find where the logs are being stored.

If I try to log directly without using Redis, the logs are being stored at Global in the logging section of Google Cloud. The queue is working properly, to check I've been appending the text to a file.

It seems the logging isn't working. If it is being logged, where can I find my logs on Google Cloud?

Rubén C.
  • 1,098
  • 6
  • 16
  • I'm curious–are you using _[Google Cloud logger](https://google-cloud-python.readthedocs.io/en/latest/logging/usage.html)_? Also I think the enqueued function should error because the worker wouldn't be able to import and run it with it being an instance method. – Oluwafemi Sule May 03 '18 at 19:03
  • Try this: go to the GCP console, App Engine and services. Select the service in which you have deployed your application. In the right side click on tools and select "Logs". This will redirect the page to your app logs. Can you see them there? – Rubén C. May 04 '18 at 10:40
  • @OluwafemiSule Yes I'm using Google Cloud logger. The enqueue function is working, which I've referred from http://python-rq.org/ . – Praneeth Rajarikam May 04 '18 at 10:47
  • 1
    @RubénC. I've checked in all types of logs (like GCE, GAE, Global, etc.) but couldn't find logs anywhere. – Praneeth Rajarikam May 04 '18 at 10:48

1 Answers1

0

Taking into account that you are using Python client library, use print() function to obtain the desired results. I don’t know if you are testing the application locally or you have deployed it.

  1. If you are testing the application locally: print() function output can be found in the cloud shell.
  2. If you have deployed the application: go to the GCP console, App Engine and services. Select the service in which you have deployed your application. In the right side click on tools and select "Logs". This will redirect the page to your app logs.

A more precise logging can be defined using the Stackdriver Logging for Python. The warning level can be defined. This can help you manage your application or identify events of interest. Find an example code here.

You might find useful Stackdriver Logging agent, an application based on fluentd that runs on your virtual machine (VM) instances. The Logging agent is pre-configured to send logs from VM instances to Stackdriver Logging. There are source and configuraiton files available for redis.

If you want a more general vision, App Engine flexible environment logs official documentation can help you to understand the different available logs.

Rubén C.
  • 1,098
  • 6
  • 16