forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
50 lines (40 loc) · 1.26 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2017 Gerasimos Maropoulos, ΓΜ. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logger
import (
"fmt"
"io"
)
// writerFunc is just an "extended" io.Writer which provides
// some "methods" which can help applications
// to adapt their existing loggers inside Iris.
type writerFunc func(p []byte) (n int, err error)
func (w writerFunc) Write(p []byte) (int, error) {
return w(p)
}
type formatPrintWriter interface {
Printf(string, ...interface{})
}
type stringWriter interface {
WriteString(string) (int, error)
}
// Log sends a message to the defined logger of io.Writer logger, it's
// just a help function for internal use but it can be used to a cusotom middleware too.
//
// See AttachLogger too.
func Log(w io.Writer, format string, a ...interface{}) {
// check if the user's defined logger is one of the "high"
// level printers, if yes then use their functions to print instead
// of allocating new byte slices.
if fpw, ok := w.(formatPrintWriter); ok {
fpw.Printf(format, a...)
return
}
formattedMessage := fmt.Sprintf(format, a...)
if sWriter, ok := w.(stringWriter); ok {
sWriter.WriteString(formattedMessage)
return
}
w.Write([]byte(formattedMessage))
}