-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
96 lines (79 loc) · 2.47 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (C) 2023 Shanhu Tech Inc.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License 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 License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package aries
import (
"errors"
"fmt"
"log"
"os"
"shanhu.io/g/errcode"
)
// LogPrinter is the interface for printing server logs.
type LogPrinter interface {
Print(s string)
}
// Logger is a logger for logging server logs
type Logger struct {
p LogPrinter
}
// NewLogger creates a new logger that prints server
// logs to the given printer.
func NewLogger(p LogPrinter) *Logger {
return &Logger{p: p}
}
type stdLog struct{}
func (*stdLog) Print(s string) { log.Println(s) }
var theStdLog = new(stdLog)
// StdLogger returns the logger that logs to the default std log.
func StdLogger() *Logger {
return &Logger{p: theStdLog}
}
// AltError logs the error and returns an alternative error with code.
func (l *Logger) AltError(err error, code, s string) error {
if err == nil {
return nil
}
l.p.Print(fmt.Sprintf("%s: %s", s, err))
return errcode.Add(code, errors.New(s))
}
// AltInternal logs the error and returns an alternative internal error.
func (l *Logger) AltInternal(err error, s string) error {
return l.AltError(err, errcode.Internal, s)
}
// AltInvalidArg logs the error and returns an alternative invalid arg error.
func (l *Logger) AltInvalidArg(err error, s string) error {
return l.AltError(err, errcode.InvalidArg, s)
}
// Exit prints the error and exit the service.
func (l *Logger) Exit(err error) error {
l.p.Print(err.Error())
os.Exit(1)
panic("unreachable")
}
// Print prints a message to the logger.
func (l *Logger) Print(args ...interface{}) {
l.p.Print(fmt.Sprint(args...))
}
// Printf prints a formatted message to the logger.
func (l *Logger) Printf(f string, args ...interface{}) {
l.p.Print(fmt.Sprintf(f, args...))
}
// Log logs the message to the logger if the logger is not nil.
func Log(l *Logger, s string) {
if l == nil {
return
}
l.Print(s)
}