blob: 4be5a1f2de395648067d34283263b483f6ca1a59 [file] [log] [blame]
divyadesai81bb7ba2020-03-11 11:45:23 +00001// Copyright 2015 CoreOS, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// +build !windows
16
17package capnslog
18
19import (
20 "fmt"
21 "log/syslog"
22)
23
24func NewSyslogFormatter(w *syslog.Writer) Formatter {
25 return &syslogFormatter{w}
26}
27
28func NewDefaultSyslogFormatter(tag string) (Formatter, error) {
29 w, err := syslog.New(syslog.LOG_DEBUG, tag)
30 if err != nil {
31 return nil, err
32 }
33 return NewSyslogFormatter(w), nil
34}
35
36type syslogFormatter struct {
37 w *syslog.Writer
38}
39
40func (s *syslogFormatter) Format(pkg string, l LogLevel, _ int, entries ...interface{}) {
41 for _, entry := range entries {
42 str := fmt.Sprint(entry)
43 switch l {
44 case CRITICAL:
45 s.w.Crit(str)
46 case ERROR:
47 s.w.Err(str)
48 case WARNING:
49 s.w.Warning(str)
50 case NOTICE:
51 s.w.Notice(str)
52 case INFO:
53 s.w.Info(str)
54 case DEBUG:
55 s.w.Debug(str)
56 case TRACE:
57 s.w.Debug(str)
58 default:
59 panic("Unhandled loglevel")
60 }
61 }
62}
63
64func (s *syslogFormatter) Flush() {
65}