11/*
2- * Copyright (c) 2022, 2024 Oracle and/or its affiliates.
2+ * Copyright (c) 2022, 2025 Oracle and/or its affiliates.
33 * Licensed under the Universal Permissive License v 1.0 as shown at
44 * https://oss.oracle.com/licenses/upl.
55 */
@@ -20,7 +20,6 @@ import (
2020 "google.golang.org/grpc/credentials/insecure"
2121 "google.golang.org/grpc/resolver"
2222 "google.golang.org/grpc/status"
23- "log"
2423 "os"
2524 "reflect"
2625 "strconv"
@@ -72,9 +71,8 @@ type Session struct {
7271 connectMutex sync.RWMutex // mutes for connection attempts
7372 firstConnectAttempted bool // indicates if the first connection has been attempted
7473 hasConnected bool // indicates if the session has ever connected
75- debug func (string , ... any ) // a function to output debug messages
76- debugConnection func (string , ... any ) // a function to output debug messages for gRPCV1 connections
77- messageDebugMode string // either "on" or "full"
74+ debug func (string , ... any ) // a function to output DEBUG messages
75+ debugConnection func (string , ... any ) // a function to output ALL messages for gRPCV1 connections
7876 requestID int64 // request id for gRPC v1
7977 filterID int64 // filter id for gRPC v1
8078 v1StreamManagerCache * streamManagerV1
@@ -192,24 +190,26 @@ func NewSession(ctx context.Context, options ...func(session *SessionOptions)) (
192190 // ensure name resolver has been registered
193191 resolver .Register (& nsLookupResolverBuilder {})
194192
195- if getBoolValueFromEnvVarOrDefault (envSessionDebug , false ) {
193+ // set the coherenceLogLevel
194+ setLogLevel (getStringValueFromEnvVarOrDefault (envLogLevel , "3" ))
195+
196+ if getBoolValueFromEnvVarOrDefault (envSessionDebug , false ) || currentLogLevel >= int (DEBUG ) {
196197 // enable session debugging
197198 session .debug = func (format string , v ... any ) {
198199 logMessage (DEBUG , format , v ... )
199200 }
201+ if currentLogLevel <= int (DEBUG ) {
202+ currentLogLevel = int (DEBUG )
203+ }
200204 }
201205
202206 messageDebug := getStringValueFromEnvVarOrDefault (envMessageDebug , "" )
203- if messageDebug != "" {
207+ if messageDebug != "" || currentLogLevel == int ( ALL ) {
204208 // enable session debugging
205209 session .debugConnection = func (s string , v ... any ) {
206- msg := getLogMessage (DEBUG , s , v ... )
207- if session .messageDebugMode == "on" && len (msg ) > 256 {
208- msg = msg [:256 ]
209- }
210- log .Println (msg )
210+ logMessage (DEBUG , s , v ... )
211211 }
212- session . messageDebugMode = messageDebug
212+ currentLogLevel = int ( ALL )
213213 }
214214
215215 // apply any options
@@ -257,6 +257,37 @@ func NewSession(ctx context.Context, options ...func(session *SessionOptions)) (
257257 return session , session .ensureConnection ()
258258}
259259
260+ // setLogLevel sets the log level from the COHERENCE_LOG_LEVEL environment variable.
261+ func setLogLevel (envLevel string ) {
262+ var level int
263+
264+ // try to convert from integer first
265+ if lvl , err := strconv .Atoi (envLevel ); err == nil {
266+ if lvl >= 1 && lvl <= 5 {
267+ currentLogLevel = lvl
268+ return
269+ }
270+ }
271+
272+ // fall-through, check for string values
273+ switch envLevel {
274+ case "ERROR" :
275+ level = 1
276+ case "WARNING" :
277+ level = 2
278+ case "INFO" :
279+ level = 3
280+ case "DEBUG" :
281+ level = 4
282+ case "ALL" :
283+ level = 5
284+ default :
285+ level = 3 // INFO
286+ }
287+
288+ currentLogLevel = level
289+ }
290+
260291func getTimeoutValue (envVar , defaultValue , description string ) (time.Duration , error ) {
261292 timeoutString := getStringValueFromEnvVarOrDefault (envVar , defaultValue )
262293 timeout , err := strconv .ParseInt (timeoutString , 10 , 64 )
@@ -418,7 +449,7 @@ func (s *Session) Close() {
418449 return newSessionLifecycleEvent (s , Closed )
419450 })
420451 if err != nil {
421- log . Printf ( "unable to close session %s %v" , s .sessionID , err )
452+ logMessage ( WARNING , "unable to close session %s %v" , s .sessionID , err )
422453 }
423454 } else {
424455 defer s .mapMutex .Unlock ()
@@ -753,7 +784,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
753784 // check if a tls.Config has been set and use this, otherwise continue to check for env and other options
754785 if s .TlSConfig != nil {
755786 if s .TlSConfig .InsecureSkipVerify {
756- log . Println ( insecureWarning )
787+ logMessage ( WARNING , insecureWarning )
757788 }
758789 return grpc .WithTransportCredentials (credentials .NewTLS (s .TlSConfig )), nil
759790 }
@@ -774,7 +805,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
774805
775806 ignoreInvalidCerts := ignoreInvalidCertsEnv == "true"
776807 if ignoreInvalidCerts {
777- log . Println ( insecureWarning )
808+ logMessage ( WARNING , insecureWarning )
778809 }
779810 s .IgnoreInvalidCerts = ignoreInvalidCerts
780811
@@ -801,7 +832,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
801832 if s .CaCertPath != "" {
802833 cp = x509 .NewCertPool ()
803834
804- log . Println ( "loading CA certificate" )
835+ logMessage ( DEBUG , "loading CA certificate" )
805836 if err = validateFilePath (s .CaCertPath ); err != nil {
806837 return nil , err
807838 }
@@ -817,7 +848,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
817848 }
818849
819850 if s .ClientCertPath != "" && s .ClientKeyPath != "" {
820- log . Println ( "loading client certificate and key, cert=" , s . ClientCertPath , " key=" , s .ClientKeyPath )
851+ logMessage ( DEBUG , "loading client certificate and key paths , cert=%s, key=%s" , s . ClientCertPath , s .ClientKeyPath )
821852 if err = validateFilePath (s .ClientCertPath ); err != nil {
822853 return nil , err
823854 }
0 commit comments