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

160 lines
4.5 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
2018-07-06 13:08:50 +00:00
import (
"fmt"
"os"
"time"
)
2018-07-06 13:31:42 +00:00
// LoggerFileFormat ...
type LoggerFileFormat func(now string, i int) string
2018-09-19 22:28:37 +00:00
type BasicLogger struct {
2018-07-06 13:31:42 +00:00
PrintLevel int
FlushLineThreshold int
FileTimeFormat string
FileFormat LoggerFileFormat
TimeFormat string
FileMode os.FileMode
2018-09-19 22:28:37 +00:00
DefaultSub Logger
2018-07-06 13:31:42 +00:00
writer *os.File
lines int
prefixPrinted bool
}
2018-09-19 22:28:37 +00:00
// Logger contains advanced logging functions and also implements io.Writer
type Logger interface {
Sub(module string) Logger
GetParent() Logger
2018-07-06 13:31:42 +00:00
Write(p []byte) (n int, err error)
2018-09-19 22:28:37 +00:00
2018-07-06 13:31:42 +00:00
Log(level Level, parts ...interface{})
Logln(level Level, parts ...interface{})
Logf(level Level, message string, args ...interface{})
2018-09-19 22:28:37 +00:00
Logfln(level Level, message string, args ...interface{})
2018-07-06 13:31:42 +00:00
Debug(parts ...interface{})
Debugln(parts ...interface{})
Debugf(message string, args ...interface{})
2018-09-19 22:28:37 +00:00
Debugfln(message string, args ...interface{})
2018-07-06 13:31:42 +00:00
Info(parts ...interface{})
Infoln(parts ...interface{})
Infof(message string, args ...interface{})
2018-09-19 22:28:37 +00:00
Infofln(message string, args ...interface{})
2018-07-06 13:31:42 +00:00
Warn(parts ...interface{})
Warnln(parts ...interface{})
Warnf(message string, args ...interface{})
2018-09-19 22:28:37 +00:00
Warnfln(message string, args ...interface{})
2018-07-06 13:31:42 +00:00
Error(parts ...interface{})
Errorln(parts ...interface{})
Errorf(message string, args ...interface{})
2018-09-19 22:28:37 +00:00
Errorfln(message string, args ...interface{})
2018-07-06 13:31:42 +00:00
Fatal(parts ...interface{})
Fatalln(parts ...interface{})
Fatalf(message string, args ...interface{})
2018-09-19 22:28:37 +00:00
Fatalfln(message string, args ...interface{})
2018-07-06 13:31:42 +00:00
}
// Create a Logger
2018-09-19 22:28:37 +00:00
func Create() Logger {
var log = &BasicLogger{
2018-07-06 13:31:42 +00:00
PrintLevel: 10,
FileTimeFormat: "2006-01-02",
FileFormat: func(now string, i int) string { return fmt.Sprintf("%[1]s-%02[2]d.log", now, i) },
TimeFormat: "15:04:05 02.01.2006",
FileMode: 0600,
FlushLineThreshold: 5,
lines: 0,
2018-07-06 13:08:50 +00:00
}
2018-09-19 22:28:37 +00:00
log.DefaultSub = log.Sub("")
2018-07-06 13:31:42 +00:00
return log
2018-07-06 13:08:50 +00:00
}
2018-09-19 22:28:37 +00:00
func (log *BasicLogger) GetParent() Logger {
return nil
}
// SetWriter formats the given parts with fmt.Sprint and logs the result with the SetWriter level
func (log *BasicLogger) SetWriter(w *os.File) {
2018-07-06 13:31:42 +00:00
log.writer = w
2018-07-06 13:08:50 +00:00
}
2018-09-19 22:28:37 +00:00
// OpenFile formats the given parts with fmt.Sprint and logs the result with the OpenFile level
func (log *BasicLogger) OpenFile() error {
2018-07-06 13:31:42 +00:00
now := time.Now().Format(log.FileTimeFormat)
2018-07-06 13:08:50 +00:00
i := 1
for ; ; i++ {
2018-07-06 13:31:42 +00:00
if _, err := os.Stat(log.FileFormat(now, i)); os.IsNotExist(err) {
2018-07-06 13:08:50 +00:00
break
2018-07-06 13:31:42 +00:00
} else if i == 99 {
2018-07-06 13:08:50 +00:00
i = 1
break
}
}
2018-07-06 13:31:42 +00:00
var err error
log.writer, err = os.OpenFile(log.FileFormat(now, i), os.O_WRONLY|os.O_CREATE|os.O_APPEND, log.FileMode)
2018-07-06 13:08:50 +00:00
if err != nil {
2018-07-06 13:31:42 +00:00
return err
} else if log.writer == nil {
return os.ErrInvalid
2018-07-06 13:08:50 +00:00
}
2018-07-06 13:31:42 +00:00
return nil
2018-07-06 13:08:50 +00:00
}
2018-09-19 22:28:37 +00:00
// Close formats the given parts with fmt.Sprint and logs the result with the Close level
func (log *BasicLogger) Close() {
2018-07-06 13:31:42 +00:00
if log.writer != nil {
log.writer.Close()
}
2018-07-06 13:08:50 +00:00
}
2018-09-19 22:28:37 +00:00
// Raw formats the given parts with fmt.Sprint and logs the result with the Raw level
func (log *BasicLogger) Raw(level Level, module, message string) {
2018-07-06 13:31:42 +00:00
if !log.prefixPrinted {
if len(module) == 0 {
message = fmt.Sprintf("[%s] [%s] %s", time.Now().Format(log.TimeFormat), level.Name, message)
} else {
message = fmt.Sprintf("[%s] [%s/%s] %s", time.Now().Format(log.TimeFormat), module, level.Name, message)
}
}
2018-07-06 13:08:50 +00:00
2018-07-06 13:31:42 +00:00
log.prefixPrinted = message[len(message)-1] != '\n'
2018-07-06 13:08:50 +00:00
2018-07-06 13:31:42 +00:00
if log.writer != nil {
_, err := log.writer.WriteString(message)
2018-07-06 13:08:50 +00:00
if err != nil {
2018-07-06 13:31:42 +00:00
fmt.Println("Failed to write to log file:", err)
2018-07-06 13:08:50 +00:00
}
}
2018-07-06 13:31:42 +00:00
if level.Severity >= log.PrintLevel {
if level.Severity >= LevelError.Severity {
2018-07-06 13:08:50 +00:00
os.Stderr.Write(level.GetColor())
2018-07-06 13:31:42 +00:00
os.Stderr.WriteString(message)
2018-07-06 13:08:50 +00:00
os.Stderr.Write(level.GetReset())
} else {
os.Stdout.Write(level.GetColor())
2018-07-06 13:31:42 +00:00
os.Stdout.WriteString(message)
2018-07-06 13:08:50 +00:00
os.Stdout.Write(level.GetReset())
}
}
}