2020-01-08 05:28:43 -08:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-01-20 05:17:11 -08:00
|
|
|
"strings"
|
2020-01-08 05:28:43 -08:00
|
|
|
"testing"
|
|
|
|
|
2022-02-12 15:58:27 -08:00
|
|
|
"github.com/grafana/regexp"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-08 05:28:43 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestJSONFileLogger_basic(t *testing.T) {
|
2022-04-27 02:24:36 -07:00
|
|
|
f, err := os.CreateTemp("", "logging")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-24 00:33:22 -07:00
|
|
|
defer func() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, f.Close())
|
|
|
|
require.NoError(t, os.Remove(f.Name()))
|
2020-08-24 00:33:22 -07:00
|
|
|
}()
|
2020-01-08 05:28:43 -08:00
|
|
|
|
|
|
|
l, err := NewJSONFileLogger(f.Name())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, l, "logger can't be nil")
|
2020-01-08 05:28:43 -08:00
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
l.Info("test", "hello", "world")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-01-08 05:28:43 -08:00
|
|
|
r := make([]byte, 1024)
|
|
|
|
_, err = f.Read(r)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2024-09-09 18:41:53 -07:00
|
|
|
|
|
|
|
result, err := regexp.Match(`^{"time":"[^"]+","level":"INFO","source":\{.+\},"msg":"test","hello":"world"}\n`, r)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, result, "unexpected content: %s", r)
|
2020-01-08 05:28:43 -08:00
|
|
|
|
|
|
|
err = l.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-01-08 05:28:43 -08:00
|
|
|
|
|
|
|
err = l.file.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.HasSuffix(err.Error(), os.ErrClosed.Error()), "file not closed")
|
2020-01-20 05:17:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONFileLogger_parallel(t *testing.T) {
|
2022-04-27 02:24:36 -07:00
|
|
|
f, err := os.CreateTemp("", "logging")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-24 00:33:22 -07:00
|
|
|
defer func() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, f.Close())
|
|
|
|
require.NoError(t, os.Remove(f.Name()))
|
2020-08-24 00:33:22 -07:00
|
|
|
}()
|
2020-01-20 05:17:11 -08:00
|
|
|
|
|
|
|
l, err := NewJSONFileLogger(f.Name())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, l, "logger can't be nil")
|
2020-01-20 05:17:11 -08:00
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
l.Info("test", "hello", "world")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-01-20 05:17:11 -08:00
|
|
|
|
|
|
|
l2, err := NewJSONFileLogger(f.Name())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, l, "logger can't be nil")
|
2020-01-20 05:17:11 -08:00
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
l2.Info("test", "hello", "world")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-01-20 05:17:11 -08:00
|
|
|
|
|
|
|
err = l.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-01-20 05:17:11 -08:00
|
|
|
|
|
|
|
err = l.file.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.HasSuffix(err.Error(), os.ErrClosed.Error()), "file not closed")
|
2020-01-20 05:17:11 -08:00
|
|
|
|
|
|
|
err = l2.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-01-20 05:17:11 -08:00
|
|
|
|
|
|
|
err = l2.file.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.HasSuffix(err.Error(), os.ErrClosed.Error()), "file not closed")
|
2020-01-08 05:28:43 -08:00
|
|
|
}
|