iris/core/logger/logger.go
kataras 74989ad0a1 Remove internal/cmd/gen, it has no use for the end-developer.
Former-commit-id: 12bead9d379c6644e391c482fe71b2e88263376c
2017-06-11 23:58:34 +03:00

51 lines
1.3 KiB
Go

// 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))
}