github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/logger_unit_test.go (about) 1 //go:build all || unit 2 // +build all unit 3 4 package hedera 5 6 /*- 7 * 8 * Hedera Go SDK 9 * 10 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 * 24 */ 25 26 import ( 27 "bytes" 28 "os" 29 "testing" 30 "time" 31 32 "github.com/rs/zerolog" 33 34 "github.com/stretchr/testify/assert" 35 "github.com/stretchr/testify/require" 36 ) 37 38 func TestNewLogger(t *testing.T) { 39 logger := NewLogger("test", LoggerLevelDebug) 40 require.NotNil(t, logger) 41 assert.Equal(t, LoggerLevelDebug, logger.level) 42 } 43 44 func TestLogger_SetLevel(t *testing.T) { 45 logger := NewLogger("test", LoggerLevelDebug) 46 logger.SetLevel(LoggerLevelInfo) 47 assert.Equal(t, LoggerLevelInfo, logger.level) 48 } 49 func TestLogger_SetSilent(t *testing.T) { 50 var buf bytes.Buffer 51 writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339} 52 zerolog.SetGlobalLevel(zerolog.TraceLevel) 53 54 l := NewLogger("test", LoggerLevelTrace) 55 l.SetLevel(LoggerLevelTrace) 56 57 logger := zerolog.New(&writer) 58 l.logger = &logger 59 60 l.Debug("debug message", "debugKey", "debugValue") 61 l.Info("info message", "infoKey", "infoValue") 62 l.Warn("warn message", "warnKey", "warnValue") 63 l.Error("error message", "errorKey", "errorValue") 64 l.Trace("trace message", "traceKey", "traceValue") 65 assert.Contains(t, buf.String(), "debug message") 66 assert.Contains(t, buf.String(), "info message") 67 assert.Contains(t, buf.String(), "warn message") 68 assert.Contains(t, buf.String(), "error message") 69 assert.Contains(t, buf.String(), "trace message") 70 71 // coverage only 72 l.SetSilent(true) 73 l.SetSilent(false) 74 } 75 76 func TestNewLoggerWithEnvironmentVariableSet(t *testing.T) { 77 os.Setenv("HEDERA_SDK_GO_LOG_PRETTY", "1") 78 logger := NewLogger("test", LoggerLevelDebug) 79 require.NotNil(t, logger) 80 assert.Equal(t, LoggerLevelDebug, logger.level) 81 os.Unsetenv("HEDERA_SDK_GO_LOG_PRETTY") 82 }