2023-03-25 13:16:39 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2023-09-01 08:32:28 +02:00
|
|
|
// Copyright 2018-present Vikunja and contributors. All rights reserved.
|
2023-03-25 13:16:39 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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 Affero General Public Licensee for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2023-03-24 20:35:12 +01:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-03-24 23:13:09 +01:00
|
|
|
|
|
|
|
"github.com/op/go-logging"
|
2024-06-18 13:28:10 +02:00
|
|
|
"github.com/wneessen/go-mail/log"
|
2023-03-24 20:35:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type MailLogger struct {
|
|
|
|
logger *logging.Logger
|
2024-06-18 13:28:10 +02:00
|
|
|
level log.Level
|
2023-03-24 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const mailFormat = `%{color}%{time:` + time.RFC3339Nano + `}: %{level}` + "\t" + `▶ [MAIL] %{id:03x}%{color:reset} %{message}`
|
|
|
|
const mailLogModule = `vikunja_mail`
|
|
|
|
|
2023-10-03 09:28:28 +00:00
|
|
|
// NewMailLogger creates and initializes a new mail logger
|
2024-06-18 13:28:10 +02:00
|
|
|
func NewMailLogger(configLogEnabled bool, configLogMail string, configLogMailLevel string) log.Logger {
|
2023-10-03 09:28:28 +00:00
|
|
|
lvl := strings.ToUpper(configLogMailLevel)
|
2023-03-24 20:35:12 +01:00
|
|
|
level, err := logging.LogLevel(lvl)
|
|
|
|
if err != nil {
|
2023-10-03 09:28:28 +00:00
|
|
|
Criticalf("Error setting mail log level %s: %s", lvl, err.Error())
|
2023-03-24 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mailLogger := &MailLogger{
|
|
|
|
logger: logging.MustGetLogger(mailLogModule),
|
|
|
|
}
|
|
|
|
|
2023-10-03 09:28:28 +00:00
|
|
|
var backend logging.Backend
|
|
|
|
backend = &NoopBackend{}
|
|
|
|
if configLogEnabled && configLogMail != "off" {
|
|
|
|
logBackend := logging.NewLogBackend(GetLogWriter(configLogMail, "mail"), "", 0)
|
|
|
|
backend = logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(mailFormat+"\n"))
|
|
|
|
}
|
2023-03-24 20:35:12 +01:00
|
|
|
|
|
|
|
backendLeveled := logging.AddModuleLevel(backend)
|
|
|
|
backendLeveled.SetLevel(level, mailLogModule)
|
|
|
|
|
|
|
|
mailLogger.logger.SetBackend(backendLeveled)
|
|
|
|
|
|
|
|
switch level {
|
|
|
|
case logging.CRITICAL:
|
|
|
|
case logging.ERROR:
|
2024-06-18 13:28:10 +02:00
|
|
|
mailLogger.level = log.LevelError
|
2023-03-24 20:35:12 +01:00
|
|
|
case logging.WARNING:
|
2024-06-18 13:28:10 +02:00
|
|
|
mailLogger.level = log.LevelWarn
|
2023-03-24 20:35:12 +01:00
|
|
|
case logging.NOTICE:
|
|
|
|
case logging.INFO:
|
2024-06-18 13:28:10 +02:00
|
|
|
mailLogger.level = log.LevelInfo
|
2023-03-24 20:35:12 +01:00
|
|
|
case logging.DEBUG:
|
2024-06-18 13:28:10 +02:00
|
|
|
mailLogger.level = log.LevelDebug
|
2023-03-24 20:35:12 +01:00
|
|
|
default:
|
2024-06-18 13:28:10 +02:00
|
|
|
mailLogger.level = 0
|
2023-03-24 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return mailLogger
|
|
|
|
}
|
|
|
|
|
2024-06-18 13:28:10 +02:00
|
|
|
func (m *MailLogger) Debugf(l log.Log) {
|
|
|
|
m.logger.Debugf(l.Format, l.Messages...)
|
2023-03-24 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
2024-06-18 13:28:10 +02:00
|
|
|
func (m *MailLogger) Infof(l log.Log) {
|
|
|
|
m.logger.Infof(l.Format, l.Messages...)
|
2023-03-24 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
2024-06-18 13:28:10 +02:00
|
|
|
func (m *MailLogger) Warnf(l log.Log) {
|
|
|
|
m.logger.Warningf(l.Format, l.Messages...)
|
2023-03-24 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
2024-06-18 13:28:10 +02:00
|
|
|
func (m *MailLogger) Errorf(l log.Log) {
|
|
|
|
m.logger.Errorf(l.Format, l.Messages...)
|
2023-03-24 20:35:12 +01:00
|
|
|
}
|