-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogentries.go
143 lines (121 loc) · 2.92 KB
/
logentries.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
133
134
135
136
137
138
139
140
141
142
143
// Go support for logging to Logentries, http://logentries.com
// Version 0.2.1
//
// Copyright 2014 Robert Kaufmann III
package logentries
import (
"crypto/tls"
"errors"
"log"
"net"
)
// Log levels
type Level int
const (
Debug Level = iota
Info
Warn
Error
Panic
)
// Logentires endpoints
const (
tcpEndpoint string = "data.logentries.com:80"
sslEndpoint string = "api.logentries.com:20000"
)
// Default tags for log levels
var levelTag = map[Level]string{
Debug: "DEBUG",
Info: "INFO",
Warn: "WARNING",
Error: "ERROR",
Panic: "PANIC",
}
// Log Client
type Logger struct {
localLogger *log.Logger
connection net.Conn
token string
verbosity Level
}
// New creates a new logging client with supplied token
func New(token string, ssl bool) (*Logger, error) {
// Setup the connection
var conn net.Conn
var err error
if ssl {
conn, err = tls.Dial("tcp", sslEndpoint, &tls.Config{})
} else {
conn, err = net.Dial("tcp", tcpEndpoint)
}
if err != nil {
return nil, err
}
return &Logger{
token: token,
verbosity: 0,
localLogger: nil,
connection: conn,
}, nil
}
// SetVerbosity sets the minimal log level which will be recorded
func (l *Logger) SetVerbosity(level Level) error {
if _, ok := levelTag[level]; ok {
l.verbosity = level
return nil
}
return errors.New("unknown log level")
}
// SetLocalLogger sets a local log instance
// the log message will be passed to.
func (l *Logger) SetLocalLogger(local *log.Logger) {
l.localLogger = local
}
// Debug is a helper function
func (l *Logger) Debug(message string) error {
return l.Log(Debug, message)
}
// Info is a helper function
func (l *Logger) Info(message string) error {
return l.Log(Info, message)
}
// Warn is a helper function
func (l *Logger) Warn(message string) error {
return l.Log(Warn, message)
}
// Error is a helper function
func (l *Logger) Error(message string) error {
return l.Log(Error, message)
}
// Panic is a helper function
func (l *Logger) Panic(message string) error {
return l.Log(Panic, message)
}
// Log sends a formatted log message logentries
// if the log is higher than the current minimium level.
// If a local logger is set then Log also passes the message to it.
func (l *Logger) Log(level Level, message string) error {
if level >= l.verbosity {
if l.localLogger != nil {
log.Print(message)
}
remoteMessage := l.format(level, message)
return l.send(remoteMessage)
}
return nil
}
// format prepares the log for transmitted by
// prepending the UUID token and appending a new line.
func (l *Logger) format(level Level, message string) string {
return l.token + " " + levelTag[level] + " " + message + "\n"
}
// SendRaw sends an unformatted log
func (l *Logger) SendRaw(message string) error {
log := l.token + " " + message + "\n"
return l.send(log)
}
// send transmits message to LogEntries
func (l *Logger) send(log string) error {
_, err := l.connection.Write([]byte(log))
return err
}