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 (
|
2022-06-27 09:16:58 -07:00
|
|
|
"fmt"
|
2024-09-09 18:41:53 -07:00
|
|
|
"log/slog"
|
2020-01-08 05:28:43 -08:00
|
|
|
"os"
|
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
"github.com/prometheus/common/promslog"
|
2020-01-08 05:28:43 -08:00
|
|
|
)
|
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
// JSONFileLogger represents a logger that writes JSON to a file. It implements the promql.QueryLogger interface.
|
2020-01-08 05:28:43 -08:00
|
|
|
type JSONFileLogger struct {
|
2024-09-09 18:41:53 -07:00
|
|
|
logger *slog.Logger
|
2020-01-08 05:28:43 -08:00
|
|
|
file *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewJSONFileLogger returns a new JSONFileLogger.
|
|
|
|
func NewJSONFileLogger(s string) (*JSONFileLogger, error) {
|
|
|
|
if s == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 01:06:44 -07:00
|
|
|
f, err := os.OpenFile(s, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o666)
|
2020-01-08 05:28:43 -08:00
|
|
|
if err != nil {
|
2024-09-09 18:41:53 -07:00
|
|
|
return nil, fmt.Errorf("can't create json log file: %w", err)
|
2020-01-08 05:28:43 -08:00
|
|
|
}
|
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
jsonFmt := &promslog.AllowedFormat{}
|
|
|
|
_ = jsonFmt.Set("json")
|
2020-01-08 05:28:43 -08:00
|
|
|
return &JSONFileLogger{
|
2024-09-09 18:41:53 -07:00
|
|
|
logger: promslog.New(&promslog.Config{Format: jsonFmt, Writer: f}),
|
2020-01-08 05:28:43 -08:00
|
|
|
file: f,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
// Close closes the underlying file. It implements the promql.QueryLogger interface.
|
2020-01-08 05:28:43 -08:00
|
|
|
func (l *JSONFileLogger) Close() error {
|
|
|
|
return l.file.Close()
|
|
|
|
}
|
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
// With calls the `With()` method on the underlying `log/slog.Logger` with the
|
|
|
|
// provided msg and args. It implements the promql.QueryLogger interface.
|
|
|
|
func (l *JSONFileLogger) With(args ...any) {
|
|
|
|
l.logger = l.logger.With(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info calls the `Info()` method on the underlying `log/slog.Logger` with the
|
|
|
|
// provided msg and args. It implements the promql.QueryLogger interface.
|
|
|
|
func (l *JSONFileLogger) Info(msg string, args ...any) {
|
|
|
|
l.logger.Info(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error calls the `Error()` method on the underlying `log/slog.Logger` with the
|
|
|
|
// provided msg and args. It implements the promql.QueryLogger interface.
|
|
|
|
func (l *JSONFileLogger) Error(msg string, args ...any) {
|
|
|
|
l.logger.Error(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug calls the `Debug()` method on the underlying `log/slog.Logger` with the
|
|
|
|
// provided msg and args. It implements the promql.QueryLogger interface.
|
|
|
|
func (l *JSONFileLogger) Debug(msg string, args ...any) {
|
|
|
|
l.logger.Debug(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn calls the `Warn()` method on the underlying `log/slog.Logger` with the
|
|
|
|
// provided msg and args. It implements the promql.QueryLogger interface.
|
|
|
|
func (l *JSONFileLogger) Warn(msg string, args ...any) {
|
|
|
|
l.logger.Warn(msg, args...)
|
2020-01-08 05:28:43 -08:00
|
|
|
}
|