Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable query logging - Google Appengine SDK Python

I am in a situation where I have a package that is calling logging.debug(xxx). I want to disable all logging statements by this package. Is there a way to do that via config?

For example, every query I run is printing out in the console of the dev server:

DEBUG:root:SELECT Entities.path, Entities.entity FROM "dev~xxxx!!Entities" AS Entities INNER JOIN "dev~xxxx!!EntitiesByProperty" AS ebp_0 ON Entities.path = ebp_0.path INNER JOIN "dev~xxxx!!EntitiesByProperty" AS ebp_1 ON Entities.path = ebp_1.path INNER JOIN "dev~xxxx!!EntitiesByProperty" AS ebp_2 ON Entities.path = ebp_2.path WHERE ebp_0.kind = :1 AND ebp_0.name = :2 AND ebp_0.value = :3 AND ebp_1.kind = :4 AND ebp_1.name = :5 AND ebp_1.value = :6 AND ebp_2.kind = :7 AND ebp_2.name = :8 AND ebp_2.value = :9 ORDER BY Entities.path ASC

So I know how to disable it by modifying the sdk source, basically comment out the logging statement in __StarSchemaQueryPlan

logging.debug(query)

Is there a way to disable the logging without touching SDK code? We currently do not define any loggingConfigurations, and are using the basicConfiguror.


Final solution thank you @lucemia:

class Filter(object):
    def filter(self, record):
        if record.funcName=='__StarSchemaQueryPlan' and record.module=='datastore_sqlite_stub':
            return 0
        else:
            return 1
like image 819
Nix Avatar asked Dec 19 '12 15:12

Nix


1 Answers

You can try to modify the log level.

Since the module used default logger. The following code will disable all log which's level smaller than critical.

import logging
logger = logging.getLogger() # get the default logger
logger.setLevel(50) # set the skip all log which is smaller than critical (50)

test case

a_lib.py

import logging

def main():
    logging.error("test")
    logging.critical('c')

a_test_case.py

import a_lib
import logging

logger = logging.getLogger()
logger.setLevel(50)
a_lib.main()

results

> python a_test.py
CRITICAL:root:c

EDIT 1

Another way to do so is using filter

import logging

# define a new filter
class Filter():
    def filter(self, record):
        # extract property of record such as record.funcName, record.module
        # http://docs.python.org/2/library/logging.html#logrecord-attributes


        if record.module == "a_lib":
            # if the record is from a module you don't want to log
            return 0
        else:
           # for record you want to log
            return 1



 # apply this filter

 filter = Filter()
 logger= logging.getLogger()
 logger.addFilter(filter)
like image 56
lucemia Avatar answered Oct 22 '22 12:10

lucemia