blob: 57b1165470d3e7e3c535889eccacb7016035f614 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// Copyright 2017 CoreSwitch
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
15package log
16
17import (
18 "fmt"
19 "os"
20 "runtime"
21 "strings"
22
23 "github.com/sirupsen/logrus"
24)
25
26// Output source file information "filename:line number" to the log.
27var SourceField bool = true
28
29// Output function name to the log.
30var FuncField bool = true
31
32//
33func SetLevel(level string) error {
34 l, err := logrus.ParseLevel(level)
35 if err != nil {
36 return err
37 }
38 origLogger.SetLevel(l)
39 return nil
40}
41
42//
43func SetOutput(output string) error {
44 switch output {
45 case "stdout":
46 origLogger.Out = os.Stdout
47 case "stderr":
48 origLogger.Out = os.Stderr
49 default:
50 return fmt.Errorf("output format error")
51 }
52 return nil
53}
54
55//
56func SetJSONFormatter() {
57 origLogger.Formatter = &logrus.JSONFormatter{}
58}
59
60//
61func SetTextFormatter() {
62 origLogger.Formatter = &logrus.TextFormatter{
63 DisableTimestamp: true,
64 }
65}
66
67type Logger interface {
68 Debug(...interface{})
69 Debugf(string, ...interface{})
70 Info(...interface{})
71 Infof(string, ...interface{})
72 Warn(...interface{})
73 Warnf(string, ...interface{})
74 Error(...interface{})
75 Errorf(string, ...interface{})
76 With(key string, value interface{}) Logger
77}
78
79type logger struct {
80 entry *logrus.Entry
81}
82
83var origLogger = logrus.New()
84var baseLogger = logger{entry: logrus.NewEntry(origLogger)}
85
86func (l logger) sourced() *logrus.Entry {
87 e := l.entry
88
89 if !SourceField && !FuncField {
90 return e
91 }
92
93 pc, file, line, ok := runtime.Caller(2)
94 if !ok {
95 return e
96 }
97
98 if SourceField {
99 slash := strings.LastIndex(file, "/")
100 file = file[slash+1:]
101 e = e.WithField("source", fmt.Sprintf("%s:%d", file, line))
102 }
103
104 if FuncField {
105 if f := runtime.FuncForPC(pc); f != nil {
106 e = e.WithField("func", f.Name())
107 }
108 }
109
110 return e
111}
112
113func (l logger) Debug(args ...interface{}) {
114 l.sourced().Debug(args...)
115}
116
117func (l logger) Debugf(format string, args ...interface{}) {
118 l.sourced().Debugf(format, args...)
119}
120
121func (l logger) Info(args ...interface{}) {
122 l.sourced().Info(args...)
123}
124
125func (l logger) Infof(format string, args ...interface{}) {
126 l.sourced().Infof(format, args...)
127}
128
129func (l logger) Warn(args ...interface{}) {
130 l.sourced().Warn(args...)
131}
132
133func (l logger) Warnf(format string, args ...interface{}) {
134 l.sourced().Warnf(format, args...)
135}
136
137func (l logger) Error(args ...interface{}) {
138 l.sourced().Error(args...)
139}
140
141func (l logger) Errorf(format string, args ...interface{}) {
142 l.sourced().Errorf(format, args...)
143}
144
145func (l logger) With(key string, value interface{}) Logger {
146 return logger{l.entry.WithField(key, value)}
147}
148
149func With(key string, value interface{}) Logger {
150 return baseLogger.With(key, value)
151}
152
153func Debug(args ...interface{}) {
154 baseLogger.sourced().Debug(args...)
155}
156
157func Debugf(format string, args ...interface{}) {
158 baseLogger.sourced().Debugf(format, args...)
159}
160
161func Info(args ...interface{}) {
162 baseLogger.sourced().Info(args...)
163}
164
165func Infof(format string, args ...interface{}) {
166 baseLogger.sourced().Infof(format, args...)
167}
168
169func Warn(args ...interface{}) {
170 baseLogger.sourced().Warn(args...)
171}
172
173func Warnf(format string, args ...interface{}) {
174 baseLogger.sourced().Warnf(format, args...)
175}
176
177func Error(args ...interface{}) {
178 baseLogger.sourced().Error(args...)
179}
180
181func Errorf(format string, args ...interface{}) {
182 baseLogger.sourced().Errorf(format, args...)
183}