maubot/vendor/maunium.net/go/maulogger/sublogger.go

194 lines
7.4 KiB
Go
Raw Normal View History

2018-07-06 13:31:42 +00:00
// mauLogger - A logger for Go programs
2018-09-19 22:28:37 +00:00
// Copyright (C) 2016-2018 Tulir Asokan
2018-07-06 13:31:42 +00:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2018-09-19 22:28:37 +00:00
//
2018-07-06 13:31:42 +00:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2018-09-19 22:28:37 +00:00
//
2018-07-06 13:31:42 +00:00
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package maulogger
import (
"fmt"
)
type Sublogger struct {
2018-09-19 22:28:37 +00:00
topLevel *BasicLogger
parent Logger
2018-07-06 13:31:42 +00:00
Module string
DefaultLevel Level
}
2018-09-19 22:28:37 +00:00
// Sub creates a Sublogger
func (log *BasicLogger) Sub(module string) Logger {
2018-07-06 13:31:42 +00:00
return &Sublogger{
2018-09-19 22:28:37 +00:00
topLevel: log,
parent: log,
2018-07-06 13:31:42 +00:00
Module: module,
2018-09-19 22:28:37 +00:00
DefaultLevel: LevelInfo,
}
}
func (log *Sublogger) GetParent() Logger {
return log.parent
}
// Sub creates a Sublogger
func (log *Sublogger) Sub(module string) Logger {
return &Sublogger{
topLevel: log.topLevel,
parent: log,
Module: fmt.Sprintf("%s/%s", log.Module, module),
DefaultLevel: log.DefaultLevel,
2018-07-06 13:31:42 +00:00
}
}
// SetModule changes the module name of this Sublogger
func (log *Sublogger) SetModule(mod string) {
log.Module = mod
}
// SetDefaultLevel changes the default logging level of this Sublogger
func (log *Sublogger) SetDefaultLevel(lvl Level) {
log.DefaultLevel = lvl
}
// SetParent changes the parent of this Sublogger
2018-09-19 22:28:37 +00:00
func (log *Sublogger) SetParent(parent *BasicLogger) {
log.topLevel = parent
2018-07-06 13:31:42 +00:00
}
//Write ...
func (log *Sublogger) Write(p []byte) (n int, err error) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(log.DefaultLevel, log.Module, string(p))
2018-07-06 13:31:42 +00:00
return len(p), nil
}
2018-09-19 22:28:37 +00:00
// Log formats the given parts with fmt.Sprint and logs the result with the given level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Log(level Level, parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(level, "", fmt.Sprint(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Logln formats the given parts with fmt.Sprintln and logs the result with the given level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Logln(level Level, parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(level, "", fmt.Sprintln(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Logf formats the given message and args with fmt.Sprintf and logs the result with the given level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Logf(level Level, message string, args ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(level, "", fmt.Sprintf(message, args...))
}
// Logfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the given level
func (log *Sublogger) Logfln(level Level, message string, args ...interface{}) {
log.topLevel.Raw(level, log.Module, fmt.Sprintf(message+"\n", args...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Debug formats the given parts with fmt.Sprint and logs the result with the Debug level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Debug(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelDebug, log.Module, fmt.Sprint(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Debugln formats the given parts with fmt.Sprintln and logs the result with the Debug level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Debugln(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelDebug, log.Module, fmt.Sprintln(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Debugf formats the given message and args with fmt.Sprintf and logs the result with the Debug level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Debugf(message string, args ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelDebug, log.Module, fmt.Sprintf(message, args...))
}
// Debugfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Debug level
func (log *Sublogger) Debugfln(message string, args ...interface{}) {
log.topLevel.Raw(LevelDebug, log.Module, fmt.Sprintf(message+"\n", args...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Info formats the given parts with fmt.Sprint and logs the result with the Info level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Info(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelInfo, log.Module, fmt.Sprint(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Infoln formats the given parts with fmt.Sprintln and logs the result with the Info level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Infoln(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelInfo, log.Module, fmt.Sprintln(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Infof formats the given message and args with fmt.Sprintf and logs the result with the Info level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Infof(message string, args ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelInfo, log.Module, fmt.Sprintf(message, args...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Infofln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Info level
func (log *Sublogger) Infofln(message string, args ...interface{}) {
log.topLevel.Raw(LevelInfo, log.Module, fmt.Sprintf(message+"\n", args...))
}
// Warn formats the given parts with fmt.Sprint and logs the result with the Warn level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Warn(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelWarn, log.Module, fmt.Sprint(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Warnln formats the given parts with fmt.Sprintln and logs the result with the Warn level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Warnln(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelWarn, log.Module, fmt.Sprintln(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Warnf formats the given message and args with fmt.Sprintf and logs the result with the Warn level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Warnf(message string, args ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelWarn, log.Module, fmt.Sprintf(message, args...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Warnfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Warn level
func (log *Sublogger) Warnfln(message string, args ...interface{}) {
log.topLevel.Raw(LevelWarn, log.Module, fmt.Sprintf(message+"\n", args...))
}
// Error formats the given parts with fmt.Sprint and logs the result with the Error level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Error(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelError, log.Module, fmt.Sprint(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Errorln formats the given parts with fmt.Sprintln and logs the result with the Error level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Errorln(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelError, log.Module, fmt.Sprintln(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Errorf formats the given message and args with fmt.Sprintf and logs the result with the Error level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Errorf(message string, args ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelError, log.Module, fmt.Sprintf(message, args...))
}
// Errorfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Error level
func (log *Sublogger) Errorfln(message string, args ...interface{}) {
log.topLevel.Raw(LevelError, log.Module, fmt.Sprintf(message+"\n", args...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Fatal formats the given parts with fmt.Sprint and logs the result with the Fatal level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Fatal(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelFatal, log.Module, fmt.Sprint(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Fatalln formats the given parts with fmt.Sprintln and logs the result with the Fatal level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Fatalln(parts ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelFatal, log.Module, fmt.Sprintln(parts...))
2018-07-06 13:31:42 +00:00
}
2018-09-19 22:28:37 +00:00
// Fatalf formats the given message and args with fmt.Sprintf and logs the result with the Fatal level
2018-07-06 13:31:42 +00:00
func (log *Sublogger) Fatalf(message string, args ...interface{}) {
2018-09-19 22:28:37 +00:00
log.topLevel.Raw(LevelFatal, log.Module, fmt.Sprintf(message, args...))
}
// Fatalfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Fatal level
func (log *Sublogger) Fatalfln(message string, args ...interface{}) {
log.topLevel.Raw(LevelFatal, log.Module, fmt.Sprintf(message+"\n", args...))
2018-07-06 13:31:42 +00:00
}