1//  Copyright (c) 2014 Couchbase, Inc.
2//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
3//  except in compliance with the License. You may obtain a copy of the License at
4//    http://www.apache.org/licenses/LICENSE-2.0
5//  Unless required by applicable law or agreed to in writing, software distributed under the
6//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
7//  either express or implied. See the License for the specific language governing permissions
8//  and limitations under the License.
9
10package logger_golog
11
12import (
13	"bytes"
14	"encoding/json"
15	"fmt"
16	"io"
17	"log"
18	"time"
19
20	"github.com/couchbase/query/logging"
21)
22
23type goLogger struct {
24	logger         *log.Logger
25	level          logging.Level
26	entryFormatter formatter
27}
28
29const (
30	_LEVEL  = "_level"
31	_MSG    = "_msg"
32	_TIME   = "_time"
33	_RLEVEL = "_rlevel"
34)
35
36func NewLogger(out io.Writer, lvl logging.Level, jsonLogging bool) *goLogger {
37	logger := &goLogger{
38		logger: log.New(out, "", 0),
39		level:  lvl,
40	}
41	if jsonLogging {
42		logger.entryFormatter = &jsonFormatter{}
43	} else {
44		logger.entryFormatter = &textFormatter{}
45	}
46	return logger
47}
48
49func (gl *goLogger) Logp(level logging.Level, msg string, kv ...logging.Pair) {
50	if gl.logger == nil {
51		return
52	}
53	if level <= gl.level {
54		e := newLogEntry(msg, level)
55		copyPairs(e, kv)
56		gl.log(e)
57	}
58}
59
60func (gl *goLogger) Debugp(msg string, kv ...logging.Pair) {
61	gl.Logp(logging.DEBUG, msg, kv...)
62}
63
64func (gl *goLogger) Tracep(msg string, kv ...logging.Pair) {
65	gl.Logp(logging.TRACE, msg, kv...)
66}
67
68func (gl *goLogger) Requestp(rlevel logging.Level, msg string, kv ...logging.Pair) {
69	if gl.logger == nil {
70		return
71	}
72	if logging.REQUEST <= gl.level {
73		e := newLogEntry(msg, logging.REQUEST)
74		e.Rlevel = rlevel
75		copyPairs(e, kv)
76		gl.log(e)
77	}
78}
79
80func (gl *goLogger) Infop(msg string, kv ...logging.Pair) {
81	gl.Logp(logging.INFO, msg, kv...)
82}
83
84func (gl *goLogger) Warnp(msg string, kv ...logging.Pair) {
85	gl.Logp(logging.WARN, msg, kv...)
86}
87
88func (gl *goLogger) Errorp(msg string, kv ...logging.Pair) {
89	gl.Logp(logging.ERROR, msg, kv...)
90}
91
92func (gl *goLogger) Severep(msg string, kv ...logging.Pair) {
93	gl.Logp(logging.SEVERE, msg, kv...)
94}
95
96func (gl *goLogger) Fatalp(msg string, kv ...logging.Pair) {
97	gl.Logp(logging.FATAL, msg, kv...)
98}
99
100func (gl *goLogger) Logm(level logging.Level, msg string, kv logging.Map) {
101	if gl.logger == nil {
102		return
103	}
104	if level <= gl.level {
105		e := newLogEntry(msg, level)
106		e.Data = kv
107		gl.log(e)
108	}
109}
110
111func (gl *goLogger) Debugm(msg string, kv logging.Map) {
112	gl.Logm(logging.DEBUG, msg, kv)
113}
114
115func (gl *goLogger) Tracem(msg string, kv logging.Map) {
116	gl.Logm(logging.TRACE, msg, kv)
117}
118
119func (gl *goLogger) Requestm(rlevel logging.Level, msg string, kv logging.Map) {
120	if gl.logger == nil {
121		return
122	}
123	if logging.REQUEST <= gl.level {
124		e := newLogEntry(msg, logging.REQUEST)
125		e.Rlevel = rlevel
126		e.Data = kv
127		gl.log(e)
128	}
129}
130
131func (gl *goLogger) Infom(msg string, kv logging.Map) {
132	gl.Logm(logging.INFO, msg, kv)
133}
134
135func (gl *goLogger) Warnm(msg string, kv logging.Map) {
136	gl.Logm(logging.WARN, msg, kv)
137}
138
139func (gl *goLogger) Errorm(msg string, kv logging.Map) {
140	gl.Logm(logging.ERROR, msg, kv)
141}
142
143func (gl *goLogger) Severem(msg string, kv logging.Map) {
144	gl.Logm(logging.SEVERE, msg, kv)
145}
146
147func (gl *goLogger) Fatalm(msg string, kv logging.Map) {
148	gl.Logm(logging.FATAL, msg, kv)
149}
150
151func (gl *goLogger) Logf(level logging.Level, format string, args ...interface{}) {
152	if gl.logger == nil {
153		return
154	}
155	if level <= gl.level {
156		e := newLogEntry(fmt.Sprintf(format, args...), level)
157		gl.log(e)
158	}
159}
160
161func (gl *goLogger) Debugf(format string, args ...interface{}) {
162	gl.Logf(logging.DEBUG, format, args...)
163}
164
165func (gl *goLogger) Tracef(format string, args ...interface{}) {
166	gl.Logf(logging.TRACE, format, args...)
167}
168
169func (gl *goLogger) Requestf(rlevel logging.Level, format string, args ...interface{}) {
170	if gl.logger == nil {
171		return
172	}
173	if logging.REQUEST <= gl.level {
174		e := newLogEntry(fmt.Sprintf(format, args...), logging.REQUEST)
175		e.Rlevel = rlevel
176		gl.log(e)
177	}
178}
179
180func (gl *goLogger) Infof(format string, args ...interface{}) {
181	gl.Logf(logging.INFO, format, args...)
182}
183
184func (gl *goLogger) Warnf(format string, args ...interface{}) {
185	gl.Logf(logging.WARN, format, args...)
186}
187
188func (gl *goLogger) Errorf(format string, args ...interface{}) {
189	gl.Logf(logging.ERROR, format, args...)
190}
191
192func (gl *goLogger) Severef(format string, args ...interface{}) {
193	gl.Logf(logging.SEVERE, format, args...)
194}
195
196func (gl *goLogger) Fatalf(format string, args ...interface{}) {
197	gl.Logf(logging.FATAL, format, args...)
198}
199
200func (gl *goLogger) Level() logging.Level {
201	return gl.level
202}
203
204func (gl *goLogger) SetLevel(level logging.Level) {
205	gl.level = level
206}
207
208func (gl *goLogger) log(newEntry *logEntry) {
209	s := gl.entryFormatter.format(newEntry)
210	gl.logger.Print(s)
211}
212
213type logEntry struct {
214	Time    string
215	Level   logging.Level
216	Rlevel  logging.Level
217	Message string
218	Data    logging.Map
219}
220
221func newLogEntry(msg string, level logging.Level) *logEntry {
222	return &logEntry{
223		Time:    time.Now().Format("2006-01-02T15:04:05.000-07:00"), // time.RFC3339 with milliseconds
224		Level:   level,
225		Rlevel:  logging.NONE,
226		Message: msg,
227	}
228}
229
230func copyPairs(newEntry *logEntry, pairs []logging.Pair) {
231	newEntry.Data = make(logging.Map, len(pairs))
232	for _, p := range pairs {
233		newEntry.Data[p.Name] = p.Value
234	}
235}
236
237type formatter interface {
238	format(*logEntry) string
239}
240
241type textFormatter struct {
242}
243
244func (*textFormatter) format(newEntry *logEntry) string {
245	b := &bytes.Buffer{}
246	appendKeyValue(b, _TIME, newEntry.Time)
247	appendKeyValue(b, _LEVEL, newEntry.Level.String())
248	if newEntry.Rlevel != logging.NONE {
249		appendKeyValue(b, _RLEVEL, newEntry.Rlevel.String())
250	}
251	appendKeyValue(b, _MSG, newEntry.Message)
252	for key, value := range newEntry.Data {
253		appendKeyValue(b, key, value)
254	}
255	b.WriteByte('\n')
256	s := bytes.NewBuffer(b.Bytes())
257	return s.String()
258}
259
260func appendKeyValue(b *bytes.Buffer, key, value interface{}) {
261	if _, ok := value.(string); ok {
262		fmt.Fprintf(b, "%v=%s ", key, value)
263	} else {
264		fmt.Fprintf(b, "%v=%v ", key, value)
265	}
266}
267
268type jsonFormatter struct {
269}
270
271func (*jsonFormatter) format(newEntry *logEntry) string {
272	if newEntry.Data == nil {
273		newEntry.Data = make(logging.Map, 5)
274	}
275	newEntry.Data[_TIME] = newEntry.Time
276	newEntry.Data[_LEVEL] = newEntry.Level.String()
277	if newEntry.Rlevel != logging.NONE {
278		newEntry.Data[_RLEVEL] = newEntry.Rlevel.String()
279	}
280	newEntry.Data[_MSG] = newEntry.Message
281	serialized, _ := json.Marshal(newEntry.Data)
282	s := bytes.NewBuffer(append(serialized, '\n'))
283	return s.String()
284}
285