// Copyright 2023 Martin Riedl // // 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 golog import "time" var Default = NewLoggerDefault() type Logger struct { Level Level Outputs []*Output } func NewLogger(level Level, outputs []*Output) *Logger { return &Logger{ Level: LevelInfo, Outputs: outputs, } } func NewLoggerDefault() *Logger { return NewLogger(LevelInfo, []*Output{NewOutputDefault()}) } func (logger *Logger) LogEntry(entry *Entry) { // check, if message should be printed if entry.Level > logger.Level { return } // set execution time entry.Time = time.Now() // run each output for _, output := range logger.Outputs { output.Send(entry) } } func (logger *Logger) Log(level Level, args ...any) { entry := NewEntry(logger) entry.Log(level, args...) } func (logger *Logger) With(key string, value any) *Entry { entry := NewEntry(logger) entry.With(key, value) return entry } func (logger *Logger) WithContent(content []Content) *Entry { entry := NewEntry(logger) entry.WithContent(content) return entry } func (logger *Logger) Fatal(args ...any) { logger.Log(LevelFatal, args...) } func (logger *Logger) Error(args ...any) { logger.Log(LevelError, args...) } func (logger *Logger) Warning(args ...any) { logger.Log(LevelWarning, args...) } func (logger *Logger) Info(args ...any) { logger.Log(LevelInfo, args...) } func (logger *Logger) Debug(args ...any) { logger.Log(LevelDebug, args...) } func (logger *Logger) Trace(args ...any) { logger.Log(LevelTrace, args...) }