2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01: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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-10-31 13:42:38 +01:00
|
|
|
package log
|
2018-09-19 08:35:53 +02:00
|
|
|
|
|
|
|
import (
|
2019-01-25 12:40:54 +01:00
|
|
|
"io"
|
2018-09-19 08:35:53 +02:00
|
|
|
"os"
|
2020-04-12 22:32:21 +02:00
|
|
|
"strings"
|
2019-01-25 12:40:54 +01:00
|
|
|
"time"
|
2020-10-11 22:10:03 +02:00
|
|
|
|
|
|
|
"code.vikunja.io/api/pkg/config"
|
|
|
|
"github.com/op/go-logging"
|
|
|
|
"github.com/spf13/viper"
|
2018-09-19 08:35:53 +02:00
|
|
|
)
|
|
|
|
|
2019-01-25 12:40:54 +01:00
|
|
|
// ErrFmt holds the format for all the console logging
|
|
|
|
const ErrFmt = `${time_rfc3339_nano}: ${level} ` + "\t" + `▶ ${prefix} ${short_file}:${line}`
|
|
|
|
|
|
|
|
// WebFmt holds the format for all logging related to web requests
|
|
|
|
const WebFmt = `${time_rfc3339_nano}: WEB ` + "\t" + `▶ ${remote_ip} ${id} ${method} ${status} ${uri} ${latency_human} - ${user_agent}`
|
|
|
|
|
|
|
|
// Fmt is the general log format
|
|
|
|
const Fmt = `%{color}%{time:` + time.RFC3339Nano + `}: %{level}` + "\t" + `▶ %{shortpkg}/%{shortfunc} %{id:03x}%{color:reset} %{message}`
|
|
|
|
|
2020-04-12 22:32:21 +02:00
|
|
|
const logModule = `vikunja`
|
|
|
|
|
2019-07-20 20:12:10 +02:00
|
|
|
// loginstance is the instance of the logger which is used under the hood to log
|
2020-04-12 22:32:21 +02:00
|
|
|
var logInstance = logging.MustGetLogger(logModule)
|
2018-09-19 08:35:53 +02:00
|
|
|
|
2018-09-20 07:31:36 +02:00
|
|
|
// InitLogger initializes the global log handler
|
2018-09-19 08:35:53 +02:00
|
|
|
func InitLogger() {
|
2019-07-06 22:12:26 +02:00
|
|
|
if !config.LogEnabled.GetBool() {
|
2019-01-25 12:40:54 +01:00
|
|
|
// Disable all logging when loggin in general is disabled, overwriting everything a user might have set.
|
2019-07-06 22:12:26 +02:00
|
|
|
config.LogStandard.Set("off")
|
|
|
|
config.LogDatabase.Set("off")
|
|
|
|
config.LogHTTP.Set("off")
|
|
|
|
config.LogEcho.Set("off")
|
2021-02-02 23:48:37 +01:00
|
|
|
config.LogEvents.Set("off")
|
2019-01-25 12:40:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-01 17:56:22 +02:00
|
|
|
// This show correct caller functions
|
|
|
|
logInstance.ExtraCalldepth = 1
|
|
|
|
|
2020-04-12 22:32:21 +02:00
|
|
|
if config.LogStandard.GetString() == "file" {
|
2019-07-06 22:12:26 +02:00
|
|
|
err := os.Mkdir(config.LogPath.GetString(), 0744)
|
2019-01-25 12:40:54 +01:00
|
|
|
if err != nil && !os.IsExist(err) {
|
2020-04-12 22:32:21 +02:00
|
|
|
Fatalf("Could not create log folder: %s", err.Error())
|
2019-01-25 12:40:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 17:42:32 +01:00
|
|
|
// The backend is the part which actually handles logging the log entries somewhere.
|
|
|
|
cf := config.LogStandard.GetString()
|
|
|
|
var backend logging.Backend
|
|
|
|
backend = &NoopBackend{}
|
|
|
|
if cf != "off" && cf != "false" {
|
2019-01-25 12:40:54 +01:00
|
|
|
stdWriter := GetLogWriter("standard")
|
|
|
|
|
2020-04-12 22:32:21 +02:00
|
|
|
logBackend := logging.NewLogBackend(stdWriter, "", 0)
|
2022-02-19 17:42:32 +01:00
|
|
|
backend = logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(Fmt+"\n"))
|
|
|
|
}
|
2019-01-25 12:40:54 +01:00
|
|
|
|
2022-02-19 17:42:32 +01:00
|
|
|
level, err := logging.LogLevel(strings.ToUpper(config.LogLevel.GetString()))
|
|
|
|
if err != nil {
|
|
|
|
Fatalf("Error setting database log level: %s", err.Error())
|
2020-04-12 22:32:21 +02:00
|
|
|
}
|
2022-02-19 17:42:32 +01:00
|
|
|
|
|
|
|
backendLeveled := logging.AddModuleLevel(backend)
|
|
|
|
backendLeveled.SetLevel(level, logModule)
|
|
|
|
|
|
|
|
logInstance.SetBackend(backendLeveled)
|
2019-01-25 12:40:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLogWriter returns the writer to where the normal log goes, depending on the config
|
|
|
|
func GetLogWriter(logfile string) (writer io.Writer) {
|
2019-09-01 18:23:35 +02:00
|
|
|
writer = os.Stdout // Set the default case to prevent nil pointer panics
|
2019-01-25 12:40:54 +01:00
|
|
|
switch viper.GetString("log." + logfile) {
|
|
|
|
case "file":
|
2020-04-12 22:32:21 +02:00
|
|
|
fullLogFilePath := config.LogPath.GetString() + "/" + logfile + ".log"
|
2020-04-13 22:30:09 +02:00
|
|
|
f, err := os.OpenFile(fullLogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
2019-01-25 12:40:54 +01:00
|
|
|
if err != nil {
|
2020-04-12 22:32:21 +02:00
|
|
|
Fatalf("Could not create logfile %s: %s", fullLogFilePath, err.Error())
|
2019-01-25 12:40:54 +01:00
|
|
|
}
|
|
|
|
writer = f
|
2019-01-25 21:09:24 +01:00
|
|
|
case "stderr":
|
|
|
|
writer = os.Stderr
|
2019-01-25 12:40:54 +01:00
|
|
|
case "stdout":
|
|
|
|
default:
|
|
|
|
writer = os.Stdout
|
|
|
|
}
|
|
|
|
return
|
2018-09-19 08:35:53 +02:00
|
|
|
}
|
2019-07-20 20:12:10 +02:00
|
|
|
|
|
|
|
// GetLogger returns the logging instance. DO NOT USE THIS TO LOG STUFF.
|
|
|
|
func GetLogger() *logging.Logger {
|
|
|
|
return logInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following functions are to be used as an "eye-candy", so one can just write log.Error() instead of log.Log.Error()
|
|
|
|
|
|
|
|
// Debug is for debug messages
|
|
|
|
func Debug(args ...interface{}) {
|
2020-04-12 22:32:21 +02:00
|
|
|
logInstance.Debug(args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf is for debug messages
|
|
|
|
func Debugf(format string, args ...interface{}) {
|
2019-07-21 23:27:30 +02:00
|
|
|
logInstance.Debugf(format, args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info is for info messages
|
|
|
|
func Info(args ...interface{}) {
|
2020-04-12 22:32:21 +02:00
|
|
|
logInstance.Info(args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Infof is for info messages
|
|
|
|
func Infof(format string, args ...interface{}) {
|
2019-07-21 23:27:30 +02:00
|
|
|
logInstance.Infof(format, args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error is for error messages
|
|
|
|
func Error(args ...interface{}) {
|
2020-04-12 22:32:21 +02:00
|
|
|
logInstance.Error(args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf is for error messages
|
|
|
|
func Errorf(format string, args ...interface{}) {
|
2019-07-21 23:27:30 +02:00
|
|
|
logInstance.Errorf(format, args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warning is for warning messages
|
|
|
|
func Warning(args ...interface{}) {
|
2020-04-12 22:32:21 +02:00
|
|
|
logInstance.Warning(args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warningf is for warning messages
|
|
|
|
func Warningf(format string, args ...interface{}) {
|
2019-07-21 23:27:30 +02:00
|
|
|
logInstance.Warningf(format, args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Critical is for critical messages
|
|
|
|
func Critical(args ...interface{}) {
|
2020-04-12 22:32:21 +02:00
|
|
|
logInstance.Critical(args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Criticalf is for critical messages
|
|
|
|
func Criticalf(format string, args ...interface{}) {
|
2019-07-21 23:27:30 +02:00
|
|
|
logInstance.Criticalf(format, args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal is for fatal messages
|
|
|
|
func Fatal(args ...interface{}) {
|
2020-04-12 22:32:21 +02:00
|
|
|
logInstance.Fatal(args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalf is for fatal messages
|
|
|
|
func Fatalf(format string, args ...interface{}) {
|
2019-07-21 23:27:30 +02:00
|
|
|
logInstance.Fatalf(format, args...)
|
2019-07-20 20:12:10 +02:00
|
|
|
}
|