yao/agent/robot/logger/logger.go
Max ea9e070f29 Enhance robot integration with Telegram and improve event handling
- Add Telegram integration support by introducing a dispatcher for handling Telegram events and messages.
- Implement event notifications for robot configuration changes (creation, update, deletion) to facilitate integration with external services.
- Refactor the robot initialization process to load robots into cache and start the dispatcher, improving the overall system setup.
- Update the delivery event structure to include additional metadata for better context during message handling.
- Enhance logging capabilities for better observability during robot execution and event processing.
2026-03-01 22:03:25 +08:00

100 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logger
import (
"fmt"
kunlog "github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config"
)
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Cyan = "\033[36m"
White = "\033[37m"
Gray = "\033[90m"
BoldCyan = "\033[1;36m"
BoldGreen = "\033[1;32m"
BoldRed = "\033[1;31m"
reset = Reset
red = Red
yellow = Yellow
cyan = Cyan
gray = Gray
)
// Logger provides robot-level structured logging. All integration adapters,
// dispatchers, event handlers, etc. share this implementation.
//
// Dev mode → colored stdout + kun/log.Trace (unified).
// Prod mode → kun/log at matching level.
type Logger struct {
tag string
}
// New creates a Logger tagged with the given component name
// (e.g. "telegram", "dispatcher", "message", "delivery").
func New(tag string) *Logger {
return &Logger{tag: tag}
}
func (l *Logger) prefix() string {
return fmt.Sprintf("[robot:%s]", l.tag)
}
func (l *Logger) Trace(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
if config.IsDevelopment() {
fmt.Printf("%s → %s %s%s\n", gray, l.prefix(), msg, reset)
}
kunlog.Trace("%s %s", l.prefix(), msg)
}
func (l *Logger) Debug(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
if config.IsDevelopment() {
fmt.Printf("%s • %s %s%s\n", gray, l.prefix(), msg, reset)
}
kunlog.Debug("%s %s", l.prefix(), msg)
}
func (l *Logger) Info(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
if config.IsDevelopment() {
fmt.Printf("%s %s %s%s\n", cyan, l.prefix(), msg, reset)
}
kunlog.Info("%s %s", l.prefix(), msg)
}
func (l *Logger) Warn(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
if config.IsDevelopment() {
fmt.Printf("%s ⚠ %s %s%s\n", yellow, l.prefix(), msg, reset)
}
kunlog.Warn("%s %s", l.prefix(), msg)
}
func (l *Logger) Error(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
if config.IsDevelopment() {
fmt.Printf("%s ✗ %s %s%s\n", red, l.prefix(), msg, reset)
}
kunlog.Error("%s %s", l.prefix(), msg)
}
// IsDev returns true when running in development mode.
func IsDev() bool {
return config.IsDevelopment()
}
// Raw writes pre-formatted text directly to stdout in dev mode only.
// Use for rich multi-line output (box-style logs, tables, etc.)
// that should bypass the standard single-line prefix format.
func Raw(s string) {
if config.IsDevelopment() {
fmt.Print(s)
}
}