-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannel.go
132 lines (108 loc) · 2.53 KB
/
channel.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
120
121
122
123
124
125
126
127
128
129
130
131
132
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"
import (
"io"
"log"
"time"
"github.com/nathanaelle/syslog5424/v2/sdata"
)
var (
// Now permit to change the local default alias function for time.Now(). only usefull in case of test or debug
Now = time.Now
)
type (
// Channel interface describe the common API of any logging channel.
// a logging channel is a context set for any log message or structured data
Channel interface {
io.Writer
IsDevNull() bool
AppName(string) Channel
Msgid(string) Channel
Logger(string) *log.Logger
Log(string, ...sdata.StructuredData)
}
// /dev/null Channel
devnull struct {
}
// a msgChannel describe a channel with a MsgID already set
msgChannel struct {
devnull
priority Priority
hostname string
pid string
appname string
msgid string
output *Sender
}
//
trueChannel struct {
msgChannel
}
)
func (d *trueChannel) AppName(sup string) Channel {
var appname string
switch string(d.appname) {
case "-":
appname = sup
default:
appname = d.appname + "/" + sup
}
return &trueChannel{msgChannel{
priority: d.priority,
hostname: d.hostname,
pid: d.pid,
appname: validApp(appname),
msgid: d.msgid,
output: d.output,
}}
}
func (d *trueChannel) Msgid(msgid string) Channel {
return &msgChannel{
priority: d.priority,
hostname: d.hostname,
pid: d.pid,
appname: d.appname,
msgid: validMsgid(msgid),
output: d.output,
}
}
func (c *msgChannel) Logger(prefix string) *log.Logger {
switch c.priority.Severity() {
case LogDEBUG:
return log.New(c, prefix, log.Lshortfile)
default:
return log.New(c, prefix, 0)
}
}
func (c *msgChannel) IsDevNull() bool {
return false
}
func (c *msgChannel) Write(d []byte) (int, error) {
c.output.Send(forgeMessage(c.priority, Now(), c.hostname, c.appname, c.pid, c.msgid, string(d)))
return len(d), nil
}
func (c *msgChannel) Log(d string, sd ...sdata.StructuredData) {
msg := forgeMessage(c.priority, Now(), c.hostname, c.appname, c.pid, c.msgid, string(d))
if len(sd) > 0 {
msg = msg.StructuredData(sd...)
}
c.output.Send(msg)
}
// /dev/null Logger
// Nothing is logged
func (dn *devnull) Logger(prefix string) *log.Logger {
return log.New(dn, prefix, 0)
}
func (dn *devnull) IsDevNull() bool {
return true
}
func (dn *devnull) AppName(string) Channel {
return dn
}
func (dn *devnull) Msgid(string) Channel {
return dn
}
func (dn *devnull) Write(d []byte) (int, error) {
return len(d), nil
}
func (dn *devnull) Log(_ string, _ ...sdata.StructuredData) {
}