forked from whyrusleeping/go-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.go
52 lines (45 loc) · 1.51 KB
/
syslog.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
// Copyright 2013, Örjan Persson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//+build !windows,!plan9
package logging
import "log/syslog"
// SyslogBackend is a simple logger to syslog backend. It automatically maps
// the internal log levels to appropriate syslog log levels.
type SyslogBackend struct {
Writer *syslog.Writer
}
// NewSyslogBackend connects to the syslog daemon using UNIX sockets with the
// given prefix. If prefix is not given, the prefix will be derived from the
// launched command.
func NewSyslogBackend(prefix string) (b *SyslogBackend, err error) {
var w *syslog.Writer
w, err = syslog.New(syslog.LOG_CRIT, prefix)
return &SyslogBackend{w}, err
}
// NewSyslogBackendPriority is the same as NewSyslogBackend, but with custom
// syslog priority, like syslog.LOG_LOCAL3|syslog.LOG_DEBUG etc.
func NewSyslogBackendPriority(prefix string, priority syslog.Priority) (b *SyslogBackend, err error) {
var w *syslog.Writer
w, err = syslog.New(priority, prefix)
return &SyslogBackend{w}, err
}
func (b *SyslogBackend) Log(level Level, calldepth int, rec *Record) error {
line := rec.Formatted(calldepth + 1)
switch level {
case CRITICAL:
return b.Writer.Crit(line)
case ERROR:
return b.Writer.Err(line)
case WARNING:
return b.Writer.Warning(line)
case NOTICE:
return b.Writer.Notice(line)
case INFO:
return b.Writer.Info(line)
case DEBUG:
return b.Writer.Debug(line)
default:
}
panic("unhandled log level")
}