2023-07-15 01:15:13 +02:00
|
|
|
// Copyright 2023 Woodpecker 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 datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
xlog "xorm.io/xorm/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newXORMLogger(level xlog.LogLevel) xlog.Logger {
|
|
|
|
return &xormLogger{
|
|
|
|
logger: log.With().Str("component", "xorm").Logger(),
|
|
|
|
level: level,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// xormLogger custom log implementation for ILogger.
|
2023-07-15 01:15:13 +02:00
|
|
|
type xormLogger struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
level xlog.LogLevel
|
|
|
|
showSQL bool
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Error implement ILogger.
|
2023-11-12 19:23:48 +02:00
|
|
|
func (x *xormLogger) Error(v ...any) {
|
2023-07-15 01:15:13 +02:00
|
|
|
if x.level <= xlog.LOG_ERR {
|
|
|
|
x.logger.Error().Msg(fmt.Sprintln(v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Errorf implement ILogger.
|
2023-11-12 19:23:48 +02:00
|
|
|
func (x *xormLogger) Errorf(format string, v ...any) {
|
2023-07-15 01:15:13 +02:00
|
|
|
if x.level <= xlog.LOG_ERR {
|
|
|
|
x.logger.Error().Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Debug implement ILogger.
|
2023-11-12 19:23:48 +02:00
|
|
|
func (x *xormLogger) Debug(v ...any) {
|
2023-07-15 01:15:13 +02:00
|
|
|
if x.level <= xlog.LOG_DEBUG {
|
|
|
|
x.logger.Debug().Msg(fmt.Sprintln(v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Debugf implement ILogger.
|
2023-11-12 19:23:48 +02:00
|
|
|
func (x *xormLogger) Debugf(format string, v ...any) {
|
2023-07-15 01:15:13 +02:00
|
|
|
if x.level <= xlog.LOG_DEBUG {
|
|
|
|
x.logger.Debug().Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Info implement ILogger.
|
2023-11-12 19:23:48 +02:00
|
|
|
func (x *xormLogger) Info(v ...any) {
|
2023-07-15 01:15:13 +02:00
|
|
|
if x.level <= xlog.LOG_INFO {
|
|
|
|
x.logger.Info().Msg(fmt.Sprintln(v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Infof implement ILogger.
|
2023-11-12 19:23:48 +02:00
|
|
|
func (x *xormLogger) Infof(format string, v ...any) {
|
2023-07-15 01:15:13 +02:00
|
|
|
if x.level <= xlog.LOG_INFO {
|
|
|
|
x.logger.Info().Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Warn implement ILogger.
|
2023-11-12 19:23:48 +02:00
|
|
|
func (x *xormLogger) Warn(v ...any) {
|
2023-07-15 01:15:13 +02:00
|
|
|
if x.level <= xlog.LOG_WARNING {
|
|
|
|
x.logger.Warn().Msg(fmt.Sprintln(v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Warnf implement ILogger.
|
2023-11-12 19:23:48 +02:00
|
|
|
func (x *xormLogger) Warnf(format string, v ...any) {
|
2023-07-15 01:15:13 +02:00
|
|
|
if x.level <= xlog.LOG_WARNING {
|
|
|
|
x.logger.Warn().Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Level implement ILogger.
|
2023-07-15 01:15:13 +02:00
|
|
|
func (x *xormLogger) Level() xlog.LogLevel {
|
|
|
|
return xlog.LOG_INFO
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// SetLevel implement ILogger.
|
2023-07-15 01:15:13 +02:00
|
|
|
func (x *xormLogger) SetLevel(l xlog.LogLevel) {
|
|
|
|
x.level = l
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// ShowSQL implement ILogger.
|
2023-07-15 01:15:13 +02:00
|
|
|
func (x *xormLogger) ShowSQL(show ...bool) {
|
|
|
|
if len(show) == 0 {
|
|
|
|
x.showSQL = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
x.showSQL = show[0]
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// IsShowSQL implement ILogger.
|
2023-07-15 01:15:13 +02:00
|
|
|
func (x *xormLogger) IsShowSQL() bool {
|
|
|
|
return x.showSQL
|
|
|
|
}
|