Skip to content

[feature][functions] Add the ability to customize logging level for Go & Python functions #16928

@tpiperatgod

Description

@tpiperatgod

Is your feature request related to a problem? Please describe.
Currently, if the user wants to output more logging level logs other than INFO, it should provide the log settings for different pulsar function runtimes.

Describe the solution you'd like

  • Python functions

    Add the --logging_level argument to python_instance_main.py to pass in the logging level, and add the following handle logic to log.py to set the logging level for each handlers.

    def init_logger(level, logfile, logging_config_file):
        global Log
        # get log file location for function instance
        os.environ['LOG_FILE'] = logfile
        logging.config.fileConfig(logging_config_file)
        Log = logging.getLogger()
        # the priority of `level` is higher than `logging_config_file`
        if level != "":
            Log.setLevel(level)
            # set logging level for each handler
            for h in Log.handlers:
                h.setLevel(level)
  • Go functions

    Pass in the logging level via environment variable, such as setting LOGGING_LEVEL, then add parsing of the LOGGING_LEVEL environment variable in log.go, , and finally set the logging level.

    const (
    	// environment variable for custom logging level
    	logLevelEnvName      = "LOGGING_LEVEL"
    	defaultLogLevel      = log.InfoLevel
    	defaultLogTimeFormat = "2006/01/02 15:04:05.000"
    )
    
    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{})
    }

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/featureThe PR added a new feature or issue requested a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions