This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
generated from benjivesterby/go-template-repo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destination.go
119 lines (107 loc) · 2.49 KB
/
destination.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright © 2019 Developer Network, LLC
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
package alog
import (
"context"
"errors"
"io"
"os"
"testing"
)
// Destination is the destination struct for registering io.Writers to the
// alog library so that different log types can be passed to each writer
// asynchronously
type Destination struct {
Types LogLevel
Format LogFmt
Writer io.Writer
}
// Standards returns the standard out and standard error destinations for
// quick access when creating a logger
// INFO, DEBUG, TRACE, WARNING, CUSTOM Logs are logged to Standard Out
// ERROR, CRITICAL, FATAL Logs are logged to Standard Error
func Standards() []Destination {
return []Destination{
{
INFO | DEBUG | TRACE | WARN | CUSTOM,
STD,
os.Stdout,
},
{
ERROR | CRIT | FATAL,
STD,
os.Stderr,
},
}
}
// TestDestinations returns a list of destinations for logging test data
// to the *testing.T that was passed in. This defaults all ERROR, CRIT, FATAL
// logs to the t.Error and the rest are routed to t.Log. These destinations
// can be used to override the logger with destinations specific to testing.
func TestDestinations(ctx context.Context, t *testing.T) []Destination {
return []Destination{
{
INFO | DEBUG | TRACE | WARN | CUSTOM,
STD,
test{
ctx: ctx,
t: t,
mode: INFO | DEBUG | TRACE | WARN | CUSTOM,
},
},
{
ERROR | CRIT | FATAL,
STD,
test{
ctx: ctx,
t: t,
mode: ERROR | CRIT | FATAL,
},
},
}
}
type test struct {
ctx context.Context
t *testing.T
mode LogLevel
}
func (t test) Write(p []byte) (int, error) {
if t.t == nil {
return 0, errors.New("invalid test object")
}
select {
case <-t.ctx.Done():
return 0, nil
default:
msg := string(p)
if t.mode&FATAL > 0 {
t.t.Fatal(msg)
} else if t.mode&(ERROR|CRIT) > 0 {
t.t.Error(msg)
} else {
t.t.Log(msg)
}
}
return len(p), nil
}
// BenchDestinations returns a list of destinations for benchmarking.
// The destination returned from this method does NOTHING. It is meant
// to remove overhead from the logger for proper benchmarks.
// This destination can be used to override the logger for benchmarking.
func BenchDestinations() []Destination {
return []Destination{
{
INFO | DEBUG | TRACE | WARN |
CUSTOM | ERROR | CRIT | FATAL,
STD,
bench{},
},
}
}
type bench struct {
}
func (bench) Write(p []byte) (int, error) {
return len(p), nil
}