This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 514b9c03076  Open [feature][function] add the ability to customize 
logging level for Go & Python functions (#16939)
514b9c03076 is described below

commit 514b9c030769ec0a25728383bf856dd96ca7434c
Author: laminar (Tian Fang) <[email protected]>
AuthorDate: Mon Aug 8 17:11:33 2022 +0800

     Open [feature][function] add the ability to customize logging level for Go 
& Python functions (#16939)
    
    Signed-off-by: laminar <[email protected]>
---
 pulsar-function-go/logutil/log.go                             |  7 +++++++
 pulsar-functions/instance/src/main/python/log.py              |  5 ++++-
 .../instance/src/main/python/python_instance_main.py          | 11 ++++++++++-
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/pulsar-function-go/logutil/log.go 
b/pulsar-function-go/logutil/log.go
index 07970062d9d..75d28e4a0ae 100644
--- a/pulsar-function-go/logutil/log.go
+++ b/pulsar-function-go/logutil/log.go
@@ -32,6 +32,7 @@ import (
 )
 
 const (
+       logLevelEnvName      = "LOGGING_LEVEL"
        defaultLogLevel      = log.InfoLevel
        defaultLogTimeFormat = "2006/01/02 15:04:05.000"
 )
@@ -175,6 +176,12 @@ func (f *TextFormatter) Format(entry *log.Entry) ([]byte, 
error) {
 
 func init() {
        log.SetLevel(defaultLogLevel)
+       // lookup and parse the logLevel variable
+       if logLevelStr, exist := os.LookupEnv(logLevelEnvName); exist {
+               if logLevel, err := log.ParseLevel(logLevelStr); err == nil {
+                       log.SetLevel(logLevel)
+               }
+       }
        log.AddHook(&contextHook{})
        log.SetFormatter(&TextFormatter{})
 }
diff --git a/pulsar-functions/instance/src/main/python/log.py 
b/pulsar-functions/instance/src/main/python/log.py
index fcf1bdc95a8..06eac25f829 100644
--- a/pulsar-functions/instance/src/main/python/log.py
+++ b/pulsar-functions/instance/src/main/python/log.py
@@ -90,7 +90,10 @@ def init_logger(level, logfile, logging_config_file):
   os.environ['LOG_FILE'] = logfile
   logging.config.fileConfig(logging_config_file)
   Log = logging.getLogger()
-  Log.setLevel(level)
+  if level is not None:
+    Log.setLevel(level)
+    for h in Log.handlers:
+      h.setLevel(level)
 
   # set print to redirect to logger
   class StreamToLogger(object):
diff --git a/pulsar-functions/instance/src/main/python/python_instance_main.py 
b/pulsar-functions/instance/src/main/python/python_instance_main.py
index 5f7669b9c4d..3967635365c 100755
--- a/pulsar-functions/instance/src/main/python/python_instance_main.py
+++ b/pulsar-functions/instance/src/main/python/python_instance_main.py
@@ -78,6 +78,7 @@ def main():
   parser.add_argument('--max_buffered_tuples', required=True, help='Maximum 
number of Buffered tuples')
   parser.add_argument('--logging_directory', required=True, help='Logging 
Directory')
   parser.add_argument('--logging_file', required=True, help='Log file name')
+  parser.add_argument('--logging_level', required=False, help='Logging level')
   parser.add_argument('--logging_config_file', required=True, help='Config 
file for logging')
   parser.add_argument('--expected_healthcheck_interval', required=True, 
help='Expected time in seconds between health checks', type=int)
   parser.add_argument('--secrets_provider', required=False, help='The 
classname of the secrets provider')
@@ -154,7 +155,15 @@ def main():
   log_file = os.path.join(args.logging_directory,
                           
util.getFullyQualifiedFunctionName(function_details.tenant, 
function_details.namespace, function_details.name),
                           "%s-%s.log" % (args.logging_file, args.instance_id))
-  log.init_logger(logging.INFO, log_file, args.logging_config_file)
+  logging_level = {"notset": logging.NOTSET,
+                   "debug": logging.DEBUG,
+                   "info": logging.INFO,
+                   "warn": logging.WARNING,
+                   "warning": logging.WARNING,
+                   "error": logging.ERROR,
+                   "critical": logging.CRITICAL,
+                   "fatal": logging.CRITICAL}.get(args.logging_level, None)
+  log.init_logger(logging_level, log_file, args.logging_config_file)
 
   Log.info("Starting Python instance with %s" % str(args))
 

Reply via email to