blob: 72e05207c52ce0d6055442004274565cc91b1a91 [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 "errors"
21 "fmt"
22 "os"
23 "path/filepath"
24
25 "github.com/coreos/go-systemd/journal"
26)
27
28func NewJournaldFormatter() (Formatter, error) {
29 if !journal.Enabled() {
30 return nil, errors.New("No systemd detected")
31 }
32 return &journaldFormatter{}, nil
33}
34
35type journaldFormatter struct{}
36
37func (j *journaldFormatter) Format(pkg string, l LogLevel, _ int, entries ...interface{}) {
38 var pri journal.Priority
39 switch l {
40 case CRITICAL:
41 pri = journal.PriCrit
42 case ERROR:
43 pri = journal.PriErr
44 case WARNING:
45 pri = journal.PriWarning
46 case NOTICE:
47 pri = journal.PriNotice
48 case INFO:
49 pri = journal.PriInfo
50 case DEBUG:
51 pri = journal.PriDebug
52 case TRACE:
53 pri = journal.PriDebug
54 default:
55 panic("Unhandled loglevel")
56 }
57 msg := fmt.Sprint(entries...)
58 tags := map[string]string{
59 "PACKAGE": pkg,
60 "SYSLOG_IDENTIFIER": filepath.Base(os.Args[0]),
61 }
62 err := journal.Send(msg, pri, tags)
63 if err != nil {
64 fmt.Fprintln(os.Stderr, err)
65 }
66}
67
68func (j *journaldFormatter) Flush() {}