github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/apmlog/logger_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package apmlog 19 20 import ( 21 "bytes" 22 "io/ioutil" 23 "log" 24 "os" 25 "path/filepath" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func init() { 33 os.Unsetenv("ELASTIC_APM_LOG_FILE") 34 os.Unsetenv("ELASTIC_APM_LOG_LEVEL") 35 SetDefaultLogger(nil) 36 } 37 38 func TestDefaultLoggerNoEnv(t *testing.T) { 39 SetDefaultLogger(nil) 40 assert.Nil(t, DefaultLogger()) 41 } 42 43 func TestDefaultLoggerInvalidFile(t *testing.T) { 44 var logbuf bytes.Buffer 45 log.SetOutput(&logbuf) 46 47 SetDefaultLogger(nil) 48 os.Setenv("ELASTIC_APM_LOG_FILE", ".") 49 defer os.Unsetenv("ELASTIC_APM_LOG_FILE") 50 51 assert.Nil(t, DefaultLogger()) 52 assert.Regexp(t, `failed to create "\.": .* \(disabling logging\)`, logbuf.String()) 53 } 54 55 func TestDefaultLoggerFile(t *testing.T) { 56 dir, err := ioutil.TempDir("", "") 57 require.NoError(t, err) 58 defer os.RemoveAll(dir) 59 60 SetDefaultLogger(nil) 61 os.Setenv("ELASTIC_APM_LOG_FILE", filepath.Join(dir, "log.json")) 62 defer os.Unsetenv("ELASTIC_APM_LOG_FILE") 63 64 logger := DefaultLogger() 65 require.NotNil(t, logger) 66 logger.Debugf("debug message") 67 logger.Errorf("error message") 68 69 data, err := ioutil.ReadFile(filepath.Join(dir, "log.json")) 70 require.NoError(t, err) 71 assert.Regexp(t, `{"level":"error","time":".*","message":"error message"}`, string(data)) 72 } 73 74 func TestDefaultLoggerStdio(t *testing.T) { 75 origStdout, origStderr := os.Stdout, os.Stderr 76 defer func() { 77 os.Stdout, os.Stderr = origStdout, origStderr 78 }() 79 80 tempStdout, err := ioutil.TempFile("", "stdout-") 81 require.NoError(t, err) 82 defer os.Remove(tempStdout.Name()) 83 defer tempStdout.Close() 84 os.Stdout = tempStdout 85 86 tempStderr, err := ioutil.TempFile("", "stderr-") 87 require.NoError(t, err) 88 defer os.Remove(tempStdout.Name()) 89 defer tempStderr.Close() 90 os.Stderr = tempStderr 91 92 defer os.Unsetenv("ELASTIC_APM_LOG_FILE") 93 for _, filename := range []string{"stdout", "stderr"} { 94 SetDefaultLogger(nil) 95 os.Setenv("ELASTIC_APM_LOG_FILE", filename) 96 require.NotNil(t, DefaultLogger()) 97 DefaultLogger().Errorf("%s", filename) 98 } 99 100 stdoutContents, err := ioutil.ReadFile(tempStdout.Name()) 101 require.NoError(t, err) 102 assert.Regexp(t, `{"level":"error","time":".*","message":"stdout"}`, string(stdoutContents)) 103 104 stderrContents, err := ioutil.ReadFile(tempStderr.Name()) 105 require.NoError(t, err) 106 assert.Regexp(t, `{"level":"error","time":".*","message":"stderr"}`, string(stderrContents)) 107 } 108 109 func TestDefaultLoggerInvalidLevel(t *testing.T) { 110 var logbuf bytes.Buffer 111 log.SetOutput(&logbuf) 112 113 dir, err := ioutil.TempDir("", "") 114 require.NoError(t, err) 115 defer os.RemoveAll(dir) 116 117 SetDefaultLogger(nil) 118 os.Setenv("ELASTIC_APM_LOG_FILE", filepath.Join(dir, "log.json")) 119 os.Setenv("ELASTIC_APM_LOG_LEVEL", "panic") 120 defer os.Unsetenv("ELASTIC_APM_LOG_FILE") 121 defer os.Unsetenv("ELASTIC_APM_LOG_LEVEL") 122 123 logger := DefaultLogger() 124 require.NotNil(t, logger) 125 logger.Debugf("debug message") 126 logger.Errorf("error message") 127 128 data, err := ioutil.ReadFile(filepath.Join(dir, "log.json")) 129 require.NoError(t, err) 130 assert.Regexp(t, `{"level":"error","time":".*","message":"error message"}`, string(data)) 131 assert.Regexp(t, `invalid ELASTIC_APM_LOG_LEVEL "panic", falling back to "error"`, logbuf.String()) 132 } 133 134 func TestDefaultLoggerLevel(t *testing.T) { 135 var logbuf bytes.Buffer 136 log.SetOutput(&logbuf) 137 138 dir, err := ioutil.TempDir("", "") 139 require.NoError(t, err) 140 defer os.RemoveAll(dir) 141 142 SetDefaultLogger(nil) 143 os.Setenv("ELASTIC_APM_LOG_FILE", filepath.Join(dir, "log.json")) 144 os.Setenv("ELASTIC_APM_LOG_LEVEL", "debug") 145 defer os.Unsetenv("ELASTIC_APM_LOG_FILE") 146 defer os.Unsetenv("ELASTIC_APM_LOG_LEVEL") 147 148 logger := DefaultLogger() 149 require.NotNil(t, logger) 150 logger.Debugf("debug message") 151 logger.Errorf("error message") 152 153 data, err := ioutil.ReadFile(filepath.Join(dir, "log.json")) 154 require.NoError(t, err) 155 assert.Regexp(t, ` 156 {"level":"debug","time":".*","message":"debug message"} 157 {"level":"error","time":".*","message":"error message"}`[1:], 158 string(data)) 159 } 160 161 func BenchmarkDefaultLogger(b *testing.B) { 162 dir, err := ioutil.TempDir("", "") 163 require.NoError(b, err) 164 defer os.RemoveAll(dir) 165 166 SetDefaultLogger(nil) 167 os.Setenv("ELASTIC_APM_LOG_FILE", filepath.Join(dir, "log.json")) 168 defer os.Unsetenv("ELASTIC_APM_LOG_FILE") 169 require.NotNil(b, DefaultLogger()) 170 171 b.ResetTimer() 172 for i := 0; i < b.N; i++ { 173 DefaultLogger().Errorf("debug message") 174 } 175 }