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 ()
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,40 @@ 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 () {
262+ var (
263+ level int
264+ envLevel = getStringValueFromEnvVarOrDefault (envLogLevel , "3" )
265+ )
266+
267+ // try to convert from integer first
268+ if lvl , err := strconv .Atoi (envLevel ); err == nil {
269+ if lvl >= 1 && lvl <= 5 {
270+ currentLogLevel = lvl
271+ return
272+ }
273+ }
274+
275+ // fall-through, check for string values
276+ switch envLevel {
277+ case "ERROR" :
278+ level = 1
279+ case "WARNING" :
280+ level = 2
281+ case "INFO" :
282+ level = 3
283+ case "DEBUG" :
284+ level = 4
285+ case "ALL" :
286+ level = 5
287+ default :
288+ level = 3 // INFO
289+ }
290+
291+ currentLogLevel = level
292+ }
293+
260294func getTimeoutValue (envVar , defaultValue , description string ) (time.Duration , error ) {
261295 timeoutString := getStringValueFromEnvVarOrDefault (envVar , defaultValue )
262296 timeout , err := strconv .ParseInt (timeoutString , 10 , 64 )
@@ -418,7 +452,7 @@ func (s *Session) Close() {
418452 return newSessionLifecycleEvent (s , Closed )
419453 })
420454 if err != nil {
421- log . Printf ( "unable to close session %s %v" , s .sessionID , err )
455+ logMessage ( WARNING , "unable to close session %s %v" , s .sessionID , err )
422456 }
423457 } else {
424458 defer s .mapMutex .Unlock ()
@@ -753,7 +787,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
753787 // check if a tls.Config has been set and use this, otherwise continue to check for env and other options
754788 if s .TlSConfig != nil {
755789 if s .TlSConfig .InsecureSkipVerify {
756- log . Println ( insecureWarning )
790+ logMessage ( WARNING , insecureWarning )
757791 }
758792 return grpc .WithTransportCredentials (credentials .NewTLS (s .TlSConfig )), nil
759793 }
@@ -774,7 +808,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
774808
775809 ignoreInvalidCerts := ignoreInvalidCertsEnv == "true"
776810 if ignoreInvalidCerts {
777- log . Println ( insecureWarning )
811+ logMessage ( WARNING , insecureWarning )
778812 }
779813 s .IgnoreInvalidCerts = ignoreInvalidCerts
780814
@@ -801,7 +835,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
801835 if s .CaCertPath != "" {
802836 cp = x509 .NewCertPool ()
803837
804- log . Println ( "loading CA certificate" )
838+ logMessage ( DEBUG , "loading CA certificate" )
805839 if err = validateFilePath (s .CaCertPath ); err != nil {
806840 return nil , err
807841 }
@@ -817,7 +851,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
817851 }
818852
819853 if s .ClientCertPath != "" && s .ClientKeyPath != "" {
820- log . Println ( "loading client certificate and key, cert=" , s . ClientCertPath , " key=" , s .ClientKeyPath )
854+ logMessage ( DEBUG , "loading client certificate and key paths , cert=%s, key=%s" , s . ClientCertPath , s .ClientKeyPath )
821855 if err = validateFilePath (s .ClientCertPath ); err != nil {
822856 return nil , err
823857 }
0 commit comments