Files
ragflow/internal/logger/logger.go
Zhichang Yu 0d85a8e7aa feat: add dynamic log level adjustment APIs (#13850)
Add REST APIs to dynamically query and modify log levels at runtime for
both Python (Flask) and Go servers.

Changes:
- common/log_utils.py: add set_log_level() and get_log_levels()
functions
- admin/server/routes.py: add GET/PUT /api/v1/admin/log_levels endpoints
- api/apps/system_app.py: add GET/PUT /api/{version}/system/log_levels
endpoints
- internal/logger/logger.go: add GetLevel() and SetLevel() with atomic
level support
- internal/handler/system.go: add GetLogLevel, SetLogLevel, Health
handlers
- internal/router/router.go: route /health to systemHandler
- internal/admin/handler.go: add GetLogLevel, SetLogLevel handlers
- internal/admin/router.go: add /api/v1/admin/log_level routes

### What problem does this PR solve?

_Briefly describe what this PR aims to solve. Include background context
that will help reviewers understand the purpose of the PR._

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-30 18:40:58 +08:00

179 lines
4.2 KiB
Go

//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// 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 logger
import (
"fmt"
"runtime"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
Logger *zap.Logger
Sugar *zap.SugaredLogger
levelMu sync.RWMutex
atomicLevel zap.AtomicLevel
)
// Init initializes the global logger
// Note: This requires zap to be installed: go get go.uber.org/zap
func Init(level string) error {
// Parse log level
var zapLevel zapcore.Level
switch level {
case "debug":
zapLevel = zapcore.DebugLevel
case "info":
zapLevel = zapcore.InfoLevel
case "warn":
zapLevel = zapcore.WarnLevel
case "error":
zapLevel = zapcore.ErrorLevel
default:
zapLevel = zapcore.InfoLevel
}
// Create atomic level for dynamic updates
atomicLevel = zap.NewAtomicLevelAt(zapLevel)
// Custom encoder config to control output format
encoderConfig := zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "", // Disable caller/line number
FunctionKey: "",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05"), // Human-readable time format
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder, // Not used since CallerKey is empty
}
// Configure zap
config := zap.Config{
Level: atomicLevel,
Development: false,
Encoding: "console",
EncoderConfig: encoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
// Build logger
logger, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
return err
}
Logger = logger
Sugar = logger.Sugar()
return nil
}
// Sync flushes any buffered log entries
func Sync() {
if Logger != nil {
_ = Logger.Sync()
}
}
// Fatal logs a fatal message using zap with caller info
func Fatal(msg string, fields ...zap.Field) {
if Logger == nil {
panic("logger not initialized")
}
// Get caller info (skip this function to get the actual caller)
_, file, line, ok := runtime.Caller(1)
if ok {
fields = append(fields, zap.String("caller", fmt.Sprintf("%s:%d", file, line)))
}
Logger.Fatal(msg, fields...)
}
// Info logs an info message using zap or standard logger
func Info(msg string, fields ...zap.Field) {
if Logger == nil {
return
}
Logger.Info(msg, fields...)
}
// Error logs an error message using zap or standard logger
func Error(msg string, err error) {
if Logger == nil {
return
}
Logger.Error(msg, zap.Error(err))
}
// Debug logs a debug message using zap or standard logger
func Debug(msg string, fields ...zap.Field) {
if Logger == nil {
return
}
Logger.Debug(msg, fields...)
}
// Warn logs a warning message using zap or standard logger
func Warn(msg string, fields ...zap.Field) {
if Logger == nil {
return
}
Logger.Warn(msg, fields...)
}
// GetLevel returns the current log level
func GetLevel() string {
levelMu.RLock()
defer levelMu.RUnlock()
return atomicLevel.String()
}
// SetLevel sets the log level at runtime
func SetLevel(level string) error {
levelMu.Lock()
defer levelMu.Unlock()
var zapLevel zapcore.Level
switch level {
case "debug":
zapLevel = zapcore.DebugLevel
case "info":
zapLevel = zapcore.InfoLevel
case "warn", "warning":
zapLevel = zapcore.WarnLevel
case "error":
zapLevel = zapcore.ErrorLevel
case "fatal":
zapLevel = zapcore.FatalLevel
case "panic":
zapLevel = zapcore.PanicLevel
default:
return fmt.Errorf("unknown log level: %s", level)
}
atomicLevel.SetLevel(zapLevel)
return nil
}