William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 1 | // 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 | |
| 17 | package capnslog |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "log/syslog" |
| 22 | ) |
| 23 | |
| 24 | func NewSyslogFormatter(w *syslog.Writer) Formatter { |
| 25 | return &syslogFormatter{w} |
| 26 | } |
| 27 | |
| 28 | func 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 | |
| 36 | type syslogFormatter struct { |
| 37 | w *syslog.Writer |
| 38 | } |
| 39 | |
| 40 | func (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 | |
| 64 | func (s *syslogFormatter) Flush() { |
| 65 | } |