mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
116 lines
3.6 KiB
Go
116 lines
3.6 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. 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 log is the leveled logging surface for the ai runtime. Records
|
|
// emit through luxfi/log to structured stderr, where the unified cloud
|
|
// process (HIP-0106) has its ZAP/o11y pipeline collect them.
|
|
//
|
|
// Info/Warning/Warn/Error take a leading format value plus optional args:
|
|
// a string containing a format verb is used as a fmt format string; any
|
|
// other leading value, or trailing args when the string has no verb, are
|
|
// rendered and appended as " %v" fields. That formatting contract lets the
|
|
// call sites read and format exactly as before.
|
|
package log
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
luxlog "github.com/luxfi/log"
|
|
)
|
|
|
|
// Info logs at info level.
|
|
func Info(f any, v ...any) { luxlog.Info(format(f, v...)) }
|
|
|
|
// Warning logs at warn level.
|
|
func Warning(f any, v ...any) { luxlog.Warn(format(f, v...)) }
|
|
|
|
// Warn logs at warn level.
|
|
func Warn(f any, v ...any) { luxlog.Warn(format(f, v...)) }
|
|
|
|
// Error logs at error level.
|
|
func Error(f any, v ...any) { luxlog.Error(format(f, v...)) }
|
|
|
|
// SetLogger applies the process log level from a JSON config's optional
|
|
// "level" field. The runtime emits structured records to stderr for the
|
|
// platform to collect (HIP-0106); the historical adapter names ("file",
|
|
// "console") denote that single sink and carry no per-adapter wiring.
|
|
func SetLogger(adapter string, config ...string) error {
|
|
for _, c := range config {
|
|
var m map[string]any
|
|
if json.Unmarshal([]byte(c), &m) != nil {
|
|
continue
|
|
}
|
|
if l, ok := parseLevel(m["level"]); ok {
|
|
luxlog.SetGlobalLevel(l)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// parseLevel resolves a config "level" to a luxfi/log level. It accepts a
|
|
// level name ("debug", "error", …) or the historical numeric scale where a
|
|
// larger number is more verbose (7 debug, 6 info, 4 warning, 3 error); JSON
|
|
// decodes those numbers as float64. Values below error clamp to error so a
|
|
// numeric threshold never suppresses error records.
|
|
func parseLevel(v any) (luxlog.Level, bool) {
|
|
switch t := v.(type) {
|
|
case string:
|
|
if l, err := luxlog.ParseLevel(t); err == nil {
|
|
return l, true
|
|
}
|
|
case float64:
|
|
switch {
|
|
case t >= 7:
|
|
return luxlog.DebugLevel, true
|
|
case t >= 5:
|
|
return luxlog.InfoLevel, true
|
|
case t >= 4:
|
|
return luxlog.WarnLevel, true
|
|
default:
|
|
return luxlog.ErrorLevel, true
|
|
}
|
|
}
|
|
return luxlog.InfoLevel, false
|
|
}
|
|
|
|
// Reset restores the default (info) log level.
|
|
func Reset() { luxlog.SetGlobalLevel(luxlog.InfoLevel) }
|
|
|
|
// format renders the leading value and args into a single message under the
|
|
// printf-plus-appended-fields contract the call sites rely on.
|
|
func format(f any, v ...any) string {
|
|
var msg string
|
|
switch t := f.(type) {
|
|
case string:
|
|
msg = t
|
|
if len(v) == 0 {
|
|
return msg
|
|
}
|
|
// A string with a verb is a fmt format; otherwise append the args
|
|
// as " %v" fields.
|
|
if !(strings.Contains(msg, "%") && !strings.Contains(msg, "%%")) {
|
|
msg += strings.Repeat(" %v", len(v))
|
|
}
|
|
default:
|
|
msg = fmt.Sprint(f)
|
|
if len(v) == 0 {
|
|
return msg
|
|
}
|
|
msg += strings.Repeat(" %v", len(v))
|
|
}
|
|
return fmt.Sprintf(msg, v...)
|
|
}
|