AFAIRE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.8 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### logger ####### Copyright (c) 2021-2022 losyme ############################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package logger
import (
"sync/atomic"
"time"
"forge.chapril.org/losyme/errors"
)
type Logger struct {
id string
name string
label string
level Level
output Output
}
func New(id, name, level string, output Output) (*Logger, error) {
if id == "" || name == "" {
return nil, errors.New("id, name and level cannot be empty") ///////////////////////////////////////////////////
}
if output == nil {
return nil, errors.New("output cannot be nil") /////////////////////////////////////////////////////////////////
}
l := &Logger{
id: id,
name: name,
label: loggerLabel(id, name),
level: StringToLevel(level),
output: output,
}
if err := _manager.register(l); err != nil {
return nil, err
}
return l, nil
}
func (l *Logger) ID() string {
return l.id
}
func (l *Logger) Name() string {
return l.name
}
func (l *Logger) Label() string {
return l.label
}
func (l *Logger) loadLevel() Level {
return Level(atomic.LoadUint32((*uint32)(&l.level)))
}
func (l *Logger) storeLevel(level Level) {
atomic.StoreUint32((*uint32)(&l.level), uint32(level))
}
func (l *Logger) Level() string {
return l.loadLevel().String()
}
func (l *Logger) SetLevel(level string) {
l.storeLevel(StringToLevel(level))
}
func (l *Logger) NewLogger(id, name string) (*Logger, error) {
return New(id, name, l.loadLevel().String(), l.output)
}
func (l *Logger) Unregister() {
_manager.unregister(l.id)
}
func (l *Logger) Log(level Level, msg string, kv ...interface{}) {
if level < l.loadLevel() {
return
}
log := _logPool.Get()
log.Time = time.Now()
log.Logger = l
log.Level = level
log.Msg = msg
log.KV = append(log.KV, kv...)
_manager.logHandler(log)
}
func (l *Logger) Trace(msg string, kv ...interface{}) {
l.Log(LevelTrace, msg, kv...)
}
func (l *Logger) Debug(msg string, kv ...interface{}) {
l.Log(LevelDebug, msg, kv...)
}
func (l *Logger) Info(msg string, kv ...interface{}) {
l.Log(LevelInfo, msg, kv...)
}
func (l *Logger) Notice(msg string, kv ...interface{}) {
l.Log(LevelNotice, msg, kv...)
}
func (l *Logger) Warning(msg string, kv ...interface{}) {
l.Log(LevelWarning, msg, kv...)
}
func (l *Logger) Error(msg string, kv ...interface{}) {
l.Log(LevelError, msg, kv...)
}
func (l *Logger) Fatal(msg string, kv ...interface{}) {
l.Log(LevelFatal, msg, kv...)
}
/*
######################################################################################################## @(°_°)@ #######
*/